COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### how to calculate area of a grayscale image?

• Follow

```how to calculate area of a grayscale image?
```
 0

```saurabh pandey wrote:
> how to calculate area of a grayscale image?

Multiply the height by the width ?

Other than that, consider thresholding the image to binary and using
bwarea()
```
 0

```Walter Roberson <roberson@hushmail.com> wrote in message <1GlOn.41508\$Ak3.35826@newsfe16.iad>...
> saurabh pandey wrote:
> > how to calculate area of a grayscale image?
>
> Multiply the height by the width ?
>
> Other than that, consider thresholding the image to binary and using
> bwarea()

actually sir i made a code for segmentation of images using morphology.now i want to anaylse quantitatively that by how much percent the image is segmented?
if i convert the image to binary,then i think it is not a best way.
is there any method to analyse the segmentation?
```
 0

```On Jun 5, 2:19=A0am, "saurabh pandey" <sathiya_saur...@yahoo.co.in>
wrote:
> actually sir i made a code for segmentation of images using morphology.no=
w i want to anaylse quantitatively that by how much percent the image is se=
gmented?
> if i convert the image to binary,then i think it is not a best way.
> is there any method to analyse the segmentation?

----------------------------------------------------------------------
So you did some grayscale morphology on a grayscale image and now you
have a grayscale image.  You didn't convert the image to binary so
exactly which of the gray pixels do you consider to be the "segmented"
ones and which are unsegmented?

I don't think you have a segmentation.  You just have a grayscale
image so all you can do now are things like calculating the mean of
the whole image, it's standard deviation, centroid, and things like
that.  You can't calculate the area of anything because you haven't
defined any object yet in the image.  All you have is the whole image
so like Walter said you can only calculate the area of the whole image.
```
 0

```ImageAnalyst <imageanalyst@mailinator.com> wrote in message <691b8b64-1fde-49b3-9626-1c0d955c953c@a30g2000yqn.googlegroups.com>...
> On Jun 5, 2:19 am, "saurabh pandey" <sathiya_saur...@yahoo.co.in>
> wrote:
> > actually sir i made a code for segmentation of images using morphology.now i want to anaylse quantitatively that by how much percent the image is segmented?
> > if i convert the image to binary,then i think it is not a best way.
> > is there any method to analyse the segmentation?
>
> ----------------------------------------------------------------------
> So you did some grayscale morphology on a grayscale image and now you
> have a grayscale image.  You didn't convert the image to binary so
> exactly which of the gray pixels do you consider to be the "segmented"
> ones and which are unsegmented?
>
> I don't think you have a segmentation.  You just have a grayscale
> image so all you can do now are things like calculating the mean of
> the whole image, it's standard deviation, centroid, and things like
> that.  You can't calculate the area of anything because you haven't
> defined any object yet in the image.  All you have is the whole image
> so like Walter said you can only calculate the area of the whole image.

no sir actually i have converted the image to binary image.and then i applied morphological image processing on it and segment the area of interest which is also a binary image(black and white).now i find the perimeter of my segmented image and overlay(downloaded from steve blog) that perimeter to my grayscale image.now the area of segmented image can be calculated by the image for which i calculate the perimeter using bwarea, because it is a binary image.but how can i calculate the area for gray image so dividing the area of segmented image by area of gray image i could found the percent of segmentation, i.e.,by how much percent the original gray image is segmented.
can you please tell me what is meant by area in gray and binary image?

```
 0

```bwarea calculates area only on the entire image, so it's "accurate" if
you have only one objects.  Otherwise, if you have multiple objects,
you should use the area measurement given by regionprops.  And bwarea
does NOT calculate perimeter as you said.  Also note that bwarea uses
a special definition of area - it's not simply counting pixels.

What you're calling "percent of segmentation" is most often called
"area fraction" and it is simply the segmented area(s) divided by the
total image area (like you said).  The total image area is the same
for the gray image and its binary representation - basically it's the
total number of pixels in the image.

I'm not sure how to answer "can you please tell me what is meant by
area in gray and binary image?"  Area is the number of pixels, or real
world units if you have a spatial calibration.  Once you know what
pixels to include, be it a segmented portion of your image or the
entire image, the area is simply the number of pixels and it makes no
difference whatsoever whether those pixels are in a gray image or a
binary image - you're just counting pixels and you don' care whether
they are 0 & 1 or range from 0-255.
```
 0

```ImageAnalyst <imageanalyst@mailinator.com> wrote in message <db46ad54-5b34-458d-aa55-7635cc45b207@y11g2000yqm.googlegroups.com>...
> bwarea calculates area only on the entire image, so it's "accurate" if
> you have only one objects.  Otherwise, if you have multiple objects,
> you should use the area measurement given by regionprops.  And bwarea
> does NOT calculate perimeter as you said.  Also note that bwarea uses
> a special definition of area - it's not simply counting pixels.
>
> What you're calling "percent of segmentation" is most often called
> "area fraction" and it is simply the segmented area(s) divided by the
> total image area (like you said).  The total image area is the same
> for the gray image and its binary representation - basically it's the
> total number of pixels in the image.
>
> I'm not sure how to answer "can you please tell me what is meant by
> area in gray and binary image?"  Area is the number of pixels, or real
> world units if you have a spatial calibration.  Once you know what
> pixels to include, be it a segmented portion of your image or the
> entire image, the area is simply the number of pixels and it makes no
> difference whatsoever whether those pixels are in a gray image or a
> binary image - you're just counting pixels and you don' care whether
> they are 0 & 1 or range from 0-255.

please tell me how the regionprop command can be used to calculate the area of image '2.jpg' uploaded to picasaweb.i am confused too much.
```
 0

