Load and Plot The Image Data
Load and Plot The Image Data
filter your image using 'imfilter'. use 'fspecial' to define your filter. Then use an active contour
model to segment the large objects. google 'active contour matlab'. use the 'polygon' and area
function to find the area of enclosed contours
cc = bwconncomp(BW);
stats = regionprops(cc, 'Area');
idx = find([stats.Area] > 80);
BW2 = ismember(labelmatrix(cc), idx);
2-D Example
1. Make a binary image containing two overlapping circular objects.
2. center1 = -10;
3. center2 = -center1;
4. dist = sqrt(2*(2*center1)^2);
5. radius = dist/2 * 1.4;
6. lims = [floor(center1-1.2*radius) ceil(center2+1.2*radius)];
7. [x,y] = meshgrid(lims(1):lims(2));
8. bw1 = sqrt((x-center1).^2 + (y-center1).^2) <= radius;
9. bw2 = sqrt((x-center2).^2 + (y-center2).^2) <= radius;
10. bw = bw1 | bw2;
11. figure, imshow(bw,'InitialMagnification','fit'), title('bw')
12. Compute the distance transform of the complement of the binary image.
13. D = bwdist(~bw);
14. figure, imshow(D,[],'InitialMagnification','fit')
title('Distance transform of ~bw')
15. Complement the distance transform, and force pixels that don't belong to the objects to
be at -Inf.
16. D = -D;
D(~bw) = -Inf;
17. Compute the watershed transform and display the resulting label matrix as an RGB
images.
18. L = watershed(D);
19. rgb = label2rgb(L,'jet',[.5 .5 .5]);
20. figure, imshow(rgb,'InitialMagnification','fit')
title('Watershed transform of D')
This demo implements the Active Contour Models as proposed by Kass et al.
If you find this useful, also look at Radon-Like Features based segmentation in the following paper:
Ritwik Kumar, Amelio V. Reina & Hanspeter Pfister, Radon-Like Features and their Application to Connectomics”, IEEE Computer
Society Workshop on Mathematical Methods in Biomedical Image Analysis (MMBIA) 2010
http://seas.harvard.edu/~rkkumar
Its code is also available on MATLAB Central
In a blog comment earlier this summer, someone asked how to find "which labeled regions have a bright
spot greater than some threshold?" That's a good question. It can be done efficiently and compactly using
the Image Processing Toolbox, but the techniques may not be widely known. (Have you ever used
the ismemberfunction to do image processing before?) Also, the techniques apply to other questions of
the form "which regions satisfy some criterion?"
I = imread('rice.png');
imshow(I)
Threshold it.
bw = im2bw(I, graythresh(I));
imshow(bw)
Clean it up a bit.
bw = bwareaopen(bw, 50);
imshow(bw)
Now label connected components and compute the PixelIdxList property for each labeled region. The
PixelIdxList is useful for the extracting pixels from a grayscale image that correspond to each labeled
region in the binary image. (See my post "Grayscale pixel values in labeled regions .")
L = bwlabel(bw);
s = regionprops(L,'PixelIdxList');
% Loop over each labeled object, grabbing the gray scale pixel values using
% PixelIdxList and computing their maximum.
for k = 1:numel(s)
max_value(k) = max(I(s(k).PixelIdxList));
end
bright_objects =
22
28
33
35
40
42
49
51
60
65
imshow(ismember(L, bright_objects))