how to measure the percentage of some colors in image?

27 views (last 30 days)
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
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?
eran golden
eran golden on 10 Jun 2015
I'm not sure that I understand your question... what I really want to do is to calculate the percentage of a given pixel-value in the pic:
for example, put your mouse on some point at the pic and want to calculate how much area I have in that pixel-value or darker (yes, as you wrote, "brightness (grayscale intensity) is less than the brightness at the current location"). thanks!

Sign in to comment.

Accepted Answer

Image Analyst
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
eran golden
eran golden on 12 Jun 2015
OK. i tried your code, it seems to be working! here is how i used it:
function [ percent ] = color_percentage( picname )
rgbImage=imread(picname);
imshow(rgbImage);
[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(:));
hsvImage_size=size(hsvImage);
numofpixels=hsvImage_size(1).*hsvImage_size(2);
percent=numDarkerPixels./numofpixels;
end
it seems that it gives the right claculation. can you take a look at it and see if I did any mistake? I'm new to matlab as you probably notice...
Image Analyst
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?

Sign in to comment.

More Answers (2)

Walter Roberson
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
eran golden
eran golden on 11 Jun 2015
I'm sorry, still haven't get what I need. here is my code:
mypic=imread('grains.jpg');
V=impixel(mypic)
Vbright = rgb2gray(reshape(V,1,1,3));
Ibright = rgb2gray(mypic);
precent_darker = mean(Ibright <= Vbright) * 100;
what i need to do next in order to get the percentage of the pixel? the variable 'precent_darker' is a matrix, I want to get a number (=X%) thanks for your kindness :)

Sign in to comment.


lasmer habiba
lasmer habiba on 18 May 2017
how I can calculate the percentage of color on a mammographic image ???
  3 Comments
corona
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
coronaexample.jpg
Image Analyst
Image Analyst on 3 Feb 2019
Start a new question with this, and say there in that new question (not here)
  1. if you know the colors (RGB values) in advance or
  2. if you know how many colors there are (four in this image), or
  3. if you need to find out how many colors there are.

Sign in to comment.

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!