```The area of *what*?  Those are very complicated images with lots of
weird looking, ill defined  things in them.  It's not really clear
what is to be measured, and it's not clear what you're even asking.
If we go by exactly what you ask -- "calculate the area of image" --
then we'd just say that the area is the number of pixels in your image
(converted to real world units such as mm^2 if you wish).  I'd have a
tough time segmenting anything out of those images so if you just need
the area of some subset of objects in there, I'd use your expert human
knowledge and manually outline them.  Heck, even with manually drawing
them I think there would be lots of judgement calls as to where to
draw the lines.  There is nothing distinct enough in the images, and
the images aren't even similar enough, to waste time trying to develop
an automatic algorithm for them (unless that's the whole point of your
dissertation work).

I don't have any code to send you, but you said that you already has
code that segmented something out of those images.  I'd be interested
in seeing that code because I have no idea what it could possibly find.
```
 0

```ImageAnalyst <imageanalyst@mailinator.com> wrote in message <62e96cdf-2c99-4939-8e14-fd943c8f8e14@c33g2000yqm.googlegroups.com>...
> The area of *what*?  Those are very complicated images with lots of
> weird looking, ill defined  things in them.  It's not really clear
> what is to be measured, and it's not clear what you're even asking.
> If we go by exactly what you ask -- "calculate the area of image" --
> then we'd just say that the area is the number of pixels in your image
> (converted to real world units such as mm^2 if you wish).  I'd have a
> tough time segmenting anything out of those images so if you just need
> the area of some subset of objects in there, I'd use your expert human
> knowledge and manually outline them.  Heck, even with manually drawing
> them I think there would be lots of judgement calls as to where to
> draw the lines.  There is nothing distinct enough in the images, and
> the images aren't even similar enough, to waste time trying to develop
> an automatic algorithm for them (unless that's the whole point of your
> dissertation work).
>
> I don't have any code to send you, but you said that you already has
> code that segmented something out of those images.  I'd be interested
> in seeing that code because I have no idea what it could possibly find.

thank you very much for your support.
as you told i calculate the area of image using regionprops by converting it to binary by im2bw.now i find edges by using canny and sobel operator which are also binary images.can i calculate area for that image? is this perfect for analysis purposes?
```
 0

```"saurabh pandey" <sathiya_saurabh@yahoo.co.in> wrote in message <i0c2gd\$q9o\$1@fred.mathworks.com>...
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message <62e96cdf-2c99-4939-8e14-fd943c8f8e14@c33g2000yqm.googlegroups.com>...
> > The area of *what*?  Those are very complicated images with lots of
> > weird looking, ill defined  things in them.  It's not really clear
> > what is to be measured, and it's not clear what you're even asking.
> > If we go by exactly what you ask -- "calculate the area of image" --
> > then we'd just say that the area is the number of pixels in your image
> > (converted to real world units such as mm^2 if you wish).  I'd have a
> > tough time segmenting anything out of those images so if you just need
> > the area of some subset of objects in there, I'd use your expert human
> > knowledge and manually outline them.  Heck, even with manually drawing
> > them I think there would be lots of judgement calls as to where to
> > draw the lines.  There is nothing distinct enough in the images, and
> > the images aren't even similar enough, to waste time trying to develop
> > an automatic algorithm for them (unless that's the whole point of your
> > dissertation work).
> >
> > I don't have any code to send you, but you said that you already has
> > code that segmented something out of those images.  I'd be interested
> > in seeing that code because I have no idea what it could possibly find.
>
>
>
> thank you very much for your support.
> as you told i calculate the area of image using regionprops by converting it to binary by im2bw.now i find edges by using canny and sobel operator which are also binary images.can i calculate area for that image? is this perfect for analysis purposes?

Your description is confusing but let's see if this is what you want.. From what I can gather you have an initial greyscale image. You use an edge detection algorithm and then morphological operations to estimate a segmented image. This segmented image will now be a black and white image, with the white pixels representing the "image" and the black pixels representing the "background"...

You now wish to determine the ratio of the segmented image to the original image as a measure of the fraction of image segmented from the original..

I would assume something like this:
*********************************************************************
% read in the original image

% perform edge detection (example threshold)
edgeDetect = edge(greyIm,'Canny',0.4);

% perform morphology
diskEnt = strel('disk',10);
morphIm = imclose(edgeDetect,diskEnt);
segmentedIm = imfill(morphIm,'holes');
*********************************************************************

At this stage we have your 'segmented' image (segmentedIm), which is black and white. Now it gets complicated. It is important to define what you mean by the term 'area'. I noticed you were using the function bwarea(). This function estimates the area of a given pixel due to it's 2x2 neighbourhood.

If it is a literal area ratio you are looking for (as in the amount of space occupied), then I would suggest using the number of pixels for the measure of your area - as this is common to both your segmented image and the original greyscale image. The amount of white pixels (remember the white pixels are your segmented image and therefore the amount of white pixels represent the amount of pixels in the segmented image) can easily be determined with:

segArea = length(find(segmentedIm == 1));

Now you want to compare this to the amount of pixels within your original greyscale image. As mentioned previously, this is simply the result of the product of the width and height dimensions of your image, or:

origArea = size(greyIm,1)*size(greyIm,2);

Now your ratio of extracted pixels can easily be determined:

areaRatio = segArea/origArea;
```
 0

