Can i use Color map to find which color is the most used color in an image ?
5 views (last 30 days)
Show older comments
How can i find dominant color from an image( Yellow, green , red , blue, orange , black , white etc..)? What is the basic method benind this ??
6 Comments
Walter Roberson
on 24 Jun 2018
No, black is the most used color. Run the image through https://www.mathworks.com/matlabcentral/fileexchange/28164-color-frequency-image
Answers (3)
Image Analyst
on 23 Jun 2018
Edited: Image Analyst
on 23 Jun 2018
You can display the 3-D color gamut with colorcloud(). Call that on your image and see what you think the dominant color is.
To give a good answer we need to know what you plan on doing assuming you had the "dominant color". If we knew that we might be able to suggest something. Post your image(s) and explain what you want to know about them or do with them assuming you knew the dominant color.
Finally, look at these fun web sites:
8 Comments
Walter Roberson
on 24 Jun 2018
Edited: Image Analyst
on 24 Jun 2018
When you ran the code, did it not pop up a file selector with title "Select a color image file" ?
Jan
on 23 Jun 2018
Edited: Jan
on 23 Jun 2018
Walter is right. What is the dominant color of the French flag? Imagine a photo of a traffic light in the dusk. Most of the pixels are dark gray, but if you ask a person about the color, you get the answer green (or yellow, or red). Of course there are some rare examples, which allows a unique decision about the most frequent RGB or Lab color. But even then there is no unique relation to a name of a color. Do you remember the discussion about the blue/black or gold/white dress (link) ? Even human could not decide which color the image contain.
What is the basic method behind this ?
There is no basic method. If you have a clear and unique definition, and an exactly defined workflow considering the color profiles of the camera, monitor, etc., it can be implemented in Matlab.
Currently the best method is to show the image printed on paper or shown on a calibrated monitor to 10 persons. If they agree, there is a high chance that other human have the same impression.
2 Comments
Jan
on 25 Jun 2018
Did you even read my answer? Start with defining, what a "dominant color" is.
close all; clear all; clc; warning off;
This is a really bad idea: Killing everything and disabling all warnings is brute and provokes errors.
Walter Roberson
on 24 Jun 2018
topN = 50;
img = imread('demo.jpg');
rimg = reshape(img, [], 3);
[urgb, ~, uidx] = unique(rimg, 'rows');
labs = reshape(uint32(uidx),size(img,1),size(img,2));
recolored = uint8(ind2rgb(labs-1, urgb));
subplot(2,2,1)
image(img);
title('original')
counts = accumarray(uidx,1);
[sortedcounts, sortidx] = sort(counts,'descend');
mcc1 = urgb(sortidx(1),:);
mcc2 = urgb(sortidx(2),:);
subplot(2,2,3)
image(reshape(mcc1,1,1,3))
title(sprintf('Most common color [%d,%d,%d]\n%d occurrences', mcc1, sortedcounts(1)));
subplot(2,2,4)
image(reshape(mcc2,1,1,3))
title(sprintf('Second most common color [%d,%d,%d]\n%d occurrences', mcc2, sortedcounts(2)));
onlytop = double(ismember(labs,sortidx(1:topN)));
subplot(2,2,2)
image(recolored, 'alphadata', onlytop);
title(sprintf('showing only top %d colors', topN));
set(gca, 'color', 'k')
You have to go to the 47th most common RGB combination to get one that is not essentially black. No yellow-ish RGB combination occupies more than 96 pixels.
6 Comments
Walter Roberson
on 24 Jun 2018
5. Then, find the unique hsv combinations in sorted numeric order (first output of unique). Also, for each original row, find the index of which of the unique rows the pixel was. For example [0 0 0] is always going to sort first in the list, which corresponds to value 1 (first in unique list), and then any entries in the uidx list that come out 1 do so because the corresponding row in rimg was [0 0 0]
6. Reshape this list of indices to be 2D. This is now an array of color numbers suitable for use in pseudocolor work. All of the pixels that matched the hsv triple uhsv(1,:) originally will have value 1, all the pixels that matched the hsv triple uhsv(2,:) originally will have value 2, and so on. So now uhsv is effectively a color map and labs is the index into the color map for each original pixel.
7. For testing purpose, we then put the image back together in the array "recoloredhsv". Subtracting 1 from labs is done for technical reasons involving whether 0 or 1 is to be used to designate the first color in the list. This might not be the same as the hsv image because I did not pay attention to whether rgb2hsv produces floating point or integer values; probably recoloredhsv is not useful without a correction to that line. I did not use the array in the later code.
11. We then count how many of each unique color there were by counting the number of times each index occurred. That is done by using accumarray().
The line
counts = accumarray(uidx,1);
is equivalent to (but much more efficient than)
counts = [];
for IDX = 1 : numel(uidx)
temp = uidx(IDX);
if size(counts,1) < temp
counts(temp,1) = 1;
else
counts(temp,1) = counts(temp,1) + 1;
end
end
12. Then we sort the counts in descending order, producing also the list of where in the original order the count came from. So sortedindex(1) tells you which counts() entry had the highest count and sortedcounts(1) tells you what the corresponding count was. If you need more information about what the sort index means, see https://www.mathworks.com/help/matlab/ref/sort.html#bt8nojg-1-I
13 and 14. Then we extract the unique hsv triple that was associated with the most common color and also the second most common color.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!