You are on page 1of 7

%# Load and plot the image data:

imageData = imread('lattice_pic.jpg');  %# Load the lattice image


subplot(221);
imshow(imageData);
title('Original image');

%# Gaussian-filter the image:


gaussFilter = fspecial('gaussian',[31 31],9);  %# Create the filter
filteredData = imfilter(imageData,gaussFilter);
subplot(222);
imshow(filteredData);
title('Gaussian-filtered image');

%# Perform a morphological close operation:


closeElement = strel('disk',31);  %# Create a disk-shaped structuring
element
closedData = imclose(filteredData,closeElement);
subplot(223);
imshow(closedData);
title('Closed image');

%# Find the regions where local maxima occur:


maxImage = imregionalmax(closedData);
maxImage = imdilate(maxImage,strel('disk',5));  %# Dilate the points
to see
                                                %# them better on the
plot
subplot(224);
imshow(maxImage);
title('Maxima locations');

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.

To run it with GUI 


   1. Type guide on the matlab prompt. 
   2. Click on "Go to Existing GUI" 
   3. Select the snk.fig file in the same directory as this file 
   4. Click the green arrow at the top to launch the GUI
   Once the GUI has been launched, you can use snakes by 
   1. Click on "New Image" and load an input image. Samples image are provided. 
   2. Set the smoothing parameter "sigma" or leave it at its default value and click "Filter". This will smooth the image. 
   3. As soon as you click "Filter", cross hairs would appear and using them and left click of you mouse you can pick initial contour
location on the image. A red circle would appear everywhere you click and in most cases you should click all the way around the
object you want to segment. The last point must be picked using a right-click in order to stop matlab for asking for more points. 
   4. Set the various snake parameters (relative weights of energy terms in the snake objective function) or leave them with their
default value and click "Iterate" button. The snake would appear and move as it converges to its low energy state.
Copyright (c) Ritwik Kumar, Harvard University 2010 
              www.seas.harvard.edu/~rkkumar
This code implements “Snakes: Active Contour Models” by Kass, Witkin and Terzopolous incorporating Eline, Eedge and Eterm
energy factors. See the included report and the paper to learn more.

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

Finding bright objects

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?"

Let's work with the rice image.

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');

Compute the maximum pixel value for each labeled region.

% Initialize vector containing max values.


max_value = zeros(numel(s), 1);

% 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

% Show all the maximum values as a bar chart.


bar(max_value)
And we'll finish by using find to determine which objects have a maximum value greater than 200. Then
we'll display those objects using ismember.

bright_objects = find(max_value > 200)

bright_objects =

22
28
33
35
40
42
49
51
60
65

imshow(ismember(L, bright_objects))

You might also like