Clear Filters
Clear Filters

how to repeat watershed transform to improve separation

2 views (last 30 days)
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
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)
maskb = imextendedmin(Db,2);
firstwatershedpriim= imshowpair(BWsegmentationb,maskb,'blend')
% Home stretch, now. Modify the distance transform so it only has minima at the desired locations, and then repeat the watershed steps above.
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)
maskb2 = imextendedmin(Db2,2);
figure(); imshowpair(firstwatershedbw,maskb2,'blend')
D2b = imimposemin(Db2,maskb2);
Ld2b = watershed(D2b);
bw3b = BWsegmentationb;
bw3b(Ld2b == 0) = 0;
figure(); imshow(bw3b)

Answers (1)

Avadhoot on 21 Dec 2023
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
maskb = imextendedmin(Db, 2);
figure(); imshow(maskb); title('Extended Minima');
% 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
D2b = imimposemin(Db, maskb);
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.
For more information about "imshowpair" function and "imimposemin" function, refer to the following documentation:
  1. "imshowpair" function:
  2. "imimpose" function:
I hope it helps.


Find more on Images in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!