分水岭算法——MATLAB(Watershed algorithm - MATLAB)
分水岭算法——MATLAB(Watershed algorithm - MATLAB)
% 1. The Compute a segmentation function. This is an image whose dark regions are the objects you are trying to segment.
Calculate the partition function. The darker areas in the image are objects to be divided.
% 2. Compute foreground markers. These are connected blobs of pixels within each of the objects.
2. Calculate the foreground mark. These are the dot pixels of the internal connections of each object.
3 Compute background markers. These are pixels that are not part of any object.
Calculate the background flag. These are pixels that don't belong to any object.
% 4. Modify the segmentation function so that it only has minima at the foreground and background marker locations.
% 4. Modify the partition function to make it only have a minimum value in the foreground and backview mark position.
% 5. Compute the watershed transform of the modified segmentation function.
5. Calculate the watershed transformation for the modified partition function.
% Use by Matlab Image Processing Toolbox
% use MATLAB image processing toolbox
% note: during the period, many functions of image processing toolbox are used, such as fspecial, imfilter, watershed, label2rgb, imopen,
% imclose and imreconstruct, imcomplement, imregionalmax, bwareaopen, graythresh, imimposemin function and so on.
Read in the Color Image and Convert it to Grayscale
% the first step: read the color image and convert it to grayscale image
CLC. Clear all; Close all;
RGB = imread (' pears. PNG ');
If ndims (RGB) = = 3
I = rgb2gray (RGB);
The else
I = RGB.
The end
Figure (' units', 'position', 'position', [0, 0, 1]);
Subplot (1, 2, 1); Imshow (RGB); The title (' artwork ');
Subplot (1, 2, 2); Imshow (I); Title (' grayscale ');
Step 2: % Use the Gradient Magnitude as the Segmentation Function
% 2: the gradient is used as a partition function
% Use the Sobel edge, imfilter, and some simple products to compute the gradient.
The gradient is high at The borders of The object