7

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 ?

vini
  • 2,182
  • 4
  • 21
  • 37
  • 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 06:25
  • Yes my university has a license... – vini Mar 18 '12 at 06:29
  • 1
    @Mohammad Octave has nearly all the functions in MATLAB's Image Processing toolbox, and in many cases the code is compatible. –  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
  • @reve_etrange: How does SciPy compare as far as including all the same functions? – endolith Mar 19 '12 at 19:04
  • These are still images, right? You can't use motion to find the people against a stationary background? – endolith Mar 19 '12 at 19:17
  • @endolith Scipy's ndimage module does have most of the essentials - correlation and convolution, binary and gray morphology, distance transforms, watershed transform. As an image processing programming language, it looks well developed, though without the accompanying data-interaction UI of MATLAB, Octave and Scilab. – reve_etrange Mar 20 '12 at 00:37
  • @reve_etrange: "Data interaction UI"? – endolith Mar 20 '12 at 01:29
  • @endolith yes these are still images – vini Mar 20 '12 at 02:23
  • @endolith Sure, all three are scientific scripting languages with graphical aids for viewing and manipulating data, and for inspecting data as it moves through a program. IPython, Scipy, Numpy, matplotlib together come close, but lack features like the workspace editor, WYSIWG variable editor and the various GUI tools. – reve_etrange Mar 20 '12 at 10:06
  • @reve_etrange: Have you seen Spyder? All this stuff is bundled together into Python(x,y), which is what I use in Windows. Haven't used Matlab since college, so I'm just curious if I'm missing anything major. – endolith Mar 20 '12 at 13:47
  • @endolith I hadn't used it before. It certainly brings all the features I mention to the Python scientific modules. I'm a general fan of Python so I'll try it for some future work. My main need from any scientific programming environment is facilitating close familiarity with my data. PS Thanks! – reve_etrange Mar 22 '12 at 10:05

2 Answers2

8

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;
reve_etrange
  • 196
  • 1
  • 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. –  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
  • Yes i got that @Jonas and is my segmentation good enough need advice on that ? – vini Mar 18 '12 at 13:45
  • @vini: This is a decision only you can make. I do not know the requirements of your downsteam application of the segmentation. – Jonas Mar 18 '12 at 14:02
  • Is there an implementation of Watershed without those "Separating" pixels? – Royi Jul 16 '15 at 11:02
  • @Drazick Watershed pixels are defined by an ambiguity in region assignment. See here and here. In other words, yes, but, it amounts to assigning Watershed pixels arbitrarily to one of the adjacent regions (e.g. to the first created label). Are the walls inside or outside the room? – reve_etrange Jul 16 '15 at 22:38
0

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

MyCarta
  • 151
  • 3