2

I am interested in separating features on an image using the watershed algorithm. Using the matlab tutorial, I tried to write a small proof of principle algorithm that I can further use in my image analysis.

Im = imread('../../Pictures/testrec.png');
bw = rgb2gray(Im);
figure
imshow(bw,'InitialMagnification','fit'), title('bw')

%Compute the distance transform of the complement of the binary image.
D = bwdist(~bw);
figure
imshow(D,[],'InitialMagnification','fit')
title('Distance transform of ~bw')

%Complement the distance transform, and force pixels that don't belong to the objects to be at Inf .
D = -D;
D(~bw) = Inf;

%Compute the watershed transform and display the resulting label matrix as an RGB image.
L = watershed(D);
L(~bw) = 0;
rgb = label2rgb(L,'jet',[.5 .5 .5]);
figure
imshow(rgb,'InitialMagnification','fit')
title('Watershed transform of D')

It appears that the feature separation is somewhat random, as can be seen from the prolonged feature in the middle. However, there does not seem to be any parameters for the watershed algorithm, that could be used to optimize its performance. Can you suggest how such parameter can be introduced, or a better algorithm to process the data.

Bonus Question: I am interested to first separate my image using bwconncomp, then selectively apply the watershed algorithm to only some of the regions. Assume I know which of the cc.PixelIdxList regions I want to apply the algorithm to - how do I get a new PixelIdxList with separated components.

Original Picture

enter image description here

Result of the algorithm

Aleksejs Fomins
  • 688
  • 1
  • 8
  • 20
  • Did you take a look at this? https://stackoverflow.com/questions/6691991/watershed-algorithm-in-matlab?rq=1 – Mehrdad Zandigohar Feb 20 '18 at 12:37
  • Hey Mehrdad, thanks for suggestion. I did have a look at it beforehand. The problem is that final result randomly separates some cells in half and that is undesirable. It would be great if the maximum thickness of separation (just made that up) could be controlled – Aleksejs Fomins Feb 21 '18 at 08:40

1 Answers1

0

Watershed transformation cannot separate convex shapes. There is no way to change that. A convex shape always results in one object.

Blobs very close to convex will always result in poor watershed results.

The only reason why you have that "somewhat random" result instead of a single basin is that a few pixels are a bit off the perimeter.

Results of watershed are improved by pre- and post-processing. But that would be very specific to a certain problem.

Piglet
  • 27,501
  • 3
  • 20
  • 43
  • Thanks for the answer, but unfortunately it is not very helpful. It is fairly clear to me that it can be done. For example, I could go through the effort of drawing every possible line through each shape, calculating the place where the width is smallest, and the average width, and then making the decision whether to separate based on that ratio. But, surely, I am not the first one to encounter this problem, and I would like to use an existing solution if possible – Aleksejs Fomins Feb 21 '18 at 08:45
  • @AleksejsFomins you asked if you can introcude some parameters to the watershed to make its results less random. I did not say it is impossible to get more reasonable results in another way. there are many ways to solve this but as I said this would depend on the specific problem and a general answer cannot be given. for this particular blob you can of course simply calculate the bounding box and take its short axis. this of course won't work for three merged blobs... you will find solutions to this problem in biological or medical image processing books where they separate cells. – Piglet Feb 21 '18 at 08:59