How does one get a certain number of DominantColors and is it always possible to do so? I have tried specifying the number as well as the option ColorCoverage -> 0.
Asked
Active
Viewed 192 times
3
J. M.'s missing motivation
- 124,525
- 11
- 401
- 574
Alderson
- 213
- 1
- 4
1 Answers
1
This seems to work in every case I've tried (v. 10.4, Mac).
ColorQuantize forces the number of colors in the image. Here's a screenshot of the original:
Here's the quantized image:
quantizedImage = ColorQuantize[img, 5]
My code worked fine on the screen shot of the poser's image, though admittedly the colors are rather dark.
Here are the extracted colors:
DominantColors[quantizedImage, 5]
{RGBColor[0.242519, 0.347167, 0.144226],
RGBColor[0.233793, 0.319928, 0.134609],
RGBColor[0.357113, 0.483923, 0.216957],
RGBColor[0.340756, 0.432562, 0.162432],
RGBColor[0.16798, 0.278479, 0.11706]}
or
Graphics[
Table[{SortBy[DominantColors[quantizedImage, 5],Mean][[i]],
Disk[{2 i, 0}]},
{i, 5}]]
I believe the dark colors arise because most of the image is quite dark, and the light highlights, while centered, represent a very small portion of the total image.
David G. Stork
- 41,180
- 3
- 34
- 96
-
1I tried to grab the OP's image, had to do a screencapture for it, but here it is. But I couldn't get it to work with your code – Jason B. Jan 11 '17 at 20:01
-
1
-
1This does give the five colors from the quantized image, they are a bit darker than the dominant colors found by the built-in function:
RGBColor@*Most /@ Union[Flatten[ImageData[quantizedImage], 1]]– Jason B. Jan 11 '17 at 20:06 -
1I have been using this function for color extraction, even though it does not produce the desired output:
color[img_] := MapAt[RGBColor, Tally[Flatten[ImageData[ColorQuantize[ImageResize[img, 50], 5]], 1]], {All, 1}] col[img_] := First[#] & /@ color[img];– Alderson Jan 11 '17 at 20:10 -
1




DominantColors[img, no. of colors]. Your problem may not be so trivial. In that case give an example where you think it is not working. – Sumit Jan 11 '17 at 17:59DominantColors[img,8, {"Color", "Count"}]It would be better if your uploaded example was just the image, not a screenshot with the white around it..BTW. – george2079 Jan 11 '17 at 22:34DominantColorsreturns less than the requested number. (unless starting with an image with fewer discrete colors) @E.Alderson what version do you have? Can you see if you have this issue with some standard example, egExampleData[{"TestImage", "Splash"}]– george2079 Jan 11 '17 at 23:07img=ImageMultiply[ColorConvert[ExampleData[{"TestImage", "Splash"}], "Grayscale"], Red]it works fine. – george2079 Jan 12 '17 at 15:43