```"Christopher " <christopher.badman@au.saabgroup.com> wrote in message <i0c46n\$fdt\$1@fred.mathworks.com>...
> "saurabh pandey" <sathiya_saurabh@yahoo.co.in> wrote in message <i0c2gd\$q9o\$1@fred.mathworks.com>...
> > ImageAnalyst <imageanalyst@mailinator.com> wrote in message <62e96cdf-2c99-4939-8e14-fd943c8f8e14@c33g2000yqm.googlegroups.com>...
> > > The area of *what*?  Those are very complicated images with lots of
> > > weird looking, ill defined  things in them.  It's not really clear
> > > what is to be measured, and it's not clear what you're even asking.
> > > If we go by exactly what you ask -- "calculate the area of image" --
> > > then we'd just say that the area is the number of pixels in your image
> > > (converted to real world units such as mm^2 if you wish).  I'd have a
> > > tough time segmenting anything out of those images so if you just need
> > > the area of some subset of objects in there, I'd use your expert human
> > > knowledge and manually outline them.  Heck, even with manually drawing
> > > them I think there would be lots of judgement calls as to where to
> > > draw the lines.  There is nothing distinct enough in the images, and
> > > the images aren't even similar enough, to waste time trying to develop
> > > an automatic algorithm for them (unless that's the whole point of your
> > > dissertation work).
> > >
> > > I don't have any code to send you, but you said that you already has
> > > code that segmented something out of those images.  I'd be interested
> > > in seeing that code because I have no idea what it could possibly find.
> >
> >
> >
> > thank you very much for your support.
> > as you told i calculate the area of image using regionprops by converting it to binary by im2bw.now i find edges by using canny and sobel operator which are also binary images.can i calculate area for that image? is this perfect for analysis purposes?
>
> Your description is confusing but let's see if this is what you want.. From what I can gather you have an initial greyscale image. You use an edge detection algorithm and then morphological operations to estimate a segmented image. This segmented image will now be a black and white image, with the white pixels representing the "image" and the black pixels representing the "background"...
>
> You now wish to determine the ratio of the segmented image to the original image as a measure of the fraction of image segmented from the original..
>
> I would assume something like this:
> *********************************************************************
> % read in the original image
>
> % perform edge detection (example threshold)
> edgeDetect = edge(greyIm,'Canny',0.4);
>
> % perform morphology
> diskEnt = strel('disk',10);
> morphIm = imclose(edgeDetect,diskEnt);
> segmentedIm = imfill(morphIm,'holes');
> *********************************************************************
>
> At this stage we have your 'segmented' image (segmentedIm), which is black and white. Now it gets complicated. It is important to define what you mean by the term 'area'. I noticed you were using the function bwarea(). This function estimates the area of a given pixel due to it's 2x2 neighbourhood.
>
> If it is a literal area ratio you are looking for (as in the amount of space occupied), then I would suggest using the number of pixels for the measure of your area - as this is common to both your segmented image and the original greyscale image. The amount of white pixels (remember the white pixels are your segmented image and therefore the amount of white pixels represent the amount of pixels in the segmented image) can easily be determined with:
>
> segArea = length(find(segmentedIm == 1));
>
> Now you want to compare this to the amount of pixels within your original greyscale image. As mentioned previously, this is simply the result of the product of the width and height dimensions of your image, or:
>
> origArea = size(greyIm,1)*size(greyIm,2);
>
> Now your ratio of extracted pixels can easily be determined:
>
> areaRatio = segArea/origArea;

sorry sir you didn't get me.actually i take a input image of say coins.png.apply some morphological operation by converting it to binary by using im2bw.now i get some segmented image,find its perimeter and then i overlay this perimeter on the original gray image,and find area of orginal image as well as segmented image by regionprops.
now again i want to compare my method with other methods like canny,sobel etc. so i find edge by canny edge detecter and overlay that image of edges on the original image.that is i found the segmented image by canny method.now i have the area of original image but i don't have the area of segmented image by canny.so i want to ask that can i find the area of that image containing edges by canny by using regionprops or bwarea or by code which you provided.
or can i use any other method for finding my code is better than canny,sobel
?
```
 0

```If you're using MATLAB's edge() function to do the Canny then you're
going to have to call imfill to get solid objects that you can call
bwlabel and regionprops on.  Post your code so we can offer more help.
```
 0

```ImageAnalyst <imageanalyst@mailinator.com> wrote in message <bac7103b-d01a-4dfa-b781-7535bc0a1ca8@s9g2000yqd.googlegroups.com>...
> If you're using MATLAB's edge() function to do the Canny then you're
> going to have to call imfill to get solid objects that you can call
> bwlabel and regionprops on.  Post your code so we can offer more help.

Thank you very much for your support.

imshow(i)
f=edge(i,'canny');
figure,imshow(f)
overlay1 = imoverlay(segout, f, [.988 0.309 0.082]);
figure,imshow(overlay1)

overlay is taken from "matlab central".Now i have the area of original image,and i want to calculate area of the segmented image by canny i.e., f=edge(i,'canny');,so that i could calculate the segmentation percentage.can i find the area by filling that image by using 'fill'
```
 0

