Skip to content

Commit

Permalink
add 'features' option to analyze_image
Browse files Browse the repository at this point in the history
  • Loading branch information
DominiqueMakowski committed Feb 25, 2022
1 parent b8d4192 commit 8a20900
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pyllusion/utilities/analyze_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def analyze_color(image, average=True):
out["RedGreen"] = lab[:, :, 1]
out["BlueYellow"] = lab[:, :, 2]

# Average all elements
if average is True:
# Average all elements
out = {key: np.mean(value) for (key, value) in out.items()}

return out
Expand Down
41 changes: 27 additions & 14 deletions pyllusion/utilities/analyze_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .analyze_luminance import analyze_luminance


def analyze_image(image):
def analyze_image(image, features="all"):
"""Compute Objective Characteristics of an Image
Compute the physical characteristics of an image.
Expand All @@ -13,6 +13,9 @@ def analyze_image(image):
----------
image : ndarray
Array for R, G and B channels.
features : str
Which features to extract. Can be 'all' or a list that can contain 'luminance', 'color',
'entropy', 'colorfulness', 'contrast', 'structure'.
Returns
----------
Expand All @@ -30,7 +33,7 @@ def analyze_image(image):
>>> # Visualize: plt.imshow(image)
>>>
>>> # Compute color
>>> out = pyllusion.analyze_image(image)
>>> out = pyllusion.analyze_image(image, features="all")
>>> out["Entropy"]
"""
Expand All @@ -45,16 +48,26 @@ def analyze_image(image):
"Please install it first (`pip install scikit-image`).",
)

out = analyze_luminance(image, average=True)
out.update(analyze_color(image, average=True))
out["Entropy"] = skimage.measure.shannon_entropy(image, base=2)
# SD of the HUE axis (the colors)
out["Colorfulness"] = np.std(skimage.color.rgb2hsv(image)[:, :, 0])
# SD of the Luminance axis
out["Contrast"] = np.std(skimage.color.rgb2lab(image)[:, :, 0])

# Edge detection
bw = skimage.color.rgb2gray(image)
edges = skimage.filters.sobel(bw) # skimage.filters.roberts or skimage.feature.canny
out["Structure"] = skimage.measure.shannon_entropy(edges, base=2)
if features == "all":
features = ["luminance", "color", "entropy", "colorfulness", "contrast", "structure"]

out = {}
if "luminance" in features:
out.update(analyze_luminance(image, average=True))
if "color" in features:
out.update(analyze_color(image, average=True))
if "entropy" in features:
out["Entropy"] = skimage.measure.shannon_entropy(image, base=2)
if "colorfulness" in features:
# SD of the HUE axis (the colors)
out["Colorfulness"] = np.std(skimage.color.rgb2hsv(image)[:, :, 0])
if "contrast" in features:
# SD of the Luminance axis
out["Contrast"] = np.std(skimage.color.rgb2lab(image)[:, :, 0])

if "structure" in features:
bw = skimage.color.rgb2gray(image)
# Edge detection
edges = skimage.filters.sobel(bw) # skimage.filters.roberts or skimage.feature.canny
out["Structure"] = skimage.measure.shannon_entropy(edges, base=2)
return out

0 comments on commit 8a20900

Please sign in to comment.