Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Area of specific pixel color?

Subject: Area of specific pixel color?

From: Allen

Date: 21 Mar, 2008 04:51:21

Message: 1 of 11

Hi guys,

Quick Q version:
How does one determine the fractional pixel area of a color. i.e., if
I highlight grains one color, how can I determine the area of the
colored portion in pixels?

Longer Q version:
I'm doing something complex (finding the surface area of a tilted
segment of an image)... but I think it can be done very simply.
I have a method to highlight only the area of interest, and now I want
to determine the area which is highlighted. I'm assuming this will be
in pixels x pixels of the image, but can convert this easily to
real-space (as well as use the cosine of the tilt to give me the real area).
So, is there a good way to get image information about areas
highlighted with specific colors etc.? If I painted one section of the
image all red, for instance, could I determine the pixel x pixel area?

Thanks a ton for your help and thoughts, everyone!! :)
-Allen

ps- this is an attempt to measure the surface-area of facets in a grown
crystal. The data is taken from an AFM, and I can highlight them based
on the surface-gradient information. The resulting images in plan-view
will be flat (forshortened areas etc.)

Subject: Area of specific pixel color?

From: carlos

Date: 21 Mar, 2008 05:30:04

Message: 2 of 11

Hi,

i think "regionprops" function is the solution to your
problem, in fact, with this function you can calculate a lot
more properties of your regions: area, centroid, perimeter,
euler number, etc.
the only condition is that you have first an segmented image
of course, but i think you have solved this.


Regards


Allen <ajhalldeleteme@gmail.com> wrote in message
<frvesa$ol9$1@news.ks.uiuc.edu>...
> Hi guys,
>
> Quick Q version:
> How does one determine the fractional pixel area of a
color. i.e., if
> I highlight grains one color, how can I determine the area
of the
> colored portion in pixels?
>
> Longer Q version:
> I'm doing something complex (finding the surface area of
a tilted
> segment of an image)... but I think it can be done very
simply.
> I have a method to highlight only the area of interest,
and now I want
> to determine the area which is highlighted. I'm assuming
this will be
> in pixels x pixels of the image, but can convert this
easily to
> real-space (as well as use the cosine of the tilt to give
me the real area).
> So, is there a good way to get image information about areas
> highlighted with specific colors etc.? If I painted one
section of the
> image all red, for instance, could I determine the pixel x
pixel area?
>
> Thanks a ton for your help and thoughts, everyone!! :)
> -Allen
>
> ps- this is an attempt to measure the surface-area of
facets in a grown
> crystal. The data is taken from an AFM, and I can
highlight them based
> on the surface-gradient information. The resulting images
in plan-view
> will be flat (forshortened areas etc.)

Subject: Area of specific pixel color?

From: Allen

Date: 21 Mar, 2008 06:03:00

Message: 3 of 11

carlos wrote:
> Hi,
>
> i think "regionprops" function is the solution to your
> problem, in fact, with this function you can calculate a lot
> more properties of your regions: area, centroid, perimeter,
> euler number, etc.
> the only condition is that you have first an segmented image
> of course, but i think you have solved this.
>
>
> Regards

Hi, Carlos!

Ha! Very interesting function! I have some reading to do (that's a big
function!).

Thank you for your response and quick help! I'll definitely post on
here if I have any success. :)

My very best!
-Allen

Subject: Area of specific pixel color?

From: Vihang Patil

Date: 21 Mar, 2008 06:44:02

Message: 4 of 11

Allen <ajhalldeleteme@gmail.com> wrote in message
<frvesa$ol9$1@news.ks.uiuc.edu>...
> Hi guys,
>
> Quick Q version:
> How does one determine the fractional pixel area
of a color. i.e., if
> I highlight grains one color, how can I determine the
area of the
> colored portion in pixels?
>
> Longer Q version:
> I'm doing something complex (finding the surface
area of a tilted
> segment of an image)... but I think it can be done very
simply.
> I have a method to highlight only the area of
interest, and now I want
> to determine the area which is highlighted. I'm
assuming this will be
> in pixels x pixels of the image, but can convert this
easily to
> real-space (as well as use the cosine of the tilt to
give me the real area).
> So, is there a good way to get image information
about areas
> highlighted with specific colors etc.? If I painted one
section of the
> image all red, for instance, could I determine the pixel
x pixel area?
>
> Thanks a ton for your help and thoughts, everyone!! :)
> -Allen
>
> ps- this is an attempt to measure the surface-area of
facets in a grown
> crystal. The data is taken from an AFM, and I can
highlight them based
> on the surface-gradient information. The resulting
images in plan-view
> will be flat (forshortened areas etc.)


