# how to repeat watershed transform to improve separation

2 views (last 30 days)
Tugce Irem on 25 Aug 2022
Hello,
I have small circle touching objects which need to be separated and analyzed. I've read Watershed transform question from tech support by Mr. Eddins. In the information he said if it's needed repeat the steps. I'm not sure how to repeat the steps. I wrote repeating steps but it still not working accurately. Here is my code. If anyone can help me to figure out where I made a mistake or how I can improve separation, I'll be really appreciate it. Thank you
%watershed
bw2b = ~bwareaopen(~BWsegmentationb,10);
figure(); imshow(bw2b)
Db = -bwdist(~BWsegmentationb);
figure(); imshow(Db,[])
Ldb = watershed(Db);
figure(); imshow(label2rgb(Ldb))
bw2b = BWsegmentationb;
bw2b(Ldb == 0) = 0;
figure(); imshow(bw2b)
% Home stretch, now. Modify the distance transform so it only has minima at the desired locations, and then repeat the watershed steps above.
%watershed2
firstwatershed= firstwatershedpriim.CData;
firstwatershedbw= imbinarize(firstwatershed);
firstwatershedbw2 = ~bwareaopen(~firstwatershedbw,10);
figure(); imshow(firstwatershedbw2)
Db2 = -bwdist(~firstwatershedbw);
figure(); imshow(Db2,[])
Ldb2 = watershed(Db2);
figure(); imshow(label2rgb(Ldb2))
firstwatershedbw2 = firstwatershedbw;
firstwatershedbw2(Ldb2 == 0) = 0;
figure(); imshow(firstwatershedbw2)
Ld2b = watershed(D2b);
bw3b = BWsegmentationb;
bw3b(Ld2b == 0) = 0;
figure(); imshow(bw3b)

Hi Tugce,
I understand that you are attempting to apply the watershed transform again to enhance the separation. The method you have adopted is appropriate. However, you might be receiving incorrect results due to your use of the "imshowpair" function in your code. The "imshowpair" function is intended solely for visualization purposes and it produces an image object. Instead, you should directly utilize the "imimposemin" function on the distance transform to impose the minima. This function yields the reconstructed image as a numeric array. The revised code will appear as follows:
% Invert the binary image and remove small objects
bw2b = ~bwareaopen(~BWsegmentationb, 10);
figure(); imshow(bw2b); title('Inverted Binary Image after Removing Small Objects');
% Compute the distance transform of the inverted binary image
Db = -bwdist(~bw2b);
figure(); imshow(Db, []); title('Distance Transform of Inverted Binary Image');
% Perform the watershed transform on the distance transform
Ldb = watershed(Db);
figure(); imshow(label2rgb(Ldb)); title('Initial Watershed Transform Result');
% Modify the original binary image using the watershed lines
bw2b = BWsegmentationb;
bw2b(Ldb == 0) = 0;
figure(); imshow(bw2b); title('Binary Image after Applying Initial Watershed Lines');
% Identify extended minima within the distance transform
% Display the overlay of extended minima on the original binary image
figure(); imshowpair(BWsegmentationb, maskb, 'blend'); title('Overlay of Extended Minima');
% Modify the distance transform so it only has minima at the desired locations
figure(); imshow(D2b, []); title('Modified Distance Transform with Imposed Minima');
% Perform the watershed transform on the modified distance transform
Ld2b = watershed(D2b);
figure(); imshow(label2rgb(Ld2b)); title('Refined Watershed Transform Result');
% Use the new watershed lines to modify the original binary image
bw3b = BWsegmentationb;
bw3b(Ld2b == 0) = 0;
figure(); imshow(bw3b); title('Final Segmentation after Refining Watershed');
This would achieve your intended results. If you need to repeat the watershed transform again, you can follow a similar procedure.
1. "imshowpair" function:https://www.mathworks.com/help/images/ref/imshowpair.html
2. "imimpose" function:https://www.mathworks.com/help/images/ref/imimposemin.html
I hope it helps.

### Categories

Find more on Images in Help Center and File Exchange

R2022a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!