# how to calculate irregular shape area

• Ok, you have to calculate the area in pixel using a technique explained in my post on 16Jan, obtaining a black and white image (object in white (1) and background in black(0)) then counting the number of white pixels.
Then you make the proportion with the reference object:
ref_px:ref_cm=obj_px:obj_cm

So, first of all, you have to understand what kind of method you have to use to "select" your object.
• Ok, you have an image with a white object and black background.
So, count the white pixels:
[code]objectAreaInPixels=length(find(Image==1));[/code]
Now convert into centemeters:
[code]centemetersInOnePixel=referenceObjectLengthInCentemeters/referenceObjectLengthInPixels;
centemetersInOnePixelSquare=centemetersInOnePixel^2;
objectAreaInCentemeters=objectAreaInPixels*centemetersInOnePixel;[/code]

• i must declare reference object length in centemeters and reference object length in pixel right?
• Yes, you need to have these information.
To measure the number of pixels of the reference object in your image you can use the imdistline matlab function (you draw a line on the image and matlab returns the length of the line in pixels).
• As I wrote in the previous posts, the image with the object has to be a black and white image. It means that is a matrix with only zeros (black) and ones (white).
Where do you want to display the message?
You can display it in the command window using the disp funcion.
Let's try this simple code and check what I mean.
[code]Image=zeros(30); Image(4:6,4:6)=1;
figure();imshow(Image);
white_pixel_count=length(find(Image==1));
disp(['the area is:' num2str(white_pixel_count) 'cm2']);[/code]
• opss..i forgot to put another coding..so now its already can read the value of pixel..=)
• Well done!
• not done..i'm now try to get the value of area in cm2.i have error of that coding..
• that coding for area in cm2 is not function.when using that coding,it give it the output value with the output for pixel area.this coding not function well.
objectAreaInCentemeters=objectAreaInPixels*centemetersInOnePixel;

can u help me for that coding. i am using this as reference value.
referenceObjectLengthInCentemeters=100;
referenceObjectLengthInPixels=1000;
• white_pixel_count=length(find(Image==1));

Image is in binary image?i try to display the length that already count by that coding by using[italic] message = sprintf('Number of pixels = %d', ...
white_pixel_count);
[/italic]this coding but its not functioning.how can i display that value when its already count?
• I was wrong, de disp in the previous post had to be in this way:
[code]disp(['the area is:' num2str(white_pixel_count) 'pixels']);[/code]
Then, you have to add the code for the conversion, combining the two codes I gave you:

[code]referenceObjectLengthInCentemeters=100;
referenceObjectLengthInPixels=1000;
Image=zeros(30); Image(4:6,4:6)=1;
figure();imshow(Image);
objectAreaInPixels=length(find(Image==1));
centemetersInOnePixel=referenceObjectLengthInCentemeters/referenceObjectLengthInPixels;
centemetersInOnePixelSquare=centemetersInOnePixel^2;
objectAreaInCentemeters=objectAreaInPixels*centemetersInOnePixel;
disp(['the area is:' num2str(objectAreaInCentemeters) 'cm2']);[/code]

Let's try this code with the phantom image so that you can check the steps of the code by hand, and understand what you are doing.
• centemetersInOnePixelSquare=centemetersInOnePixel^2;

when this coding functioning?

• centemetersInOnePixelSquare=centemetersInOnePixel^2;

when this coding functioning?

• ok..i got it..i'm already understand that coding..thank you..my problem now to get the value of "referenceObjectLengthInPixels"..i'm try to find it by using your suggestion by using "imdistline"...but do you have any idea if i can gel the reference object lenght in pixel automatically without using "imdistline"...because i want to get it automatically without using manual style.