Implement Adaptive watershed segmentation in Matlab
<p>I will like to implement "Adaptive Watershed Segmentation" in Matlab.
There are six steps in this algorithm.  Input is figure(a) and result is figure(d).
Would you please to help me check is there any mistake in my code, and I don't know how to implement the sixth step. 
Thank you so much!</p>

<p><img src="https://i.stack.imgur.com/n6BRI.gif" alt="Input image"></p>

<p><img src="https://i.stack.imgur.com/EFLwz.png" alt="Result image"></p>

<p>Load image:</p>

<pre><code>input_image = imread('test.gif');
</code></pre>

<p>Step 1 : Calculate D(x,y) at each (x,y), obtain the Euclidian distance map of the binary image and assign each value of M(x,y) as 0.</p>

<pre><code>DT = bwdist(input_image,'euclidean'); % Trandform distance:Euclidian distance 
[h,w]=size(DT);
M = zeros(h,w);
</code></pre>

<p>Step 2 : Smooth the distance map using Gaussian filter to merge the adjacent maxima, set M(x,y) as 1 if D(x,y) is a local maximum, and then obtain the marker map of the distance map.</p>

<pre><code>H = fspecial('gaussian');
gfDT = imfilter(DT,H); 
M = imregionalmax(gfDT); % maker map, M = local maximum of gfDT
</code></pre>

<p>Step3 : Scan the marker map pixel by pixel. If M(x0,y0) is 1, seek the spurious maxima in its neighbourhood with a radius of D(x ,y ).When M(x,y) equals 1 and sqr((x − x0)^2 + (y − y0)^2 ) ≤ D(x0, y0) , set M(x,y) as 0 if D(x,y) &lt; D(x0,y0).</p>

<pre><code>for x0 = 1:h
    for y0 = 1:w
        if M(x0,y0) == 1
            r = ceil(gfDT(x0,y0));

            % range begin:(x0-r,y0-r) end:(x0+r,y0+r)
            xb = x0-r;
            if xb &lt;= 0
                xb =1;
            end

            yb = y0-r;
            if yb &lt;= 0
                yb =1;
            end

            xe = x0+r;
            if xe &gt; w
            xe = w;
            end

            ye = y0+r;
            if ye &gt; h
                ye = h;
            end

            for x = yb:ye
                for y = xb:xe
                    if M(x,y)==1 
                        Pos = [x0,y0 ;x,y];
                        Dis = pdist(Pos,'euclidean');
                        IFA = Dis&lt;= (gfDT(x0,y0));
                        IFB = gfDT(x,y)&lt;gfDT(x0,y0);
                        if ( IFA &amp;&amp; IFB)
                            M(x,y) = 0;
                        end
                    end
                end
            end
        end
    end
end
</code></pre>

<p>Step 4:</p>

<p>Calculate the inverse of the distance map,and the local maxima turn out to be the local minima.</p>

<pre><code>igfDT = -(gfDT);
</code></pre>

<p>STep5:</p>

<p>Segment the distance map according to the markers by the conventional watershed algorithm and obtain the segmentation of binary image.</p>

<pre><code>I2 = imimposemin(igfDT,M);
L = watershed(I2);
igfDT (L==0)=0;
</code></pre>

<p>Step 6 :  Straighten the watershed lines by linking the ends of the watershed lines with a straight line and reclassifying the pixels along the straight line.</p>

<p><strong>I don't know how to implement this step</strong></p>