Test image for color, grayscale, or single hue gradient attachment
mouse 713 · person cloud · link
Last update
2016-12-20
2016
12-20
« — »
Cover original

Discussing with the amazing guys at ImageMagick forums I ended up with this solution:

  1. compute sat_mean: the mean saturation percentage (1-100%)
  2. compute hue_st_dev: the standard deviation of the unique hues (without considering their absolute frequency)
  3. compute hue2_st_dev: the standard deviation of the 180° shifted unique hues (to workaround the red hue wrapping at 360°)
  4. if sat_mean >= 30, then it's a colored image
  5. if sat_mean < 10, then it's a grayscale image
  6. if sat_mean is between 10 and 30, compute hue_sd = MIN(hue_st_dev, hue2_st_dev): if hue_sd > 25 then it's a colored image otherwise it's a grayscale one.

In order to compute those values I extracted the image's histogram, converting the image to 64 colors:

1
convert fname.ext -dither FloydSteinberg -colors 64 -colorspace HSB -format %c histogram:info:-

in this manner I have a good enough approximation of the image hues and a smaller dataset to elaborate.

Before computing hue std.dev I removed all low saturation rows as their corresponding hues are not meaningful.

Attached to this post you can find a Ruby script I wrote to perform this test.

For the image set in the post cover the results are the following (images in alphabetical order):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
bash$ ls|sort|while read fname; do ../test.rb "$fname"; done
hq_1-color.jpg                  S:58.40  H:35.78  H2:21.83  N:64  R: 5  O: 20%  =>    colored
hq_1-color-low.jpg              S:48.27  H:33.78  H2:24.44  N:64  R: 6  O: 24%  =>    colored
hq_2-gray1.png                  S: 0.00  H: 0.00  H2: 0.00  N:64  R:64  O:100%  =>    grayscale
hq_2-gray2.png                  S: 0.00  H: 0.00  H2: 0.00  N:64  R:64  O:100%  =>    grayscale
hq_2-gray3.png                  S: 0.00  H: 0.00  H2: 0.00  N:64  R:64  O:100%  =>    grayscale
hq_3-col1_H240-S50-L0.png       S:17.38  H: 0.00  H2: 0.00  N:63  R: 5  O: 74%  =>    monochrome_mid_sat
hq_3-col1_H250-S60-L0.png       S:18.88  H: 0.49  H2: 0.49  N:64  R: 4  O: 73%  =>    monochrome_mid_sat
hq_3-col1_H40-S50-L0.png        S:17.38  H: 0.72  H2: 0.72  N:64  R: 5  O: 74%  =>    monochrome_mid_sat
hq_3-col2_H360-S80-L0.png       S:15.12  H: 0.00  H2: 0.00  N:63  R: 3  O: 82%  =>    monochrome_mid_sat
hq_3-col3_H360-S80-L40.png      S:20.81  H: 0.00  H2: 0.00  N:61  R: 4  O: 75%  =>    monochrome_mid_sat
hq_3-gradient.jpg               S:23.02  H: 7.88  H2: 5.97  N:64  R:11  O: 50%  =>    monochrome_mid_sat
hq_4-monochrome.png             S: 0.00  H: 0.00  H2: 0.00  N: 2  R: 2  O:100%  =>    grayscale
hq_5-white-red.png              S:20.88  H:  NaN  H2:  NaN  N: 2  R: 1  O: 79%  =>    monochrome
lq_1-color.jpg                  S:54.94  H:35.62  H2:22.30  N:64  R:12  O: 30%  =>    colored
lq_1-color-low.jpg              S:44.92  H:33.14  H2:24.68  N:64  R:10  O: 33%  =>    colored
lq_2-gray1.jpg                  S: 0.00  H: 0.00  H2: 0.00  N:16  R:16  O:100%  =>    grayscale
lq_2-gray2.jpg                  S: 0.00  H: 0.00  H2: 0.00  N:16  R:16  O:100%  =>    grayscale
lq_2-gray3.jpg                  S: 0.00  H: 0.00  H2: 0.00  N:16  R:16  O:100%  =>    grayscale
lq_3-col1_H240-S50-L0.jpg       S:15.24  H:16.70  H2:16.87  N:64  R:16  O: 74%  =>    monochrome_mid_sat
lq_3-col1_H250-S60-L0.jpg       S:17.24  H:16.48  H2:16.74  N:64  R:15  O: 75%  =>    monochrome_mid_sat
lq_3-col1_H40-S50-L0.jpg        S:16.57  H:20.04  H2:15.23  N:64  R:18  O: 71%  =>    monochrome_mid_sat
lq_3-col2_H360-S80-L0.jpg       S:14.02  H:41.89  H2:14.72  N:64  R:18  O: 80%  =>    monochrome_mid_sat
lq_3-col3_H360-S80-L40.jpg      S:23.07  H:38.80  H2:16.24  N:64  R: 9  O: 56%  =>    monochrome_mid_sat
lq_3-gradient.jpg               S:12.58  H:10.14  H2:10.39  N:64  R:17  O: 78%  =>    monochrome_mid_sat
lq_4-monochrome.jpg             S: 0.00  H: 0.00  H2: 0.00  N:16  R:16  O:100%  =>    grayscale
lq_5-white-red.jpg              S:24.19  H:41.33  H2:20.64  N:64  R: 4  O: 69%  =>    monochrome_mid_sat

the columns are:

  • S saturation mean
  • H unique hue standard deviation
  • H2 180° shifted unique hue standard deviation
  • N number of colors
  • R removed rows from the histogram (outlayers)
  • O percent of outlayered pixels
  • resulting image type

Images named lq_* are the quality=1 JPEG conversion of the same image set.


Source: ImageMagick thread