How do I define a color?
37 views (last 30 days)
I am carrying out an image segmentation and during this I define colour as the specific colour I need segmented by clicking on the image itself. However, is it possible for me to put in that colour is equal to a specific colour?
[x, y] = ginput(1);
This is basically the code I use and then it gives me the option to pick the spot I want and it defines it as that color. Is there a function I can use to define it as the color permanently, without needing me to pick it every time.
Salaheddin Hosseinzadeh on 24 Aug 2014
Oh yeah, It's crystal clear now!
So, for instance the color you want to pick is black and you just want to define it.
I took a look at your question again. ginput gives you the x and y in respect to x and y axes, for images, ginput returns the pixel number (pixel 0 0 is on the top and left of the image) so it does not provide any color info on it's own.
You can use the result of the ginput to get the color though. Depending on the type of the image you're loading to MATLAB, you'll have different data types, lets say you loaded a colored image of type PNG. then you'll have a (Xpixel x Ypixelcdata x 3) cdata (color data) matrix. 3rd dimension is 3, representing the red, green and blue and they're uint8 type, means the're in between 0 to 255.
So let's say you want to red color. then
myPic(1,1,1:3) = [255,0,0]
myPic(1,2,1:3) = [0,255,0]
myPic(1,3,1:3) = [0,0,255]
to define a color for your purpose I believe you've to do it as such
color = [255,100,44] % whatever color it is!
Just combine the three colors with with different amounts and produce what ever color you want.
If you want to cheat from the image, just use ginput to get the x and y coordinates of the desired pixel then in the cdata matrix or whatever matrix you named it, I named it myPic, get the color values to understand their mixture.
Hope this can answer your question, sorry I got back to you a bit late.
More Answers (1)
Image Analyst on 25 Aug 2014
If you want to count that one particular, specific color, just define it and find it.
myColor = [142, 129, 204]; % Or whatever you want.
% Find the red color
binaryRed = rgbImage(:,:,1) == myColor(1);
% Find the green color
binaryGreen = rgbIage(:,:,1) == myColor(2);
% Find the blue color
binaryBlue = rgbImage(:,:,1) == myColor(3);
% Count how many times it matches all 3 color channels anywhere in the image.
thatColor = binaryRed & binaryGreen & binaryBlue; % 2d binary map of where this color lives
countForThatColor = sum(thatColor(:)); % Sum up to do the counting.
If you want to do it for more than just one color and want to do it for the whole image (every color in the image), then you want the "color frequency image" and that code is given here: http://www.mathworks.com/matlabcentral/fileexchange/28164-color-frequency-image It has interesting visualizations.
In addition, see my File Exchange http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862, particularly the delta E one where it finds colors similar to the color you specify.