Hello Allen
If you could highlight the area of interest, then it means
that you know that threshold. So you could easily convert
this RGB image to grayscale and then binary B/W image. And
then use bwlabel() and regionprops() followed by it..

ex:
RGB = imread('Your Image');
gray = rgb2gray(RGB);
bw = im2bw(gray, <computed threshold>);
L = bwlabel(bw);
stats = regionprops(L,'All');
area = [stats.Area];

HTH
Vihang

Subject: Area of specific pixel color?

From: ImageAnalyst

Date: 21 Mar, 2008 17:46:09

Message: 5 of 11

On Mar 21, 12:51=A0am, Allen <ajhalldelet...@gmail.com> wrote:
> Hi guys,
>
> Quick Q version:
> =A0 =A0 =A0 =A0 How does one determine the fractional pixel area of a colo=
r. =A0i.e., if
> I highlight grains one color, how can I determine the area of the
> colored portion in pixels?
>
> Longer Q version:
> =A0 =A0 =A0 =A0 I'm doing something complex (finding the surface area of a=
 tilted
> segment of an image)... but I think it can be done very simply.
> =A0 =A0 =A0 =A0 I have a method to highlight only the area of interest, an=
d now I want
> to determine the area which is highlighted. =A0I'm assuming this will be
> in pixels x pixels of the image, but can convert this easily to
> real-space (as well as use the cosine of the tilt to give me the real area=
).
> =A0 =A0 =A0 =A0 So, is there a good way to get image information about are=
as
> highlighted with specific colors etc.? =A0If I painted one section of the
> image all red, for instance, could I determine the pixel x pixel area?
>
> Thanks a ton for your help and thoughts, everyone!! =A0:)
> -Allen
>
> ps- this is an attempt to measure the surface-area of facets in a grown
> crystal. =A0The data is taken from an AFM, and I can highlight them based
> on the surface-gradient information. =A0The resulting images in plan-view
> will be flat (forshortened areas etc.)

---------------------------------------------------------------------
Allen:
Rather than manually paint the image you cold also try the roipoly
function, This will let you outline an area. Then convert the area
to a binary image and call bwlabel and then regionprops.

Alternatively you could paint a color on the image (maybe by making a
modification to freehanddraw macro on the user file exchange), and
then threshold each color band, something like
paintcolor =3D [1 1 1];
paintedRegion =3D imageArray(:,:,1) =3D=3D paintcolor(1) && imageArray(:,:,
2) =3D=3D paintcolor(2) && imageArray(:,:,3) =3D=3D paintcolor(3)
areaOfPaintedRegion =3D sum(sum(paintedRegion));
That's the area of the projected region. If it's tilted away from the
screen by an angle theta, then to get then you need to divide by the
cosine of the angle to get the actual area as if it weren't tilted.
actualArea =3D areaOfPaintedRegion / cos(theta)
and if the angle is not a single angle, but a more general tilt, it
gets more complicated.

You might have to take care about whether the image and/or the paint
color is to be normalized to the 0-1 range or if it can stay in the
0-255 range - that's always a watchout in MATLAB.
Regards,
ImageAnalyst

Subject: Area of specific pixel color?

From: Allen

Date: 22 Mar, 2008 18:55:48

Message: 6 of 11

Hi, Vihang and ImageAnalyst!

Thank you guys a ton for more details about roipoly and region props! I
really appreciate it. I'll be working more on this code this afternoon.
  So, the pointers are greatly appreciated. :)

Thanks so much for the help!
-Allen

--
------
a.s.p. Brother of the Briar
May 2001

Subject: Area of specific pixel color?

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 22 Mar, 2008 20:43:22

Message: 7 of 11

