8

I have a problem while implementing the marker controlled watershed in Matlab.

The input image is a binary mask which have two clustered object. An other image is an other binary image where the white regions indicate the markers.

image marker

Then I try to use the marker controlled watershed to splitting the clustered object. The code is as follows:

bw=imread('Im.jpg'); % read image
bwMarker=imread('Marker.jpg');% read marker
bw(bwMarker)=0; % impose the marker on the image
L=watershed(bw);% do the watershed
%% plot
rgb = label2rgb(L,'jet',[.5 .5 .5]);
figure(2), imshow(rgb,'InitialMagnification','fit')
title('Marker Controlled Watershed transform ')

The result is show as this plot.

enter image description here
It splits the object into two, but the shape is not intact. I want to get the whole object but just a line splitting the two (as shown below) Can anybody help me out? Thanks.

enter image description here

Appended question: Thank you for yoda provides the answer. However, in this case, the distance transform using 'cityblock' is good but if we using 'euclidean' it will lead to over segmentation. Because the watershed will depend on the local minimums in the image and it is coincident that the 'cityblock ' distance transform result in correct number of local minimums. If we apply the 'cityblock' for other image, it will lead to over segmentation as well. Now let's see yoda's code:

img=im2bw(imread('https://i.stack.imgur.com/qrYCL.jpg'));

imgDist=-bwdist(~img,'cityblock');
imgDist(~img)=-inf; 

% check local minimums
BW = imregionalmin(imgDist);
figure(1), imshow(BW);  
title('Regional Minima in Original Image');

imgLabel=watershed(imgDist); 

The local minimums and the result are shown as follows:

enter image description hereenter image description here

Note that in the left figure, the local minimum are show as white regions. It is observed that only two local minimum shown in the clustered objects. So leads to the good results.

Now let's take a look at the distance transform by using 'euclidean'.

imgDist=-bwdist(~img);
imgDist(~img)=-inf; 

% check local minimums    
BW = imregionalmin(imgDist);
figure(1), imshow(BW);  
title('Regional Minima in Original Image');

imgLabel=watershed(imgDist);    

imshow(imgLabel==0,'InitialMagnification','fit')

The local minimums and the result are shown as follows:

enter image description hereenter image description here

Note that in the left figure, the local minimum are show as white regions. It is observed that there are many local minimum shown in the clustered objects region. So this leads to the over-segmented results.

The over segmentation is because the watershed will first check out the local minimums in the image, then base on the local minimums, perform the watershed. Note that if there are too many local minimums than the desired segmented objects, it will lead to over segmentation. The marker controlled watershed is proposed to replace the original local minimums and achieve better result(since each marker will represent one desired segmented object). But I don't know how to impose the 'marker' so that the original local minimums are depressed and the image only have the local minimums specified by the 'marker'. Thanks.

highBandWidth
  • 16,751
  • 20
  • 84
  • 131
Cheung
  • 373
  • 6
  • 15

2 Answers2

8

Solution 2: Using marker based watershed:

You can use the function imimposemin to force the local minima to be where your markers are. You'll need to slighty modify the code in solution 1 below by replacing the first imDist... line with

imgDist=-bwdist(~img);
imgDist=imimposemin(imgDist,marker);

The rest of the code is the same. You should get the segmentation as follows:

enter image description here


Solution 1: Using Manhattan distance:

Here is a solution in MATLAB that separates your two clusters:

img=im2bw(imread('https://i.stack.imgur.com/qrYCL.jpg'));

imgDist=-bwdist(~img,'cityblock');
imgDist(~img)=-inf;    
imgLabel=watershed(imgDist);    

imshow(imgLabel==0,'InitialMagnification','fit')

enter image description here

Community
  • 1
  • 1
abcd
  • 41,765
  • 7
  • 81
  • 98
  • 1
    @belisarius: `inf` is infinity - or for practical purposes, a very large number. – Jonas Sep 15 '11 at 13:10
  • Hi yoda, thank you for your answer. However, in this case, the distance transform using 'cityblock' is good but if we using 'euclidean' it will lead to over segmentation. If we change to other example it will fail as well. Please see my edited question for more details. Thanks. – Cheung Sep 15 '11 at 16:27
  • @Cheung Please see my edit that answers your question on marker based watershed :) – abcd Sep 15 '11 at 22:02
3

This is how I would do this in Mathematica. Hope you can translate.

i1 = Binarize@Import["https://i.stack.imgur.com/qrYCL.jpg"];
marker = Binarize@Import[  "https://i.stack.imgur.com/CMI6Z.jpg"]; 

ImageMultiply[i1, WatershedComponents[i1, marker] // Colorize]

enter image description here

Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
  • 1
    Not sure how a Mathematica answer is applicable here.. "hope you can translate" isn't particularly helpful. – Dang Khoa Sep 15 '11 at 00:11
  • 4
    @strictly Many Matlab users were grateful from my Mathematica answers before, because image manipulation is quite similar. See for example http://stackoverflow.com/questions/5312514/clean-noise-from-an-image-with-matlab/6259407#6259407, http://stackoverflow.com/questions/7387801/image-analysis-fiber-recognition/7390040#7390040, http://stackoverflow.com/questions/5770818/how-to-align-image-matlab/6240992#6240992. In the last one, for example, another user (Amro) posted the translation. So I guess my answers in the Matlab tag are useful after all – Dr. belisarius Sep 15 '11 at 00:51
  • Hi belisarius, I don'know Mathematica, but still thank you for the answer. – Cheung Sep 16 '11 at 03:56