```ImageAnalyst <imageanalyst@mailinator.com> wrote in message <bac7103b-d01a-4dfa-b781-7535bc0a1ca8@s9g2000yqd.googlegroups.com>...
> If you're using MATLAB's edge() function to do the Canny then you're
> going to have to call imfill to get solid objects that you can call
> bwlabel and regionprops on.  Post your code so we can offer more help.

Thank you very much for your support.

imshow(i)
f=edge(i,'canny');
figure,imshow(f)
overlay1 = imoverlay(segout, f, [.988 0.309 0.082]);
figure,imshow(overlay1)

overlay is taken from "matlab central".Now i have the area of original image,and i want to calculate area of the segmented image by canny i.e., f=edge(i,'canny');,so that i could calculate the segmentation percentage.can i find the area by filling that image by using 'fill'
```
 0

```"saurabh pandey" <sathiya_saurabh@yahoo.co.in> wrote in message <i116ee\$gfn\$1@fred.mathworks.com>...
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message <bac7103b-d01a-4dfa-b781-7535bc0a1ca8@s9g2000yqd.googlegroups.com>...
> > If you're using MATLAB's edge() function to do the Canny then you're
> > going to have to call imfill to get solid objects that you can call
> > bwlabel and regionprops on.  Post your code so we can offer more help.
>
>
>
>
> Thank you very much for your support.
>
> imshow(i)
> f=edge(i,'canny');
> figure,imshow(f)
> overlay1 = imoverlay(segout, f, [.988 0.309 0.082]);
> figure,imshow(overlay1)
>
> overlay is taken from "matlab central".Now i have the area of original image,and i want to calculate area of the segmented image by canny i.e., f=edge(i,'canny');,so that i could calculate the segmentation percentage.can i find the area by filling that image by using 'fill'
now for this particular image i can fill the holes by using imfill function,but if i have another images. it doesn't work so efficiently.how can i do this for cameraman image or any other image.
```
 0

```On Jul 7, 2:36=A0am, "saurabh pandey"
> now for this particular image i can fill the holes by using imfill functi=
on,but if i have another images. it doesn't work so efficiently.how can i d=
o this for cameraman image or any other image.
----------------------------------
You might have to close up the gaps in the edges with imclose().

```
 0

```ImageAnalyst <imageanalyst@mailinator.com> wrote in message <2904bdb7-d633-492c-9b1d-271251c80bb5@i28g2000yqa.googlegroups.com>...
> On Jul 7, 2:36 am, "saurabh pandey"
> > now for this particular image i can fill the holes by using imfill function,but if i have another images. it doesn't work so efficiently.how can i do this for cameraman image or any other image.
> ----------------------------------
> You might have to close up the gaps in the edges with imclose().

thank you very very much for your support.
```
 0

```"saurabh pandey" <sathiya_saurabh@yahoo.co.in> wrote in message <i1a70n\$jbl\$1@fred.mathworks.com>...
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message <2904bdb7-d633-492c-9b1d-271251c80bb5@i28g2000yqa.googlegroups.com>...
> > On Jul 7, 2:36 am, "saurabh pandey"
> > > now for this particular image i can fill the holes by using imfill function,but if i have another images. it doesn't work so efficiently.how can i do this for cameraman image or any other image.
> > ----------------------------------
> > You might have to close up the gaps in the edges with imclose().
>
> thank you very very much for your support.

hello sir
i want to select some particular objects in image to segment them. so i start using roipoly.it gives a mask.now how can i use this mask to perform my morphological segmentation in that selected portion(for example i take coins.png.i select 4 coins using roipoly.now how can i go for further morphological segmentation in that selected area)..
thank you very much..
```
 0

```"saurabh pandey" <sathiya_saurabh@yahoo.co.in> wrote in message <i1a9qt\$78i\$1@fred.mathworks.com>...
> "saurabh pandey" <sathiya_saurabh@yahoo.co.in> wrote in message <i1a70n\$jbl\$1@fred.mathworks.com>...
> > ImageAnalyst <imageanalyst@mailinator.com> wrote in message <2904bdb7-d633-492c-9b1d-271251c80bb5@i28g2000yqa.googlegroups.com>...
> > > On Jul 7, 2:36 am, "saurabh pandey"
> > > > now for this particular image i can fill the holes by using imfill function,but if i have another images. it doesn't work so efficiently.how can i do this for cameraman image or any other image.
> > > ----------------------------------
> > > You might have to close up the gaps in the edges with imclose().
> >
> > thank you very very much for your support.
>
>
>
>
> hello sir
> i want to select some particular objects in image to segment them. so i start using roipoly.it gives a mask.now how can i use this mask to perform my morphological segmentation in that selected portion(for example i take coins.png.i select 4 coins using roipoly.now how can i go for further morphological segmentation in that selected area)..
> thank you very much..

a hint:
- use the mask returned by ROIPOLY to carve out your objects...
- use BWLABEL to identify your objects...
- use REGIONPROPS to compute your objects' characteristics...

us
```
 0

