A list,

Watershed algorithm is an image region segmentation method. In the segmentation process, images are transformed into grayscale images, and then GRAYscale values will be regarded as elevations, and water will be injected to lower points. For this topographic interpretation, three points are mainly considered:



Minimum point, the point at which the lowest point in a basin, when we drop a drop of water in the basin, will eventually converge to that point due to gravity. Note: there may be a minimum plane in which there are minimum points.

Other points in the basin where the droplets converge to the local minimum point.

At the edge of a basin, where it meets other basins, a drop of water will flow equally to any basin.



Once we understand these three points, we start filling the basin with water at the minimum point, and then as we fill the basin, each minimum point slowly spreads out, and then we know that the water in the two basins meets, and where it meets is the watershed that we want.

It can be intuitively understood from the figure below. Firstly, the three regions all contain minimum points



And then you fill it in and you get the watershed.



The dividing line can be obtained to complete image segmentation:

Ii. Source code

clear, close all;
clc;
%1.Read the image and find the image boundary. rgb = imread('tree.jpeg'); % Read original image I = rgb2gray(RGB); % into grayscale image figure; subplot(121Imshow (I) text(732.501.'Image courtesy of Corel'.'FontSize'.7.'HorizontalAlignment'.'right')
hy = fspecial('sobel'); %sobel operator, the application of sobel operator sharpen the image hx = HY';
Iy = imfilter(double(I), hy, 'replicate'); Ix = imfilter(Ix = imfilter(double(I), hx, 'replicate'); X direction edge gradmag =sqrt(Ix.^2 + Iy.^2); Touch the subplot (% o122); Imshow (gradmag,[]), %'Gradient magnitude (gradmag)')

%2.Direct use of gradient modulus for watershed algorithm :(there is often over-segmentation, the effect is not good) L = watershed(gradmag); % Directly apply watershed algorithm Lrgb = Label2RGB (L); % to color image figure; Imshow (Lrgb), % display the segmented image title('Watershed transform of gradient magnitude (Lrgb)')% over segmentation %3.Mark foreground and background separately: In this example, morphological reconstruction technique is used to mark foreground objects. First, open operation is used. After open operation, some small objects can be removed. Open and close operations can remove specific image details smaller than structural elements, while ensuring no global geometric distortion. % open operation can be smaller than the structural elements of the spike filter, cut off the slender lap and play a separate role; % closed operation can be smaller than the structural elements of the gap or hole filling, lap short discontinuity and play a role of connection. se = strel('disk'.4); % Circular structural elements,STREL('disk',R,N),R is the specified radius, When N is greater than 0, the disk-shaped structuring %element is approximated by a sequence of N Io = imopen(I, se); % morphology open operation figure; subplot(121) imshow(Io), % show the image after performing the open operation'Opening (Io)') Ie = imerode(I, se); Basic parameters: input image reconstruction and structural element object Iobr = IMreconstruct (Ie, I). % morphological reconstruction subplot(122); Imshow (Iobr), % display the reconstructed image title('Opening-by-reconstruction (Iobr)') Ioc = imclose(Io, se); % Morphological operation, first expansion, then corrosion, both operations use the same structural element figure; subplot(121) imshow(Ioc), % display image title('Opening-closing (Ioc)') Iobrd = imdilate(Iobr, se); % To inflate the image, basic parameters: input image to be processed and structure element object. Iobrcbr = imreconstruct(imcomplement(Iobrd), ... imcomplement(Iobr)); % morphological reconstruction = IMcomplement (Iobrcbr); % image inverse subplot(122); Imshow (Iobrcbr), % display the image after reconstruction,figure4
title('Opening-closing by reconstruction (Iobrcbr)')
%As you can see by comparing Iobrcbr with Ioc, 
%reconstruction-based opening and closing are more 
%effective than standard opening andclosing at removing %small blemishes without affecting the overall %shapes of the objects. Calculate the regional maxima  %of Iobrcbr to obtain good foreground markers. fgm= imregionalmax(Iobrcbr); % local maximum figure; Imshow (FGM), % shows the reconstructed local maximum image,figure5
title('Regional maxima of opening-closing by reconstruction (fgm)')
I2 = I; % Foreground marker overlay with original I2(FGM) =255; The pixel value at % local maximum is set to255figure; Imshow (I2), % shows the maximum region on the original image,figure6
title('Regional maxima superimposed on original image (I2)')
se2 = strel(ones(3.3)); % fgm2 = imclose(FGM, se2); % fgm3 = imerode(fgm2, se2); % corrosion fgm4 = bwareaopen(fgm3,20); % open I3 = I; I3(fgm4) =255; % the foreground is set to255
figure; subplot(121) imshow(I3)% shows the modified maximum value area,figure7
title('Modified regional maxima')
bw = im2bw(Iobrcbr, graythresh(Iobrcbr)); % to binary image subplot(122); Imshow (bw), % display binary image,figure7
title('Thresholded opening-closing by reconstruction')%4. Perform watershed transformation and display: D= bwdist(bw); % Calculated distance DL = watershed(D); % watershed BGM = DL ==0; % to find the partition boundaryCopy the code

3. Operation results

















Fourth, note

Version: 2014 a