In article <frvesa$ol9$1@news.ks.uiuc.edu>,
Allen <ajhalldeleteme@gmail.com> wrote:

>Quick Q version:
> How does one determine the fractional pixel area of a color. i.e., if
>I highlight grains one color, how can I determine the area of the
>colored portion in pixels?

>ps- this is an attempt to measure the surface-area of facets in a grown
>crystal. The data is taken from an AFM, and I can highlight them based
>on the surface-gradient information. The resulting images in plan-view
>will be flat (forshortened areas etc.)

I am concerned about the foreshortened areas. I see that there
are corrections for it posted ("divide by the cosine of the angle")
but there will be angles for which there would be considerable
error inherent in that process. In your particular application,
would it make sense to produce an error estimate as part of
the output? For example "The boundary that would have produced
one less pixel coloured would have been Xn and the boundary
that would have produced one more pixel coloured would have been Xf
so the true area is between Xn/cos(angle) and Xf/cos(angle)" ??

--
amazon.com's top 8 books about "walter" are Kotzwinkle/ Gundy/ Colman's
"Walter the Farting Dog"

Subject: Area of specific pixel color?

From: Allen

Date: 25 Mar, 2008 01:48:05

Message: 8 of 11

Hi Walter,

You're very right to be concerned about the foreshortening. Basically,
due to the projection of the surface onto the view-plane (since I'm
looking down the z-axis), the areas will be altered into their
projection. If the selection algorithm is exactly perfect, then I'll
get exactly the area of desire, and the foreshortening will not affect
the results. However, if there is some error in the selection process,
you're absolutely correct.

I determine bounds for the selection angles (surface normals), and this
highlights the regions of interest for me. These bounds are the perfect
way to express your error as you suggested. I'll definitely implement
that in my work!

Thanks for your thoughts, Walter! I appreciate them! :)
-Allen

Walter Roberson wrote:
> I am concerned about the foreshortened areas. I see that there
> are corrections for it posted ("divide by the cosine of the angle")
> but there will be angles for which there would be considerable
> error inherent in that process. In your particular application,
> would it make sense to produce an error estimate as part of
> the output? For example "The boundary that would have produced
> one less pixel coloured would have been Xn and the boundary
> that would have produced one more pixel coloured would have been Xf
> so the true area is between Xn/cos(angle) and Xf/cos(angle)" ??

Subject: Area of specific pixel color?

From: Dagher Dagher

Date: 28 Mar, 2008 19:26:02

Message: 9 of 11

hello allen,

i've been searching over the net to calculate in a picture
the area covered by a certain color, and i found ur post,
since i'm not a matlab expert (only done some image
processing) i wonder if you can help solve the issue,

Thank You
bobby

Subject: Area of specific pixel color?

From: ImageAnalyst

Date: 29 Mar, 2008 00:57:20

Message: 10 of 11

On Mar 28, 3:26=A0pm, "Dagher Dagher" <bobid...@hotmail.com> wrote:
> hello allen,
>
> i've been searching over the net to calculate in a picture
> the area covered by a certain color, and i found ur post,
> since i'm not a matlab expert (only done some image
> processing) i wonder if you can help solve the issue,
>
> Thank You
> bobby

bobby/Dagher Dagher
I don't think Allen is doing what you want to do. He's painting over
some area with a particular color and then finding that area. That
would work for you only if the color you were looking for was a
certain very specific RGB triplet, which I doubt it is. You probably
want to search for some range of colors in your image, not one very
specific color such as (100, 150, 200). To do that, the usual
starting place is to convert your image into HSV, HSL, or HSI color
space (see http://www.easyrgb.com/math.html), and then set up some
threshold ranges that will apply to each of the H band, the S (also
called C for Chroma) band, and the Intensity or Value band. Then you
simply AND all the thresholded images for each color space together
and call bwlabel() and regionprops(). It can get more complicated
than that but that's the starter version.
Regards,
ImageAnalyst

Subject: Area of specific pixel color?

From: Dagher Dagher

Date: 29 Mar, 2008 08:25:03

Message: 11 of 11

Thank You for all the information imageanalyst i'll check
out the link you've given and post, hope i'll be able to do
it
thanx again
bobby

Tags for this Thread

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.

Contact us