```"saurabh pandey" <sathiya_saurabh@yahoo.co.in> wrote in message <i1a9qt\$78i\$1@fred.mathworks.com>...
> hello sir
> i want to select some particular objects in image to segment them. so i start using roipoly.it gives a mask.now how can i use this mask to perform my morphological segmentation in that selected portion(for example i take coins.png.i select 4 coins using roipoly.now how can i go for further morphological segmentation in that selected area)..
> thank you very much..
------------------------------------------------------------
Did you see my code:
http://www.mathworks.com/matlabcentral/fileexchange/25157-blobsdemo
It finds the coins, measures them all, and extracts each to a separate subimage.
Basically you can use roipoly() or (my preference) roipolyold() to get the user drawn polygon, then turn it into a mask with poly2mask().
Then multiply the mask by the image to zero out the stuff outside the mask (or inside).
Then just do the usual threshold (preceded by morphological operations if that is what you want to do), bwlabel(), and regionprops().
labeledImage = bwlabel(binaryImage, 8);     % Label each blob so we can make measurements of it
% Get all the blob properties.  Can only pass in originalImage in version R2008a and later.
blobMeasurements = regionprops(labeledImage, grayImage , 'all');
```
 0

```"Image Analyst" <imageanalyst@mailinator.com> wrote in message <i1ar7l\$nfj\$1@fred.mathworks.com>...
> "saurabh pandey" <sathiya_saurabh@yahoo.co.in> wrote in message <i1a9qt\$78i\$1@fred.mathworks.com>...
> > hello sir
> > i want to select some particular objects in image to segment them. so i start using roipoly.it gives a mask.now how can i use this mask to perform my morphological segmentation in that selected portion(for example i take coins.png.i select 4 coins using roipoly.now how can i go for further morphological segmentation in that selected area)..
> > thank you very much..
> ------------------------------------------------------------
> Did you see my code:
> http://www.mathworks.com/matlabcentral/fileexchange/25157-blobsdemo
> It finds the coins, measures them all, and extracts each to a separate subimage.
> Basically you can use roipoly() or (my preference) roipolyold() to get the user drawn polygon, then turn it into a mask with poly2mask().
> Then multiply the mask by the image to zero out the stuff outside the mask (or inside).
> Then just do the usual threshold (preceded by morphological operations if that is what you want to do), bwlabel(), and regionprops().
> binaryImage = maskedImage > thresholdValue;
> labeledImage = bwlabel(binaryImage, 8);     % Label each blob so we can make measurements of it
> % Get all the blob properties.  Can only pass in originalImage in version R2008a and later.
> blobMeasurements = regionprops(labeledImage, grayImage , 'all');

thanks for your response. i have R2007b version of matlab.so i cant run your code.
suppose i am doing like this.

my code
%calculte area in selected rectangle.
m = zeros(size(I,1),size(I,2));          %-- create initial mask
%m(111:222,123:234) = 1;
imshow(I)
r2=getrect
m(r2(2):r2(2)+r2(4), r2(1):r2(1)+r2(3))=1;
figure,imshow(m)

L=bwlabel(I);
STATS=regionprops(L,'Area');
disp('area using regionprops')
s=[STATS.Area];
w=sum(s)
it will calculate area of whole image i think.how should it be so that it will calculate area in selected rectangle.
or how can this be done by using roipoly where each points selected by user manually and it will do further processsing in that selected portion?
```
 0

```"saurabh pandey"
> STATS=regionprops(L,'Area');
> disp('area using regionprops')
> s=[STATS.Area];
> w=sum(s)
> it will calculate area of whole image i think.how should it be so that it will calculate area in selected rectangle.
> or how can this be done by using roipoly where each points selected by user manually and it will do further processsing in that selected portion?

a hint:
- look at the many other options of REGIONPROPS...

us
```
 0

