How can I calculate area of an object in the image with .png extension?

15 visualizaciones (últimos 30 días)
Hi there,
I want to obtain area of an object in the png image. Could anyone please tell me how to do it? One more thing, is there any way to calculate length a between 2 points of the horizontal line and the curve? (as picture)
I'm not sure there have been answers for my question, although I've been trying to find them on the internet. But they don't work in my situation.
Any help would be greatly appreciated. Excuse for my English.
Khanh.

Respuesta aceptada

Rushikesh Tade
Rushikesh Tade el 12 de Sept. de 2014
I have converted the image into problem image by removing the text which you used to describe the problem and filling the background with black color.
Following is the code to do it.
input_im=imread('area2.png');
input_im=rgb2gray(input_im);
imshow(input_im)
title('Original Object')
After that I have tried separating the object out:
sum_of_x_axis=sum(input_im,1);
sum_of_y_axis=sum(input_im,2);
location_of_object_on_x=find(sum_of_x_axis>0);
location_of_object_on_y=find(sum_of_y_axis>0);
seperated_object=input_im(location_of_object_on_y(1):...
location_of_object_on_y(end),location_of_object_on_x(1):...
location_of_object_on_x(end));
figure;
imshow(seperated_object);
title('Seperated Object')
Now the area of the object can be calculated as : (seperated_object_image_area) - (area_which_does_not_belong_to_object_in_the_seperated_object_image)
and Height of the line ("a" displayed in your image) can be found as:maximum vertical sum along the x axis.
Following is the code for that:
detected_outer_surface=im2bw(seperated_object);
sum_of_outer_space=0;
for i=1:size(seperated_object,1)
if seperated_object(i,1)>0
break
end
for j=1:size(seperated_object,2)
if seperated_object(i,j)>0
break
end
detected_outer_surface(i,j)=1;
sum_of_outer_space=sum_of_outer_space+1;
end
end
for i=1:size(seperated_object,1)
if seperated_object(i,size(seperated_object,2))>0
break
end
for j=size(seperated_object,2):-1:1
if seperated_object(i,j)>0
break
end
detected_outer_surface(i,j)=1;
sum_of_outer_space=sum_of_outer_space+1;
end
end
figure;
imshow(detected_outer_surface)
title('Detected outer Surface which does not belong to object')
Area_of_object=size(seperated_object,1)*size(seperated_object,2);
Area_of_object=Area_of_object-sum_of_outer_space
Height_of_the_line=max(sum_of_x_axis)
Answers which I have got are:
%Area_of_object =103353
%Height_of_the_line =24240
  10 comentarios
Kiran Das
Kiran Das el 8 de Mayo de 2022
@Image Analyst Thank you for amswering my comment. But it is my humble request you to please explain the code for me. I can not get it as I am a begginer. This code is helpful to me it will be more fruitful to me as if someone expain the code.
Thank You.
Image Analyst
Image Analyst el 8 de Mayo de 2022
The code in this answer doesn't look like very good code (it's not my code). There are much better ways to do what he did. I suggest you don't try to understand it and just look at the Image Segmentation Tutorial in my File Exchange. It has lots of comments to walk/talk you through the basic image segmentation process.

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 12 de Sept. de 2014
Three steps.
  1. Convert your color image to grayscale
  2. Threshold the grayscale image
  3. Either sum the white pixels, or call bwarea()
% Calculate the area using bwarea().
area1 = bwarea(binaryImage)
% Calculate the area in pixels using sum()
area2 = sum(binaryImage(:))
The attached code (full blown demo test.m attached in blue below the image) calculates the area both ways and produces this image.
I get 103353, or 103374.4 depending on the calculation method used.
  7 comentarios
Khanh
Khanh el 13 de Sept. de 2014
Your helps are so amazing. Thank you. I will study your comments and programs for my problem.
Image Analyst
Image Analyst el 13 de Sept. de 2014
You're welcome. You can always vote for good answers even though they were not the one answer you officially "Accepted". By the way, you can get the height of the line in a single line of code rather than those two dozen lines of 4 nested for loops in the answer you accepted.
height = find(binaryImage(:,col), 1, 'last')-find(binaryImage(:,col), 1, 'first');
where col is the column of the image where you're interested in knowing the height for.

Iniciar sesión para comentar.

Categorías

Más información sobre Image Segmentation and Analysis en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by