Close all;
%%
%step 1: Grayscale image, Color * * *
RGB = Imread (' pears.png ');
I = Rgb2gray (RGB);
Figure;subplot (121)
Imshow (I)
%step 2: Using gradients to achieve image segmentation
% using the Sobel operator for edge detection,
Text (732,501, ' Image courtesy of Corel ', ' FontSize ', 7, ' HorizontalAlignment ', ' right ')
hy = fspecial (' Sobel ');
HX = Hy ';
Iy = IMFilter (double (I), HY, ' replicate ');% implements the linear spatial filter function, a kind of image enhancement method using filtering processing. Its theoretical basis is spatial convolution and spatial correlation. The aim is to improve image quality, including the removal of high frequency noise and interference, image edge enhancement, linear enhancement and de-blurring.
Ix = IMFilter (double (I), HX, ' replicate ');
Gradmag = sqrt (ix.^2 + iy.^2),% modulo
Subplot (122), Imshow (gradmag,[]), title (' Gradmag ')
% direct with watershed
%l=watershed (GRADMAG);
%lrgb=label2rgb (L);
%figure;imshow (LRGB),
%title (' Lrgb ')
%no. Without additional preprocessing, such as the following markup calculations, the direct result of using watershed transformations is often "over-segmented." ”
% below is the mark foreground and background object
The various programs can be applied here to find the foreground marker, which must be connected to the blob of pixels within each foreground object. In this example, you will use morphological techniques called "Open from Reconstruction" and "closed from reconstruction" to "clean". These actions create a flat maximum value that can be used to imregionalmax within each object.
%step 3: Morphological open operation
SE = Strel (' disk ', 20);% Circular structure element
Io = Imopen (I, se);% morphological open operation
Figure;subplot (121)
Imshow (IO), title (' io ')% shows the graph after execution
%step 4: Corrosion and reconstruction
Ie = Imerode (I, se),% corrosion of image
IOBR = Imreconstruct (Ie, I);% Rebuilding the image
Subplot (122); Imshow (IOBR),% display reconstructed image
Title (' Iobr ')
%step 5: Morphological close operation
Ioc = Imclose (Io, se);% morphological close operation
Figure;subplot (121)
Imshow (IOC),
Title (' Ioc ')
%step 6: Image expansion and negation
IOBRD = imdilate (iobr, SE),% expands the image
IOBRCBR = Imreconstruct (Imcomplement (IOBRD), Imcomplement (IOBR));
IOBRCBR = Imcomplement (IOBRCBR),% image negation
Subplot (122); Imshow (IOBRCBR),
Title (' Iobrcbr ')
%%step 7: Get local maximum value
FGM = Imregionalmax (IOBRCBR);% obtains local maximum value
Figure;imshow (FGM),
Title (' FGM ')
%step 8: Display the maximum area on the original
I2 = I;
I2 (FGM) = 255;% The pixel value at local maximum value is set to 255
Figure;imshow (I2),
Title (' FGM superimposed on original image ')% displays the maximum area on the original
Se2 = Strel (Ones (5,5));% build Element
FGM2 = Imclose (FGM, SE2);% off operation
FGM3 = Imerode (fgm2, se2);% corrosion
FGM4 = Bwareaopen (FGM3, 20);% open operation
%step 9: Show the modified maximum area
I3 = I;
I3 (FGM4) = 255;% foreground set to 255
Figure;subplot (121),
Imshow (I3)% shows the modified maximum area
Title (' Fgm4 superimposed on original image ')
% now marks the background, after cleaning the image, IOBRCBR, dark pixels belong to the background, so you can manipulate from a threshold.
%step 10: Converting to a two-value image
BW = IM2BW (IOBRCBR, Graythresh (IOBRCBR));
Subplot (122); Imshow (BW),
Title (' BW ')
The background pixel is black, but ideally, we don't want the background marker to be too close to the edge of our target object. We subdivide by ' skeletal ', divide the distance of the two value image, and then look for the boundary of the watershed.
%step 11:
D = Bwdist (BW);% calculated distance
DL = watershed (D);% Watershed Transformation
BGM = DL = = 0;% to find the split boundary
Figure Imshow (BGM),% shows the split border
Title (' Watershed Ridge Lines (BGM) ')
Gradmag2 = Imimposemin (Gradmag, BGM | fgm4);
L = watershed (gradmag2);% Watershed Transformation
I4 = I;
I4 (imdilate (L = = 0, ones (3, 3)) | bgm | fgm4) = 255;% foreground and boundary disposition 255
Figure Subplot (121)
Imshow (I4)% highlights foreground and boundary
Title (' Markers and object boundaries ')
Lrgb = Label2rgb (L, ' Jet ', ' w ', ' shuffle '),% converted to pseudo-color * * * *
Subplot (122); Imshow (LRGB)% display pseudo-color * * * * *
Title (' Colored Watershed label Matrix ')
Figure Imshow (I),
On
Himage = Imshow (LRGB),% showing pseudo-color image on original
Set (Himage, ' alphadata ', 0.3);
Title (' Lrgb superimposed transparently on original image ')
This article is from the "Gz7seven" blog, make sure to keep this source http://10734047.blog.51cto.com/10724047/1701827
Watershed algorithm MATLAB Programming code parsing