```"saurabh pandey" <sathiya_saurabh@yahoo.co.in> wrote in message <i1bevk\$rfv\$1@fred.mathworks.com>...
> thanks for your response. i have R2007b version of matlab.so i cant run your code.
> suppose i am doing like this.
----------------------------------------------------------------------------------------
Come on, you're just not trying very hard.  Did you read the documentation about regionprops?  You just don't pass in the gray image if you have an old version.  In my uploaded code I gave comments on what to do for old versions.  You can do it the way you did if you want, or since you know r2, you can just calculate the area directly, without ever evn calling bwlabel or regionprops.  Here's another demo where I use imfreehanddraw to calculate the area and mean of the drawn area:
IMPORTANT: BE SURE TO JOIN ANY LINES SPLIT INTO TWO BY THE NEWSREADER!

% Change the current folder to the folder of this m-file.
% (The line of code below is from Brett Shoelson of The Mathworks.)
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
clc;	% Clear command window.
clear;	% Delete all variables.
close all;	% Close all figure windows except those created by imtool.
imtool close all;	% Close all figure windows created by imtool.
workspace;	% Make sure the workspace panel is showing.
fontSize = 20;

% Read in standard MATLAB gray scale demo image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
message = sprintf('Left click and hold to begin drawing.\nSimply lift the mouse button to finish');
uiwait(msgbox(message));
hFH = imfreehand();

% Create a binary image ("mask") from the ROI object.
subplot(2, 2, 2);
imshow(binaryImage);
title('Binary mask of the region', 'FontSize', fontSize);

% Calculate the area, in pixels, that they drew.
numberOfPixels1 = sum(binaryImage(:))
% Another way to calculate it that takes fractional pixels into account.
numberOfPixels2 = bwarea(binaryImage)

% Get coordinates of the boundary of the freehand drawn region.
structBoundaries = bwboundaries(binaryImage);
xy=structBoundaries{1}; % Get n by 2 array of x,y coordinates.
x = xy(:, 2);	% Columns.
y = xy(:, 1);	% Rows.
subplot(2, 2, 1); % Plot over original image.
hold on;	% Don't blow away the image.
plot(x, y, 'LineWidth', 2);

% Burn line into image by setting it to 255 wherever the mask is true.
burnedImage = grayImage;
burnedImage(binaryImage) = 255;
% Display the image with the mask "burned in."
subplot(2, 2, 3);
imshow(burnedImage);
title('New image with mask burned into image', 'FontSize', fontSize);

% Mask the image and display it.
% Will keep only the part of the image that's inside the mask, zero outside mask.
subplot(2, 2, 4);

% Calculate the mean

% Report results.
message = sprintf('Mean value within drawn area = %.3f\nNumber of pixels = %d\nArea in pixels = %.2f', ...
meanGL, numberOfPixels1, numberOfPixels2);
msgbox(message);
```
 0

```"Image Analyst" <imageanalyst@mailinator.com> wrote in message <i1cerh\$7fv\$1@fred.mathworks.com>...
> "saurabh pandey" <sathiya_saurabh@yahoo.co.in> wrote in message <i1bevk\$rfv\$1@fred.mathworks.com>...
> > thanks for your response. i have R2007b version of matlab.so i cant run your code.
> > suppose i am doing like this.
> ----------------------------------------------------------------------------------------
> Come on, you're just not trying very hard.  Did you read the documentation about regionprops?  You just don't pass in the gray image if you have an old version.  In my uploaded code I gave comments on what to do for old versions.  You can do it the way you did if you want, or since you know r2, you can just calculate the area directly, without ever evn calling bwlabel or regionprops.  Here's another demo where I use imfreehanddraw to calculate the area and mean of the drawn area:
> IMPORTANT: BE SURE TO JOIN ANY LINES SPLIT INTO TWO BY THE NEWSREADER!
>
> % Change the current folder to the folder of this m-file.
> % (The line of code below is from Brett Shoelson of The Mathworks.)
> if(~isdeployed)
> 	cd(fileparts(which(mfilename)));
> end
> clc;	% Clear command window.
> clear;	% Delete all variables.
> close all;	% Close all figure windows except those created by imtool.
> imtool close all;	% Close all figure windows created by imtool.
> workspace;	% Make sure the workspace panel is showing.
> fontSize = 20;
>
> % Read in standard MATLAB gray scale demo image.
> subplot(2, 2, 1);
> imshow(grayImage, []);
> title('Original Grayscale Image', 'FontSize', fontSize);
> set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
> message = sprintf('Left click and hold to begin drawing.\nSimply lift the mouse button to finish');
> uiwait(msgbox(message));
> hFH = imfreehand();
>
> % Create a binary image ("mask") from the ROI object.
> % Display the freehand mask.
> subplot(2, 2, 2);
> imshow(binaryImage);
> title('Binary mask of the region', 'FontSize', fontSize);
>
> % Calculate the area, in pixels, that they drew.
> numberOfPixels1 = sum(binaryImage(:))
> % Another way to calculate it that takes fractional pixels into account.
> numberOfPixels2 = bwarea(binaryImage)
>
> % Get coordinates of the boundary of the freehand drawn region.
> structBoundaries = bwboundaries(binaryImage);
> xy=structBoundaries{1}; % Get n by 2 array of x,y coordinates.
> x = xy(:, 2);	% Columns.
> y = xy(:, 1);	% Rows.
> subplot(2, 2, 1); % Plot over original image.
> hold on;	% Don't blow away the image.
> plot(x, y, 'LineWidth', 2);
>
> % Burn line into image by setting it to 255 wherever the mask is true.
> burnedImage = grayImage;
> burnedImage(binaryImage) = 255;
> % Display the image with the mask "burned in."
> subplot(2, 2, 3);
> imshow(burnedImage);
> title('New image with mask burned into image', 'FontSize', fontSize);
>
> % Mask the image and display it.
> % Will keep only the part of the image that's inside the mask, zero outside mask.
> subplot(2, 2, 4);
>
> % Calculate the mean
>
> % Report results.
> message = sprintf('Mean value within drawn area = %.3f\nNumber of pixels = %d\nArea in pixels = %.2f', ...
>     meanGL, numberOfPixels1, numberOfPixels2);
> msgbox(message);

thanks a lot.
imfreehand is not selecting the region. it gives error like........

Error in ==> roiParseInputs at 29
iptchecknargin(low,high,nargin_client,client_name);

Error in ==> imfreehand at 166
[commonArgs,specificArgs] = roiParseInputs(1,5,varargin,mfilename,{'Closed'});

Error in ==> freehandarea at 21
hFH = imfreehand();
```
 0

