Skip to content

Commit 8a20900

Browse files
add 'features' option to analyze_image
1 parent b8d4192 commit 8a20900

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

pyllusion/utilities/analyze_color.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ def analyze_color(image, average=True):
7676
out["RedGreen"] = lab[:, :, 1]
7777
out["BlueYellow"] = lab[:, :, 2]
7878

79+
# Average all elements
7980
if average is True:
80-
# Average all elements
8181
out = {key: np.mean(value) for (key, value) in out.items()}
8282

8383
return out

pyllusion/utilities/analyze_image.py

+27-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from .analyze_luminance import analyze_luminance
55

66

7-
def analyze_image(image):
7+
def analyze_image(image, features="all"):
88
"""Compute Objective Characteristics of an Image
99
1010
Compute the physical characteristics of an image.
@@ -13,6 +13,9 @@ def analyze_image(image):
1313
----------
1414
image : ndarray
1515
Array for R, G and B channels.
16+
features : str
17+
Which features to extract. Can be 'all' or a list that can contain 'luminance', 'color',
18+
'entropy', 'colorfulness', 'contrast', 'structure'.
1619
1720
Returns
1821
----------
@@ -30,7 +33,7 @@ def analyze_image(image):
3033
>>> # Visualize: plt.imshow(image)
3134
>>>
3235
>>> # Compute color
33-
>>> out = pyllusion.analyze_image(image)
36+
>>> out = pyllusion.analyze_image(image, features="all")
3437
>>> out["Entropy"]
3538
3639
"""
@@ -45,16 +48,26 @@ def analyze_image(image):
4548
"Please install it first (`pip install scikit-image`).",
4649
)
4750

48-
out = analyze_luminance(image, average=True)
49-
out.update(analyze_color(image, average=True))
50-
out["Entropy"] = skimage.measure.shannon_entropy(image, base=2)
51-
# SD of the HUE axis (the colors)
52-
out["Colorfulness"] = np.std(skimage.color.rgb2hsv(image)[:, :, 0])
53-
# SD of the Luminance axis
54-
out["Contrast"] = np.std(skimage.color.rgb2lab(image)[:, :, 0])
55-
56-
# Edge detection
57-
bw = skimage.color.rgb2gray(image)
58-
edges = skimage.filters.sobel(bw) # skimage.filters.roberts or skimage.feature.canny
59-
out["Structure"] = skimage.measure.shannon_entropy(edges, base=2)
51+
if features == "all":
52+
features = ["luminance", "color", "entropy", "colorfulness", "contrast", "structure"]
53+
54+
out = {}
55+
if "luminance" in features:
56+
out.update(analyze_luminance(image, average=True))
57+
if "color" in features:
58+
out.update(analyze_color(image, average=True))
59+
if "entropy" in features:
60+
out["Entropy"] = skimage.measure.shannon_entropy(image, base=2)
61+
if "colorfulness" in features:
62+
# SD of the HUE axis (the colors)
63+
out["Colorfulness"] = np.std(skimage.color.rgb2hsv(image)[:, :, 0])
64+
if "contrast" in features:
65+
# SD of the Luminance axis
66+
out["Contrast"] = np.std(skimage.color.rgb2lab(image)[:, :, 0])
67+
68+
if "structure" in features:
69+
bw = skimage.color.rgb2gray(image)
70+
# Edge detection
71+
edges = skimage.filters.sobel(bw) # skimage.filters.roberts or skimage.feature.canny
72+
out["Structure"] = skimage.measure.shannon_entropy(edges, base=2)
6073
return out

0 commit comments

Comments
 (0)