Thread Subject: Color recognition

Subject: Color recognition

From: omo_ewa

Date: 23 Nov, 2009 18:53:44

Message: 1 of 7

Hello all,

I am new to using matlab for any serious coding. However, I will like
to be able to recognize specific colors (green, red, blue, black,
etc.) in a scene of different color blocks of different colors. So, a
sample will be to recognize a yellow-triangle block in a scene of
yellow-triangular block, blue-square block, red-circular block. Thank
you for your help!

omo_ewa

Subject: Color recognition

From: jrenfree

Date: 23 Nov, 2009 19:27:31

Message: 2 of 7

On Nov 23, 10:53 am, omo_ewa <morenike.aj...@gmail.com> wrote:
> Hello all,
>
> I am new to using matlab for any serious coding. However, I will like
> to be able to recognize specific colors (green, red, blue, black,
> etc.) in a scene of different color blocks of different colors. So, a
> sample will be to recognize a yellow-triangle block in a scene of
> yellow-triangular block, blue-square block, red-circular block. Thank
> you for your help!
>
> omo_ewa

Generally, colors are described by a combination of their RGB (red,
green, and blue) values. These values range between 0 and 255. In
Matlab, an image will typically be given by an MxNx3 matrix that gives
the RGB values for each pixel of an image. You will need to determine
the RGB ranges that coincide with the colors you want to detect and
use some sort of thresholding in your image matrix to pick those
colors out.

There are other ways to do it, such as converting the image to HSV
(hue, saturation, and value) values instead of RGB.

Subject: Color recognition

From: omo_ewa

Date: 23 Nov, 2009 19:41:34

Message: 3 of 7

On Nov 23, 2:27 pm, jrenfree <jrenf...@gmail.com> wrote:
> On Nov 23, 10:53 am, omo_ewa <morenike.aj...@gmail.com> wrote:
>
> > Hello all,
>
> > I am new to using matlab for any serious coding. However, I will like
> > to be able to recognize specific colors (green, red, blue, black,
> > etc.) in a scene of differentcolorblocks of different colors. So, a
> > sample will be to recognize a yellow-triangle block in a scene of
> > yellow-triangular block, blue-square block, red-circular block. Thank
> > you for your help!
>
> > omo_ewa
>
> Generally, colors are described by a combination of their RGB (red,
> green, and blue) values.  These values range between 0 and 255.  In
> Matlab, an image will typically be given by an MxNx3 matrix that gives
> the RGB values for each pixel of an image.  You will need to determine
> the RGB ranges that coincide with the colors you want to detect and
> use some sort of thresholding in your image matrix to pick those
> colors out.
>
> There are other ways to do it, such as converting the image to HSV
> (hue, saturation, and value) values instead of RGB.

Hello jrenfree,

Thank you for your help. It will be useful to me later on. But, do you
have a sample that shows even how to start this. I know only so far
how to read an image in to matlab. How do i code something that now
checks for green in an image? What i am saying is that I don't know
what to write. I have been checking online for a sample code i can
work with, but i have not found any yet. I believe my problem is more
basic.

Thank you,

omo_ewa

Subject: Color recognition

From: jrenfree

Date: 23 Nov, 2009 19:48:44

Message: 4 of 7

On Nov 23, 11:41 am, omo_ewa <morenike.aj...@gmail.com> wrote:
> On Nov 23, 2:27 pm, jrenfree <jrenf...@gmail.com> wrote:
>
>
>
> > On Nov 23, 10:53 am, omo_ewa <morenike.aj...@gmail.com> wrote:
>
> > > Hello all,
>
> > > I am new to using matlab for any serious coding. However, I will like
> > > to be able to recognize specific colors (green, red, blue, black,
> > > etc.) in a scene of differentcolorblocks of different colors. So, a
> > > sample will be to recognize a yellow-triangle block in a scene of
> > > yellow-triangular block, blue-square block, red-circular block. Thank
> > > you for your help!
>
> > > omo_ewa
>
> > Generally, colors are described by a combination of their RGB (red,
> > green, and blue) values.  These values range between 0 and 255.  In
> > Matlab, an image will typically be given by an MxNx3 matrix that gives
> > the RGB values for each pixel of an image.  You will need to determine
> > the RGB ranges that coincide with the colors you want to detect and
> > use some sort of thresholding in your image matrix to pick those
> > colors out.
>
> > There are other ways to do it, such as converting the image to HSV
> > (hue, saturation, and value) values instead of RGB.
>
> Hello jrenfree,
>
> Thank you for your help. It will be useful to me later on. But, do you
> have a sample that shows even how to start this. I know only so far
> how to read an image in to matlab. How do i code something that now
> checks for green in an image? What i am saying is that I don't know
> what to write. I have been checking online for a sample code i can
> work with, but i have not found any yet. I believe my problem is more
> basic.
>
> Thank you,
>
> omo_ewa

