watershed segmentation: smooth boundaries between objects

14 Ansichten (letzte 30 Tage)
Sebastian Kalt
Sebastian Kalt am 17 Dez. 2021
Beantwortet: Prasanna am 20 Feb. 2024
Hi guys,
I'm trying to separate objects using the watershed segmentation algorithm. However, the best results, that I was able to get, were object boundaries consisting of horizontal, vertical, and diagonal segments as shown in the image below. In some cases, this works just fine but in others like the one shown below the resulting boundary does not really match the real one.
If you use the watershed algorithm implemented in ImageJ, for example, the resulting boundaries look much smoother and hence closer to reality. Do you have an idea how this could be realized in MATLAB as well? This would be really helpful.
Have a great day and thank you in advance for your help.

Antworten (1)

Prasanna
Prasanna am 20 Feb. 2024
Hi Sebastian,
It is my understanding that you would like to know how to perform watershed segmentation of an image using MATLAB and how to improve the obtained segmentation results.
The watershed transform finds catchment basins in an image by treating it as a surface where light pixels represent high elevations and dark pixels represent low elevations. The watershed transform can be used to segment contiguous regions of interest into distinct objects. You can perform watershed segmentation in MATLAB using the “watershed” method present. For more information on the “watershed” transform, refer to the following link: https://www.mathworks.com/help/images/ref/watershed.html
Sometimes, the watershed segmentation algorithm produces boundaries that are not smooth due to the nature of the algorithm, which segments the image based on the gradient magnitude. However, you can improve the smoothness of the boundaries by preprocessing your image and adjusting the algorithm's application. The below workflow helps achieve a better segmentation using MATLAB:
I = imread('your_image.jpg');
% Apply a smoothing filter to reduce noise and create a more continuous gradient.
smoothedImg = imgaussfilt(I, sigma); %'sigma' controls the degree of smoothing
% Compute the gradient magnitude of the smoothed image.
% This will serve as the basis for the watershed segmentation.
gmag = imgradient(smoothedImg);
% Implement marker-controlled watershed. This involves defining markers for the objects and
% background to guide the segmentation.
% Foreground markers
se2 = strel('disk', 1);
fg = imerode(I, se2);
fg = imbinarize(fg);
% Background markers
bg = imdilate(I, se2);
bg = imbinarize(bg);
bg = bwareaopen(bg, 10); % Remove small objects (noise)
% Modify the gradient image
gmag2 = imimposemin(gmag, bg | fg);
% Watershed segmentation
L = watershed(gmag2);
% edge detection to refine the boundaries after the watershed segmentation.
edges = edge(smoothedImg, 'Canny');
% Superimpose edges on the segmented image
L(edges) = max(L(:)) + 1;
In the above workflow, we initially smoothen the image using a gaussian blur. We then compute the gradient magnitude of the smoothed image to serve as the basis of watershed transform. Then, instead of applying the watershed algorithm directly to the gradient image, use marker-controlled watershed. This involves defining markers for the objects and background to guide the segmentation. Optionally, you can use edge detection to refine the boundaries before or after the watershed segmentation. These steps should help you achieve smoother boundaries like the ones you see in ImageJ.
For further documentation on watershed transform and improved image segmentation, you can refer to the following links:
Hope this helps.
Regards,
Prasanna

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by