4
4
from .analyze_luminance import analyze_luminance
5
5
6
6
7
- def analyze_image (image ):
7
+ def analyze_image (image , features = "all" ):
8
8
"""Compute Objective Characteristics of an Image
9
9
10
10
Compute the physical characteristics of an image.
@@ -13,6 +13,9 @@ def analyze_image(image):
13
13
----------
14
14
image : ndarray
15
15
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'.
16
19
17
20
Returns
18
21
----------
@@ -30,7 +33,7 @@ def analyze_image(image):
30
33
>>> # Visualize: plt.imshow(image)
31
34
>>>
32
35
>>> # Compute color
33
- >>> out = pyllusion.analyze_image(image)
36
+ >>> out = pyllusion.analyze_image(image, features="all" )
34
37
>>> out["Entropy"]
35
38
36
39
"""
@@ -45,16 +48,26 @@ def analyze_image(image):
45
48
"Please install it first (`pip install scikit-image`)." ,
46
49
)
47
50
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 )
60
73
return out
0 commit comments