How to get Areas of Specified Objects in an Image?

I have been trying to take an image and divide it up into regions, and then calculate various properties of those regions. A sample image is linked: <http://tinypic.com/view.php?pic=esq2it&s=7>
I have been running the following code:
close all;
clear all;
RGB = imread('vir1036.tif');
threshold = graythresh(RGB);
BW = im2bw(RGB,threshold);
L = bwlabel(BW);
imshow(BW)
figure
imagesc(L)
I am new at MATLAB and still learning about all of these commands. As you can see, there is some noise, but bwlabel has identified most of the regions I was targeting (the regions in various colors of blue). I would like to be able to find the area of each of those regions separately. Is there a way to clean up this image so that it find all of the regions I was targeting, and do you have any suggestions for how to find the areas of each of those regions?
Thank you in advance for any help!

 Accepted Answer

Edit Per Comments
%%Answers 7/21
I = imread('coins.png');
Ibw = I>100;
Edited Again
Ibw = bwareaopen(Ibw,20); %remove eveyr object with less than 20px.
CC = bwconncomp(Ibw);
imshow(Ibw);
H = msgbox('Click on Object of Interest');
uiwait(H);
while 1
[x y] = ginput(1);
Pxidx = sub2ind(size(Ibw),round(y),round(x)); %pixel linear index
Objidx = cellfun(@(x)any(x==Pxidx),CC.PixelIdxList); %what object?
if ~any(Objidx)
H = errordlg('Doh! You didn''t select a valid object');
uiwait(H);
H = msgbox(sprintf('Let''s try again\nClick on Object of Interest'));
uiwait(H);
else
break;
end
end
ObjArea = numel(CC.PixelIdxList{Objidx}); %get area
msgbox(sprintf('Area is %i Px',ObjArea)); %show area

14 Comments

I tried
stats = regionprops(L,'Area')
stats = regionprops(L, 'CovexArea')
they returned
stats =
104x1 struct array with fields:
ConvexArea
stats =
104x1 struct array with fields:
Area
How do I use region props to find for example the region of the blue region in the lower left hand corner
You can use ginput to have the user point to the region, get the x/y coordinate (column/row), and compare that to the labeled image. When using these coordinates on the labeled image, you should get a number. This number is the index you want to use in your stats struct: stats(index).Area
Hopefully this helps.
You could also use bwconncomp to get the pixelidxlist. Then use ginput -> convert to linear index with (sub2ind) and then ismember with cellfun on the CC.PixelIdxList, to find which region it's in by index. Then the area will correspond to that one
Both your comments were very helpful! I was able to get the area of each region by the number of pixels. Unfortunately, I am doing this for a number of pictures, and the index of a region keeps changing because bwlabel labels some of the noise. Is there anyway to get bwlabel to only label pixels over a certain number?
Example:
if size(pixels)>20,
L = bwlabel(BW);
end
Use bwareaopen to get rid of small stuff (noise).
weirdly at 50 it gets rid of smaller regions I want but no small dots. At 10 nothing happens.
I'll bet you'd be surprised at how small an object actually is if it only has 10px. Did you see the above code?
I just did. It works very well, but you also have smaller regions that did not get removed by bwareaopen (the coin near the top right). Why doesn't that disappear?
Because it's greater than 20 pixels in size... These are the sizes of every coin:
cellfun('prodofsize',CC.PixelIdxList)
ans =
2693 1899 2725 1772 2796 2563 2648 2598
1935 1906
I'd have to up the ante in bwareaopen.
I tried 800 and even the smallest ones still didn't go away.
cellfun('prodofsize',CC.PixelIdxList)
run this and see how big your objects actually are.
Or do a histogram of that - etc. Post an image, but I think the code above is basically what you want.
I meant in your image. Were you able to make those small areas disappear? I could not even at 800.
In my image none of the areas were below 800!!! They were the sizes that I showed you four comments ago.
I see I'm looking at what you caned an invalid object (when you click on it it says that you did not select a valid object. I believe I figured it out by using imcomplement before using bwareaopen! Thank you so much! I'm sorry for the misunderstanding!

Sign in to comment.

More Answers (2)

Hello Sean de Wolski how do I calculate the translation of these objects?
PERİMETER?

2 Comments

In short
props = regionprops(binaryImage, 'Perimeter');
allPerimeters = [props.Perimeter]

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!