How do I define a color?

50 views (last 30 days)
msij
msij on 21 Aug 2014
Answered: Image Analyst on 25 Aug 2014
Hi,
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);
color=imLabel(floor(y),floor(x),:);
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.
Thanks.
  2 Comments
Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh on 21 Aug 2014
Edited: Salaheddin Hosseinzadeh on 21 Aug 2014
Hi
What do you mean you want a permanent color?!
Just define color as
color = [.5 1 1];
if you don't want to pick a point!!!
I don't think if I understood your question!
How about explaining a bit more? Tell us what's the application of the "color" vector? Where you gonna use that? Possibly in a plot command?!
Tnx
msij
msij on 22 Aug 2014
Sorry if I wasn't clear.
I have an image with several different colour components. Basically, I am trying to end up with just one of the colours and count them.
From the colour segmentation tutorial they used the ginput command so that I can physically pick the colour on the image, however I want that step to be automatic meaning that the colour I want segmented is already define.
Does that make more sense?
I want to be able to click run, the code will run and pick and count the spots of that colour without me having to click on it.

Sign in to comment.

Accepted Answer

Salaheddin Hosseinzadeh
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.
mypic(x,y,:)
Hope this can answer your question, sorry I got back to you a bit late.
Good Luck!

More Answers (1)

Image Analyst
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.

Community Treasure Hunt

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

Start Hunting!