# How to apply watershed to segment images using matlab?

How to segment this image using watershed to retrieve only the people in the image ?

I have done the following so far :

1. Calculated a gradient

1. Calculated the watershed transform

My code:

  clear;
I=rgb2gray(I);

hy = fspecial('sobel');
hx = hy';
Iy = imfilter(double(I), hy, 'replicate');
Ix = imfilter(double(I), hx, 'replicate');
gradmag = sqrt(Ix.^2 + Iy.^2);

% Lrgb = label2rgb(L);
figure, imshow(L), title('Watershed transform of gradient magnitude (Lrgb)')


I have been successful in apply the watershed

Wanted to know can i outline my objects in the original image so that it appears segmented ?

• Somewhat tangential, but are you using image processing license of MATLAB from university or corporate? I have looked into it and it seems quite expensive :-/ – Spacey Mar 18 '12 at 6:25
• Yes my university has a license... – vini Mar 18 '12 at 6:29
• @Mohammad Octave has nearly all the functions in MATLAB's Image Processing toolbox, and in many cases the code is compatible. – reve_etrange Mar 18 '12 at 11:21
• @reve_etrange Ah thank you for that!! I will have to look into it - my old boss always said bad things about octave like it cant hold big matricies, not nice GUI like matlab, etc etc, so I never really looked into it... – Spacey Mar 18 '12 at 15:58
• @Mohammad Also, Scilab's image processing toolbox is growing and thriving. Not sure if it yet offers what you're looking for. – Phonon Mar 19 '12 at 16:56

Recall that the Watershed transform treats its input as a topographic map, and simulates flooding that topography with water. The "catchment basins" or "Watershed regions" are then the parts of the map which "hold water" without spilling into other regions.

The gradient magnitude is a poor segmentation function as-is; the noise and open contours lead to an extreme oversegmentation of the image. We can try a series of morphological operations with the intent of creating approximate foreground and background markers, and use these to remove the spurious parts of the gradient.

%# Normalize.
g = g / max(g(:));

th = graythresh(g); %# Otsu's method.
a = imhmax(g,th/2); %# Conservatively remove local maxima.
th = graythresh(a);
b = a > th/4; %# Conservative global threshold.
c = imclose(b,ones(6)); %# Try to close contours.
d = imfill(c,'holes'); %# Not a bad segmentation by itself.
%# Use the rough segmentation to define markers.
g2 = imimposemin(g, ~ imdilate( bwperim(a), ones(3) );
L = watershed(g2);


This works OK. You get both groups of people and their shadows as regions, with a little bit of noise.

Can you elaborate on your goals? That is, will you be segmenting many different images or just images highly similar to this example? Do you need to ignore the shadows and separate the two overlapping people?

I will try to update the answer if you respond to these questions.

Segmentation Overlay

You asked how to overlay a segmentation. One way is to use the watershed lines to specify pixels in the original and set them to a bright color.

boundaries = L == 0;
I(boundaries) = 255;

• All of my images have the same conditions as portrayed by this image..Isn't thresholding considered as a bad means of segmentation and yes i would like to show them as different objects – vini Mar 18 '12 at 13:07
• Thresholding can produce odd results in images with odd histograms, and a given threshold is usually only useful for a single image, but there is nothing inherently "bad" about them. Otsu's method should be in any computer vision coder's toolbox - just don't apply it blindly. – reve_etrange Mar 18 '12 at 13:10
• Yes it is present however the results were not satisfactory when i applied it – vini Mar 18 '12 at 13:12
• Check my segmentation result is it good enough ? =) – vini Mar 18 '12 at 13:14
• @vini: Any image segmentation requires some sort of thresholding at some point, since you'll need to make a decision whether an object is part of the foreground or the background eventually. – Jonas Mar 18 '12 at 13:39

You can use the function bwperim Some good examples here http://blogs.mathworks.com/steve/2006/06/02/cell-segmentation/

• i have already solved my problem thanks for the reference – vini Apr 16 '12 at 9:05