```"saurabh pandey" <sathiya_saurabh@yahoo.co.in> wrote in message <i1eblp\$nt4\$1@fred.mathworks.com>...
> "Image Analyst" <imageanalyst@mailinator.com> wrote in message <i1cerh\$7fv\$1@fred.mathworks.com>...
> > "saurabh pandey" <sathiya_saurabh@yahoo.co.in> wrote in message <i1bevk\$rfv\$1@fred.mathworks.com>...
> > > thanks for your response. i have R2007b version of matlab.so i cant run your code.
> > > suppose i am doing like this.
> > ----------------------------------------------------------------------------------------
> > Come on, you're just not trying very hard.  Did you read the documentation about regionprops?  You just don't pass in the gray image if you have an old version.  In my uploaded code I gave comments on what to do for old versions.  You can do it the way you did if you want, or since you know r2, you can just calculate the area directly, without ever evn calling bwlabel or regionprops.  Here's another demo where I use imfreehanddraw to calculate the area and mean of the drawn area:
> > IMPORTANT: BE SURE TO JOIN ANY LINES SPLIT INTO TWO BY THE NEWSREADER!
> >
> > % Change the current folder to the folder of this m-file.
> > % (The line of code below is from Brett Shoelson of The Mathworks.)
> > if(~isdeployed)
> > 	cd(fileparts(which(mfilename)));
> > end
> > clc;	% Clear command window.
> > clear;	% Delete all variables.
> > close all;	% Close all figure windows except those created by imtool.
> > imtool close all;	% Close all figure windows created by imtool.
> > workspace;	% Make sure the workspace panel is showing.
> > fontSize = 20;
> >
> > % Read in standard MATLAB gray scale demo image.
> > subplot(2, 2, 1);
> > imshow(grayImage, []);
> > title('Original Grayscale Image', 'FontSize', fontSize);
> > set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
> > message = sprintf('Left click and hold to begin drawing.\nSimply lift the mouse button to finish');
> > uiwait(msgbox(message));
> > hFH = imfreehand();
> >
> > % Create a binary image ("mask") from the ROI object.
> > % Display the freehand mask.
> > subplot(2, 2, 2);
> > imshow(binaryImage);
> > title('Binary mask of the region', 'FontSize', fontSize);
> >
> > % Calculate the area, in pixels, that they drew.
> > numberOfPixels1 = sum(binaryImage(:))
> > % Another way to calculate it that takes fractional pixels into account.
> > numberOfPixels2 = bwarea(binaryImage)
> >
> > % Get coordinates of the boundary of the freehand drawn region.
> > structBoundaries = bwboundaries(binaryImage);
> > xy=structBoundaries{1}; % Get n by 2 array of x,y coordinates.
> > x = xy(:, 2);	% Columns.
> > y = xy(:, 1);	% Rows.
> > subplot(2, 2, 1); % Plot over original image.
> > hold on;	% Don't blow away the image.
> > plot(x, y, 'LineWidth', 2);
> >
> > % Burn line into image by setting it to 255 wherever the mask is true.
> > burnedImage = grayImage;
> > burnedImage(binaryImage) = 255;
> > % Display the image with the mask "burned in."
> > subplot(2, 2, 3);
> > imshow(burnedImage);
> > title('New image with mask burned into image', 'FontSize', fontSize);
> >
> > % Mask the image and display it.
> > % Will keep only the part of the image that's inside the mask, zero outside mask.
> > subplot(2, 2, 4);
> > title('Masked Image', 'FontSize', fontSize);
> >
> > % Calculate the mean
> >
> > % Report results.
> > message = sprintf('Mean value within drawn area = %.3f\nNumber of pixels = %d\nArea in pixels = %.2f', ...
> >     meanGL, numberOfPixels1, numberOfPixels2);
> > msgbox(message);
>
>
>
> thanks a lot.
> imfreehand is not selecting the region. it gives error like........
>
> Error in ==> roiParseInputs at 29
>   iptchecknargin(low,high,nargin_client,client_name);
>
> Error in ==> imfreehand at 166
>   [commonArgs,specificArgs] = roiParseInputs(1,5,varargin,mfilename,{'Closed'});
>
> Error in ==> freehandarea at 21
> hFH = imfreehand();

also i am calculating area of object using bwarea. it gives in format
4.5299e+004. can i change its format so that it display 45299. actually i am new in matlab.i dont know every commond.
```
 0

