How can I calculate area of an object in the image with .png extension?
15 次查看(过去 30 天)
显示 更早的评论
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.
采纳的回答
Rushikesh Tade
2014-9-12
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 个评论
Kiran Das
2022-5-8
@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
2022-5-8
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.
更多回答(1 个)
Image Analyst
2014-9-12
Three steps.
- Convert your color image to grayscale
- Threshold the grayscale image
- 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 个评论
Image Analyst
2014-9-13
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Segmentation and Analysis 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!