Ok, so let's say you read in an image:

A = imread('test.jpg');

The variable A should be some MxNx3 matrix. Now if we want to pick
out all the green values from that matrix, we need to know the RGB
values for green. Solid green would contain no red (R=0), full green
(G=255), and no blue (B=0). We could pick those out by:

idx = A(:,:,1) == 0 & A(:,:,2) == 255 & A(:,:,3) == 0;

This is essentially saying to look for indices where the red color
values equal 0, the green color values equal 255, and the blue color
values equal 0. The result, idx, is a logical matrix that indexes
into A to pick out only the pixels that are green.

Subject: Color recognition

From: omo_ewa

Date: 23 Nov, 2009 19:50:59

Message: 5 of 7

On Nov 23, 2:48 pm, jrenfree <jrenf...@gmail.com> wrote:
> On Nov 23, 11:41 am, omo_ewa <morenike.aj...@gmail.com> wrote:
>
>
>
> > On Nov 23, 2:27 pm, jrenfree <jrenf...@gmail.com> wrote:
>
> > > On Nov 23, 10:53 am, omo_ewa <morenike.aj...@gmail.com> wrote:
>
> > > > Hello all,
>
> > > > I am new to using matlab for any serious coding. However, I will like
> > > > to be able to recognize specific colors (green, red, blue, black,
> > > > etc.) in a scene of differentcolorblocks of different colors. So, a
> > > > sample will be to recognize a yellow-triangle block in a scene of
> > > > yellow-triangular block, blue-square block, red-circular block. Thank
> > > > you for your help!
>
> > > > omo_ewa
>
> > > Generally, colors are described by a combination of their RGB (red,
> > > green, and blue) values.  These values range between 0 and 255.  In
> > > Matlab, an image will typically be given by an MxNx3 matrix that gives
> > > the RGB values for each pixel of an image.  You will need to determine
> > > the RGB ranges that coincide with the colors you want to detect and
> > > use some sort of thresholding in your image matrix to pick those
> > > colors out.
>
> > > There are other ways to do it, such as converting the image to HSV
> > > (hue, saturation, and value) values instead of RGB.
>
> > Hello jrenfree,
>
> > Thank you for your help. It will be useful to me later on. But, do you
> > have a sample that shows even how to start this. I know only so far
> > how to read an image in to matlab. How do i code something that now
> > checks for green in an image? What i am saying is that I don't know
> > what to write. I have been checking online for a sample code i can
> > work with, but i have not found any yet. I believe my problem is more
> > basic.
>
> > Thank you,
>
> > omo_ewa
>
> Ok, so let's say you read in an image:
>
> A = imread('test.jpg');
>
> The variable A should be some MxNx3 matrix.  Now if we want to pick
> out all the green values from that matrix, we need to know the RGB
> values for green.  Solid green would contain no red (R=0), full green
> (G=255), and no blue (B=0).  We could pick those out by:
>
> idx = A(:,:,1) == 0 & A(:,:,2) == 255 & A(:,:,3) == 0;
>
> This is essentially saying to look for indices where the red color
> values equal 0, the green color values equal 255, and the blue color
> values equal 0.  The result, idx, is a logical matrix that indexes
> into A to pick out only the pixels that are green.