```"saurabh pandey" <sathiya_saurabh@yahoo.co.in> wrote in message <i1eblp\$nt4\$1@fred.mathworks.com>...
> "Image Analyst" <imageanalyst@mailinator.com> wrote in message <i1cerh\$7fv\$1@fred.mathworks.com>...
> > "saurabh pandey" <sathiya_saurabh@yahoo.co.in> wrote in message <i1bevk\$rfv\$1@fred.mathworks.com>...
> > > thanks for your response. i have R2007b version of matlab.so i cant run your code.
> > > suppose i am doing like this.
> > ----------------------------------------------------------------------------------------
> > Come on, you're just not trying very hard.  Did you read the documentation about regionprops?  You just don't pass in the gray image if you have an old version.  In my uploaded code I gave comments on what to do for old versions.  You can do it the way you did if you want, or since you know r2, you can just calculate the area directly, without ever evn calling bwlabel or regionprops.  Here's another demo where I use imfreehanddraw to calculate the area and mean of the drawn area:
> > IMPORTANT: BE SURE TO JOIN ANY LINES SPLIT INTO TWO BY THE NEWSREADER!
> >
> > % Change the current folder to the folder of this m-file.
> > % (The line of code below is from Brett Shoelson of The Mathworks.)
> > if(~isdeployed)
> > 	cd(fileparts(which(mfilename)));
> > end
> > clc;	% Clear command window.
> > clear;	% Delete all variables.
> > close all;	% Close all figure windows except those created by imtool.
> > imtool close all;	% Close all figure windows created by imtool.
> > workspace;	% Make sure the workspace panel is showing.
> > fontSize = 20;
> >
> > % Read in standard MATLAB gray scale demo image.
> > subplot(2, 2, 1);
> > imshow(grayImage, []);
> > title('Original Grayscale Image', 'FontSize', fontSize);
> > set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
> > message = sprintf('Left click and hold to begin drawing.\nSimply lift the mouse button to finish');
> > uiwait(msgbox(message));
> > hFH = imfreehand();
> >
> > % Create a binary image ("mask") from the ROI object.
> > % Display the freehand mask.
> > subplot(2, 2, 2);
> > imshow(binaryImage);
> > title('Binary mask of the region', 'FontSize', fontSize);
> >
> > % Calculate the area, in pixels, that they drew.
> > numberOfPixels1 = sum(binaryImage(:))
> > % Another way to calculate it that takes fractional pixels into account.
> > numberOfPixels2 = bwarea(binaryImage)
> >
> > % Get coordinates of the boundary of the freehand drawn region.
> > structBoundaries = bwboundaries(binaryImage);
> > xy=structBoundaries{1}; % Get n by 2 array of x,y coordinates.
> > x = xy(:, 2);	% Columns.
> > y = xy(:, 1);	% Rows.
> > subplot(2, 2, 1); % Plot over original image.
> > hold on;	% Don't blow away the image.
> > plot(x, y, 'LineWidth', 2);
> >
> > % Burn line into image by setting it to 255 wherever the mask is true.
> > burnedImage = grayImage;
> > burnedImage(binaryImage) = 255;
> > % Display the image with the mask "burned in."
> > subplot(2, 2, 3);
> > imshow(burnedImage);
> > title('New image with mask burned into image', 'FontSize', fontSize);
> >
> > % Mask the image and display it.
> > % Will keep only the part of the image that's inside the mask, zero outside mask.
> > subplot(2, 2, 4);
> > title('Masked Image', 'FontSize', fontSize);
> >
> > % Calculate the mean
> >
> > % Report results.
> > message = sprintf('Mean value within drawn area = %.3f\nNumber of pixels = %d\nArea in pixels = %.2f', ...
> >     meanGL, numberOfPixels1, numberOfPixels2);
> > msgbox(message);
>
>
>
> thanks a lot.
> imfreehand is not selecting the region. it gives error like........
>
> Error in ==> roiParseInputs at 29
>   iptchecknargin(low,high,nargin_client,client_name);
>
> Error in ==> imfreehand at 166
>   [commonArgs,specificArgs] = roiParseInputs(1,5,varargin,mfilename,{'Closed'});
>
> Error in ==> freehandarea at 21
> hFH = imfreehand();

also i am calculating area of object using bwarea. it gives in format
4.5299e+004. can i change its format so that it display 45299. actually i am new in matlab.i dont know every commond.
```
 0

```"saurabh pandey" <sathiya_saurabh@yahoo.co.in> wrote in message
news:i1ei6d\$ol1\$1@fred.mathworks.com...
> "saurabh pandey" <sathiya_saurabh@yahoo.co.in> wrote in message
> <i1eblp\$nt4\$1@fred.mathworks.com>...
>> "Image Analyst" <imageanalyst@mailinator.com> wrote in message
>> <i1cerh\$7fv\$1@fred.mathworks.com>...
>> > "saurabh pandey" <sathiya_saurabh@yahoo.co.in> wrote in message
>> > <i1bevk\$rfv\$1@fred.mathworks.com>...

*snip*

First, when you quote a message in a reply, please delete the section of the
message you're quoting that's not relevant to your reply, like I did above.
It makes the messages shorter and IMO easier to read.

>> thanks a lot.
>> imfreehand is not selecting the region. it gives error like........
>>
>> Error in ==> roiParseInputs at 29
>>   iptchecknargin(low,high,nargin_client,client_name);
>>
>> Error in ==> imfreehand at 166
>>   [commonArgs,specificArgs] =
>> roiParseInputs(1,5,varargin,mfilename,{'Closed'});
>>
>> Error in ==> freehandarea at 21
>> hFH = imfreehand();

I believe we recently had a thread that touched upon this issue.  See

> also i am calculating area of object using bwarea. it gives in format
> 4.5299e+004. can i change its format so that it display 45299. actually i
> am new in matlab.i dont know every commond.

Change the display format of numbers in MATLAB using the FORMAT function.  I
usually use "format long g" personally.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
http://www.mathworks.com

```
 0
Reply slord (13284) 7/12/2010 2:38:26 PM

26 Replies
757 Views

Similiar Articles:

7/20/2012 3:46:02 PM