how can find size of this objects in a binary image?

조회 수: 27 (최근 30일)
sara
sara 2014년 9월 24일
편집: Image Analyst 2021년 10월 23일
hi I have a binary image and it has some objects...how can find size of this objects??Is there any toolbox in matlab for this??

채택된 답변

Meshooo
Meshooo 2014년 9월 25일
편집: Meshooo 2014년 9월 25일
There are many ways to do that. One easy way is to use nnz (Number of nonzero matrix elements).
So if your image is I, then:
I = im2bw (I); %be sure that your image is binary
Total_White_Pixels = nnz(I); % total area based on number of white pixels
Hope that helps you.
  댓글 수: 3
sylvester Fok
sylvester Fok 2021년 10월 23일
what is the units?
Image Analyst
Image Analyst 2021년 10월 23일
편집: Image Analyst 2021년 10월 23일
@sylvester Fok, the units are "pixels" since nnz() gives a count of elements with non-zero values in an array. If you have the area of a pixel in real world units, like it's 5 square meters per pixel, then you can multiply by that spatial calibration factor, like I do in the attached demo.
Note that this answer just gives the area of all blobs added together, not of single blobs like I showed in my answer below. Essentially:
mask = grayImage > 128; % Create a logical binary image.
props = regionprops(mask, 'Area'); % Find areas of each blob and put into a structure.
allAreas = [props.Area]
Be sure to run my Image Segmentation Tutorial.

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Meshooo
Meshooo 2014년 9월 25일
편집: Meshooo 2014년 9월 25일
You can do it in few steps. If your input image is I then:
%Step 1: Label each object using the following code.
I = im2bw(I); % be sure your image is binary
L = bwlabel(I); % label each object
%Step 2: see the label of each object
s = regionprops(L, 'Centroid');
imshow(I)
hold on
for k = 1:numel(s)
c = s(k).Centroid;
text(c(1), c(2), sprintf('%d', k), ...
'HorizontalAlignment', 'center', ...
'VerticalAlignment', 'middle');
end
hold off
% Step 3: find the area of the object you want using its label
Obj = (L == 1) % 1 is the label number of the first object.
figure, imshow(Obj);
Area = regionprops(Obj,'Area') % the answer
Now, for example if you want to find the area of object number 3, the just change number 1 to 3.
Obj = (L == 3) where 3 is the label number of the third object.
Area is the number of pixels in the object.
Hope that will help.

Matt J
Matt J 2014년 9월 24일
How about,
regionprops(...,'Area')

Image Analyst
Image Analyst 2014년 9월 24일
편집: Image Analyst 2021년 10월 23일
Of course sara. I thought you've already run across my image segmentation tutorial by now. Run it here http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862, replacing the coin image with yours. It will tell you the areas, centroids, bounding boxes, coordinates of the perimeter, etc.
Basically, it's
mask = grayImage > 128; % Create a logical binary image.
props = regionprops(mask, 'Area'); % Find areas of each blob and put into a structure.
allAreas = [props.Area]

sara
sara 2014년 9월 25일
편집: sara 2014년 9월 25일
thanks Image Analyst and Meshooo Matt
I searched and tried this codes befor... but I want to know the size of an object in this image..I don't want to obtain the largest or smallest objects of this image..I want to know just the size of white object.. for simples of this question we can assume that we just have one object in this image...I hope I could explain my question clearly ...I have tow images that are very similar to each other and I want to chose the image that my goal object is larger in it...thanks

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by