7
$\begingroup$

enter image description here

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

enter image description here

  1. Calculated the watershed transform

enter image description here

My code:

  clear;
I=imread('inpaint.jpg');
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);


figure, imshow(gradmag,[]), title('Gradient magnitude (gradmag)')
L = watershed(gradmag);
% Lrgb = label2rgb(L);
 figure, imshow(L), title('Watershed transform of gradient magnitude (Lrgb)')

I have been successful in apply the watershed

enter image description here

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

$\endgroup$

migrated from stackoverflow.com Mar 19 '12 at 15:23

This question came from our site for professional and enthusiast programmers.

  • $\begingroup$ 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 :-/ $\endgroup$ – Spacey Mar 18 '12 at 6:25
  • $\begingroup$ Yes my university has a license... $\endgroup$ – vini Mar 18 '12 at 6:29
  • 1
    $\begingroup$ @Mohammad Octave has nearly all the functions in MATLAB's Image Processing toolbox, and in many cases the code is compatible. $\endgroup$ – reve_etrange Mar 18 '12 at 11:21
  • $\begingroup$ @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... $\endgroup$ – Spacey Mar 18 '12 at 15:58
  • $\begingroup$ @Mohammad Also, Scilab's image processing toolbox is growing and thriving. Not sure if it yet offers what you're looking for. $\endgroup$ – Phonon Mar 19 '12 at 16:56
8
$\begingroup$

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 = gradmag - min(gradmag(:));
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;
$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – vini Mar 18 '12 at 13:07
  • $\begingroup$ 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. $\endgroup$ – reve_etrange Mar 18 '12 at 13:10
  • $\begingroup$ Yes it is present however the results were not satisfactory when i applied it $\endgroup$ – vini Mar 18 '12 at 13:12
  • $\begingroup$ Check my segmentation result is it good enough ? =) $\endgroup$ – vini Mar 18 '12 at 13:14
  • $\begingroup$ @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. $\endgroup$ – Jonas Mar 18 '12 at 13:39
0
$\begingroup$

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

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.