Thank you very much, I'll start from there.

omo_ewa

Subject: Color recognition

From: jrenfree

Date: 23 Nov, 2009 20:05:14

Message: 6 of 7

On Nov 23, 11:50 am, omo_ewa <morenike.aj...@gmail.com> wrote:
> On Nov 23, 2:48 pm, jrenfree <jrenf...@gmail.com> wrote:
>
>
>
> > On Nov 23, 11:41 am, omo_ewa <morenike.aj...@gmail.com> wrote:
>
> > > On Nov 23, 2:27 pm, jrenfree <jrenf...@gmail.com> wrote:
>
> > > > On Nov 23, 10:53 am, omo_ewa <morenike.aj...@gmail.com> wrote:
>
> > > > > Hello all,
>
> > > > > I am new to using matlab for any serious coding. However, I will like
> > > > > to be able to recognize specific colors (green, red, blue, black,
> > > > > etc.) in a scene of differentcolorblocks of different colors. So, a
> > > > > sample will be to recognize a yellow-triangle block in a scene of
> > > > > yellow-triangular block, blue-square block, red-circular block. Thank
> > > > > you for your help!
>
> > > > > omo_ewa
>
> > > > Generally, colors are described by a combination of their RGB (red,
> > > > green, and blue) values.  These values range between 0 and 255.  In
> > > > Matlab, an image will typically be given by an MxNx3 matrix that gives
> > > > the RGB values for each pixel of an image.  You will need to determine
> > > > the RGB ranges that coincide with the colors you want to detect and
> > > > use some sort of thresholding in your image matrix to pick those
> > > > colors out.
>
> > > > There are other ways to do it, such as converting the image to HSV
> > > > (hue, saturation, and value) values instead of RGB.
>
> > > Hello jrenfree,
>
> > > Thank you for your help. It will be useful to me later on. But, do you
> > > have a sample that shows even how to start this. I know only so far
> > > how to read an image in to matlab. How do i code something that now
> > > checks for green in an image? What i am saying is that I don't know
> > > what to write. I have been checking online for a sample code i can
> > > work with, but i have not found any yet. I believe my problem is more
> > > basic.
>
> > > Thank you,
>
> > > omo_ewa
>
> > Ok, so let's say you read in an image:
>
> > A = imread('test.jpg');
>
> > The variable A should be some MxNx3 matrix.  Now if we want to pick
> > out all the green values from that matrix, we need to know the RGB
> > values for green.  Solid green would contain no red (R=0), full green
> > (G=255), and no blue (B=0).  We could pick those out by:
>
> > idx = A(:,:,1) == 0 & A(:,:,2) == 255 & A(:,:,3) == 0;
>
> > This is essentially saying to look for indices where the red color
> > values equal 0, the green color values equal 255, and the blue color
> > values equal 0.  The result, idx, is a logical matrix that indexes
> > into A to pick out only the pixels that are green.
>
> Thank you very much, I'll start from there.
>
> omo_ewa

I should add that if you wanted to look at just those pixels, you
could do:

image(A(idx))

Subject: Color recognition

From: ImageAnalyst

Date: 23 Nov, 2009 20:41:37

Message: 7 of 7

You could use the known RB values and do some thresholding and ANDing
(like jrenfree showed you) - pretty simple but not too robust. See my
demo:
http://www.mathworks.com/matlabcentral/fileexchange/25157
and just adapt it to three color planes instead of one.

Another method is to use k-means clustering like in this MATLAB demo:
http://www.mathworks.com/products/demos/image/color_seg_k/ipexhistology.html

Like jrenfree said, you really need to say what you mean by a certain
color because there are a possible 16.7 million of them in a 24 bit
rgb image and of course we don't have 16.7 million names. So each
name, such as "blue" must cover a RANGE of colors and you need to
define this. It also depends on the image. If you take a picture of
a very simple image it might be clear what blue, red, and black are,
and everybody agrees on them, but if it's more complicated, say a Van
Gogh Painting, it's not so clear what the color names mean because
there is a CONTINUUM of colors, not a small, discrete set of colors.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com