how to measure the percentage of some colors in image?
27 views (last 30 days)
Show older comments
eran golden
on 9 Jun 2015
Commented: Image Analyst
on 3 Feb 2019
I'm going to use the "impixel" function to get the pixel value. now I want to know the percentage of the area that darker than the pixel value I took with the "impixel". which function can I use?
*my image isn't B&W
2 Comments
Walter Roberson
on 9 Jun 2015
When you say "darker" do you mean whose brightness (grayscale intensity) is less than the brightness at the current location?
Or are you looking on a color-plane by color-plane basis, like
(R < current_red) | (G < current_green) | (B < current_blue)
which can include pixels that are brighter in a grayscale sense?
Accepted Answer
Image Analyst
on 10 Jun 2015
Try something like this (untested)
msgbox('Click on a point');
[x,y] = ginput(1);
row = round(y);
column = round(x);
% Convert to hsv color space
hsvImage = rgb2hsv(rgbImage);
% Get V of where they clicked:
vRef = hsvImage(row, column, 3);
% Get map of where pixels are darker than ref
mask = hsvImage(:,:,3) < vRef;
% Count the pixels that are darker
numDarkerPixels = sum(mask(:))
4 Comments
Image Analyst
on 12 Jun 2015
Looks right, though it might be simpler if you did this:
[rows, columns, numberOfColorChannels] = size(hsvImage);
numberOfPixels = rows * columns;
percent = numDarkerPixels / numberOfPixels;
I still think the number of darker pixels than where they clicked is not as good a method as the delta E method though. Anyway, if you think you have a working solution, can you officially "Accept" this answer?
More Answers (2)
Walter Roberson
on 10 Jun 2015
Let V be the vector of pixel components you want to test against. Then
Vbright = rgb2gray(reshape(V,1,1,3));
Ibright = rgb2gray(YourImage);
precent_darker = mean(Ibright <= Vbright) * 100;
4 Comments
lasmer habiba
on 18 May 2017
how I can calculate the percentage of color on a mammographic image ???
3 Comments
corona
on 3 Feb 2019
Dear Image Analyst
I have some images in red,blue,yellow,green and background is in black (attached). I wish to find out how much is the percentage of each colour in entire image. Kindly advise.
Cheers
corona
Image Analyst
on 3 Feb 2019
Start a new question with this, and say there in that new question (not here)
- if you know the colors (RGB values) in advance or
- if you know how many colors there are (four in this image), or
- if you need to find out how many colors there are.
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!