MATLAB image segmentation algorithm Source code
1. Image inversion
The MATLAB program is implemented as follows:
I=imread (' xian.bmp ');
J=double (I);
j=-j+ (256-1); % Image Inversion Linear transformation
H=uint8 (J);
Subplot (1,2,1), Imshow (I);
Subplot (1,2,2), Imshow (H);
2. Gray-Scale linear transformation
The MATLAB program is implemented as follows:
I=imread (' xian.bmp ');
Subplot (2,2,1), Imshow (I);
Title (' original image ');
Axis ([50,250,50,200]);
Axis on; % Display coordinate system
I1=rgb2gray (I);
Subplot (2,2,2), Imshow (I1);
Title (' Grayscale image ');
Axis ([50,250,50,200]);
Axis on; % Display coordinate system
J=imadjust (i1,[0.1 0.5],[]); % partial stretching, stretching [0.1 0.5] to [0 1]
Subplot (2,2,3), Imshow (J);
Title (' Linear transform image [0.1 0.5] ');
Axis ([50,250,50,200]);
Grid on; % Display grid lines
Axis on; % Display coordinate system
K=imadjust (i1,[0.3 0.7],[]); % partial stretching, stretching [0.3 0.7] to [0 1]
Subplot (2,2,4), Imshow (K);
Title (' Linear transform image [0.3 0.7] ');
Axis ([50,250,50,200]);
Grid on; % Display grid lines
Axis on; % Display coordinate system
3. Nonlinear transformations
The MATLAB program is implemented as follows:
I=imread (' xian.bmp ');
I1=rgb2gray (I);
Subplot (1,2,1), Imshow (I1);
Title (' Grayscale image ');
Axis ([50,250,50,200]);
Grid on; % Display grid lines
Axis on; % Display coordinate system
J=double (I1);
j=40* (log (j+1));
H=uint8 (J);
Subplot (1,2,2), Imshow (H);
Title (' Logarithmic transform image ');
Axis ([50,250,50,200]);
Grid on; % Display grid lines
Axis on; % Display coordinate system
4. Histogram equalization
The MATLAB program is implemented as follows:
I=imread (' xian.bmp ');
I=rgb2gray (I);
Figure
Subplot (2,2,1);
Imshow (I);
Subplot (2,2,2);
Imhist (I);
I1=histeq (I);
Figure
Subplot (2,2,1);
Imshow (I1);
Subplot (2,2,2);
Imhist (I1);
5. Linear Smoothing Filter
Using MATLAB to implement the field averaging method to suppress the noise program:
I=imread (' xian.bmp ');
Subplot (231)
Imshow (I)
Title (' Original image ')
I=rgb2gray (I);
I1=imnoise (I, ' salt & Pepper ', 0.02);
Subplot (232)
Imshow (I1)
Title (' Add salt and pepper noise image ')
K1=filter2 (fspecial (' Average ', 3), I1)/255; % for 3*3 template smoothing filtering
K2=filter2 (fspecial (' Average ', 5), I1)/255; % for 5*5 Template Smoothing Filter k3=filter2 (fspecial (' average ', 7), I1)/255; % for 7*7 template smoothing filtering
K4=filter2 (fspecial (' average ', 9), I1)/255; % for 9*9 template smoothing filtering
Subplot (233), imshow (K1), title (' 3*3 Template smoothing filter ');
Subplot (234), Imshow (K2), title (' 5*5 Template smoothing filter ');
Subplot (235), Imshow (K3), title (' 7*7 Template smoothing filter ');
Subplot (236), Imshow (K4), title (' 9*9 Template smoothing filter ');
6. Median filter
Using MATLAB to implement the median filter program is as follows:
I=imread (' xian.bmp ');
I=rgb2gray (I);
J=imnoise (I, ' Salt&pepper ', 0.02);
Subplot (231), imshow (I); title (' Original image ');
Subplot (232), Imshow (J); title (' Add salt and pepper noise image ');
K1=MEDFILT2 (J); % for 3*3 Template median filter
K2=MEDFILT2 (j,[5,5]); % for 5*5 Template median filter
K3=MEDFILT2 (j,[7,7]); % for 7*7 Template median filter
K4=MEDFILT2 (j,[9,9]); % for 9*9 Template median filter
Subplot (233), imshow (K1), title (' 3*3 template median filter ');
Subplot (234), Imshow (K2), title (' 5*5 template median filter ');
Subplot (235), Imshow (K3), title (' 7*7 template median filter ');
Subplot (236), Imshow (K4), title (' 9*9 template median filter ');
7. Sharpen the image with the Sobel operator and Laplace:
I=imread (' xian.bmp ');
Subplot (2,2,1), Imshow (I);
Title (' original image ');
Axis ([50,250,50,200]);
Grid on; % Display grid lines
Axis on; % Display coordinate system
I1=IM2BW (I);
Subplot (2,2,2), Imshow (I1);
Title (' binary image ');
Axis ([50,250,50,200]);
Grid on; % Display grid lines
Axis on; % Display coordinate system
H=fspecial (' Sobel '); % Select Sobel operator
J=filter2 (H,I1); % convolution operation
Subplot (2,2,3), Imshow (J);
Title (' Sobel operator sharpening image ');
Axis ([50,250,50,200]);
Grid on; % Display grid lines
Axis on; % Display coordinate system
H=[0 1 0,1-4 1,0 1 0]; % Laplace operator
J1=conv2 (i1,h, ' same '); % convolution operation
Subplot (2,2,4), Imshow (J1);
Title (' Laplace operator sharpening image ');
Axis ([50,250,50,200]);
Grid on; % Display grid lines
Axis on; % Display coordinate system
8. Gradient operator Detection Edge
Use MATLAB to achieve the following:
I=imread (' xian.bmp ');
Subplot (2,3,1);
Imshow (I);
Title (' original image ');
Axis ([50,250,50,200]);
Grid on; % Display grid lines
Axis on; % Display coordinate system
I1=IM2BW (I);
Subplot (2,3,2);
Imshow (I1);
Title (' binary image ');
Axis ([50,250,50,200]);
Grid on; % Display grid lines
Axis on; % Display coordinate system
I2=edge (I1, ' Roberts ');
Figure
Subplot (2,3,3);
Imshow (I2);
Title (' Roberts operator segmentation result ');
Axis ([50,250,50,200]);
Grid on; % Display grid lines
Axis on; % Display coordinate system
I3=edge (I1, ' Sobel ');
Subplot (2,3,4);
Imshow (I3);
Title (' Sobel operator segmentation result ');
Axis ([50,250,50,200]);
Grid on; % Display grid lines
Axis on; % Display coordinate system
I4=edge (I1, ' Prewitt ');
Subplot (2,3,5);
Imshow (I4);
Title (' Prewitt operator segmentation result ');
Axis ([50,250,50,200]);
Grid on; % Display grid lines
Axis on; % Display coordinate system
9.LOG operator Detection Edge
Using MATLAB program to achieve the following:
I=imread (' xian.bmp ');
Subplot (2,2,1);
Imshow (I);
Title (' original image ');
I1=rgb2gray (I);
Subplot (2,2,2);
Imshow (I1);
Title (' Grayscale image ');
I2=edge (I1, ' log ');
Subplot (2,2,3);
Imshow (I2);
Title (' Log operator segmentation result ');
10.Canny operator Detection Edge
Using MATLAB program to achieve the following:
I=imread (' xian.bmp ');
Subplot (2,2,1);
Imshow (I);
Title (' Original image ')
I1=rgb2gray (I);
Subplot (2,2,2);
Imshow (I1);
Title (' Grayscale image ');
I2=edge (I1, ' canny ');
Subplot (2,2,3);
Imshow (I2);
Title (' Canny operator segmentation result ');
11. Boundary Tracking (bwtraceboundary function)
Clc
Clear All
I=imread (' xian.bmp ');
Figure
Imshow (I);
Title (' original image ');
I1=rgb2gray (I); % Convert color image to grayscale image
Threshold=graythresh (I1); % calculates the threshold required to convert a grayscale image to a two-value image
BW=IM2BW (I1, Threshold); % convert grayscale images to two-value images
Figure
Imshow (BW);
Title (' binary image ');
Dim=size (BW);
Col=round (Dim (2)/2)-90; % calculates the starting point column coordinates
Row=find (BW (:, col), 1); % calculate starting point line coordinates
connectivity=8;
num_points=180;
Contour=bwtraceboundary (Bw,[row,col], ' N ', connectivity,num_points);
% Extraction Boundary
Figure
Imshow (I1);
Hold on;
Plot (Contour (:, 2), Contour (:, 1), ' G ', ' linewidth ', 2);
Title (' Boundary tracking image ');
12.Hough transformations
i= imread (' xian.bmp ');
Roti=rgb2gray (I);
Subplot (2,2,1);
Imshow (RotI);
Title (' Grayscale image ');
Axis ([50,250,50,200]);
Grid on;
Axis on;
Bw=edge (RotI, ' Prewitt ');
Subplot (2,2,2);
Imshow (BW);
Title (' Prewitt operator edge detection image ');
Axis ([50,250,50,200]);
Grid on;
Axis on;
[H,t,r]=hough (BW);
Subplot (2,2,3);
Imshow (h,[], ' XData ', T, ' Ydata ', R, ' initialmagnification ', ' fit ');
Title (' Hough Transform graph ');
Xlabel (' \theta '), Ylabel (' \rho ');
Axis on, axis normal, hold on;
P=houghpeaks (h,5, ' threshold ', ceil (0.3*max (H (:))));
X=t (P (:, 2)); Y=r (P (:, 1));
Plot (x, y, ' s ', ' color ', ' white ');
Lines=houghlines (bw,t,r,p, ' Fillgap ', 5, ' MinLength ', 7);
Subplot (2,2,4);, Imshow (RotI);
Title (' Hough Transform image detection ');
Axis ([50,250,50,200]);
Grid on;
Axis on;
Hold on;
max_len=0;
For K=1:length (lines)
Xy=[lines (k). Point1;lines (k). Point2];
Plot (XY (:, 1), XY (:, 2), ' LineWidth ', 2, ' Color ', ' green ');
Plot (XY), XY (+), ' x ', ' LineWidth ', 2, ' Color ', ' yellow ');
Plot (XY (2,1), XY (2,2), ' x ', ' LineWidth ', 2, ' Color ', ' red ');
Len=norm (lines (k). Point1-lines (k). Point2);
if (Len>max_len)
Max_len=len;
Xy_long=xy;
End
End
Plot (Xy_long (:, 1), Xy_long (:, 2), ' LineWidth ', 2, ' Color ', ' cyan ');
13. Histogram threshold Value method
Using MATLAB to realize the histogram threshold method:
I=imread (' xian.bmp ');
I1=rgb2gray (I);
Figure
Subplot (2,2,1);
Imshow (I1);
Title (' Grayscale image ')
Axis ([50,250,50,200]);
Grid on; % Display grid lines
Axis on; % Display coordinate system
[M,n]=size (I1); % Measurement image size parameters
Gp=zeros (1,256); % pre-create vectors that hold the probability of grayscale occurrence
For k=0:255
GP (k+1) =length (Find (I1==k))/(M*n); % calculates the probability of each level of grayscale appearing in the corresponding position in GP
End
Subplot (2,2,2), bar (0:255,GP, ' g ')% plot histogram
Title (' Grayscale histogram ')
Xlabel (' grayscale value ')
Ylabel (' Probability of occurrence ')
I2=IM2BW (i,150/255);
Subplot (2,2,3), Imshow (I2);
Title (' Segmented image of threshold 150 ')
Axis ([50,250,50,200]);
Grid on; % Display grid lines
Axis on; % Display coordinate system
I3=IM2BW (i,200/255); %
Subplot (2,2,4), Imshow (I3);
Title (' Segmented image of threshold 200 ')
Axis ([50,250,50,200]);
Grid on; % Display grid lines
Axis on; % Display coordinate system
14. Automatic Threshold Method: Otsu method
Using MATLAB to implement the Otsu algorithm:
Clc
Clear All
I=imread (' xian.bmp ');
Subplot (1,2,1), Imshow (I);
Title (' Original image ')
Axis ([50,250,50,200]);
Grid on; % Display grid lines
Axis on; % Display coordinate system
Level=graythresh (I); % Determination of gray threshold value
BW=IM2BW (I,level);
Subplot (1,2,2), Imshow (BW);
Title (' Otsu method threshold segmentation image ')
Axis ([50,250,50,200]);
Grid on; % Display grid lines
Axis on; % Display coordinate system
15. Expansion operation
I=imread (' xian.bmp '); % load Image
I1=rgb2gray (I);
Subplot (1,2,1);
Imshow (I1);
Title (' Grayscale image ')
Axis ([50,250,50,200]);
Grid on; % Display grid lines
Axis on; % Display coordinate system
Se=strel (' disk ', 1); % generate circular structural elements
I2=imdilate (I1,SE); % expands the image with the resulting structural elements
Subplot (1,2,2);
Imshow (I2);
Title (' expanded image ');
Axis ([50,250,50,200]);
Grid on; % Display grid lines
Axis on; % Display coordinate system
16. Corrosion operation
MATLAB for corrosive operation
I=imread (' xian.bmp '); % load Image
I1=rgb2gray (I);
Subplot (1,2,1);
Imshow (I1);
Title (' Grayscale image ')
Axis ([50,250,50,200]);
Grid on; % Display grid lines
Axis on; % Display coordinate system
Se=strel (' disk ', 1); % generate circular structural elements
I2=imerode (I1,SE); % corrosion of images with generated structural elements
Subplot (1,2,2);
Imshow (I2);
Title (' corroded image ');
Axis ([50,250,50,200]);
Grid on; % Display grid lines
Axis on; % Display coordinate system
17. Opening and closing operations
Open and close operation with Matlab
I=imread (' xian.bmp '); % load Image
Subplot (2,2,1), Imshow (I);
Title (' original image ');
Axis ([50,250,50,200]);
Axis on; % Display coordinate system
I1=rgb2gray (I);
Subplot (2,2,2), Imshow (I1);
Title (' Grayscale image ');
Axis ([50,250,50,200]);
Axis on; % Display coordinate system
Se=strel (' disk ', 1); % uses a circle with a radius of 1 as the structural element
I2=imopen (I1,SE); % Open operation
I3=imclose (I1,SE); % closed operation
Subplot (2,2,3), Imshow (I2);
Title (' Open operation Image ');
Axis ([50,250,50,200]);
Axis on; % Display coordinate system
Subplot (2,2,4), Imshow (I3);
Title (' Closed Operation Image ');
Axis ([50,250,50,200]);
Axis on; % Display coordinate system
18. Open and close combination operation
I=imread (' xian.bmp '); % load Image
Subplot (3,2,1), Imshow (I);
Title (' original image ');
Axis ([50,250,50,200]);
Axis on; % Display coordinate system
I1=rgb2gray (I);
Subplot (3,2,2), Imshow (I1);
Title (' Grayscale image ');
Axis ([50,250,50,200]);
Axis on; % Display coordinate system
Se=strel (' disk ', 1);
I2=imopen (I1,SE); % Open operation
I3=imclose (I1,SE); % closed operation
Subplot (3,2,3), Imshow (I2);
Title (' Open operation Image ');
Axis ([50,250,50,200]);
Axis on; % Display coordinate system
Subplot (3,2,4), Imshow (I3);
Title (' Closed Operation Image ');
Axis ([50,250,50,200]);
Axis on; % Display coordinate system
Se=strel (' disk ', 1);
I4=imopen (I1,SE);
I5=imclose (I4,SE);
Subplot (3,2,5), Imshow (I5); % open-close operation image
Title (' Open-close operation Image ');
Axis ([50,250,50,200]);
Axis on; % Display coordinate system
I6=imclose (I1,SE);
I7=imopen (I6,SE);
Subplot (3,2,6), Imshow (I7); % closed-open operation image
Title (' Closed-open operation Image ');
Axis ([50,250,50,200]);
Axis on; % Display coordinate system
19. Morphological Boundary Extraction
Use MATLAB to achieve the following:
I=imread (' xian.bmp '); % load Image
Subplot (1,3,1), Imshow (I);
Title (' original image ');
Axis ([50,250,50,200]);
Grid on; % Display grid lines
Axis on; % Display coordinate system
I1=IM2BW (I);
Subplot (1,3,2), Imshow (I1);
Title (' binary image ');
Axis ([50,250,50,200]);
Grid on; % Display grid lines
Axis on; % Display coordinate system
I2=bwperim (I1); % gets the perimeter of the zone
Subplot (1,3,3), Imshow (I2);
Title (' Two value image ' at the perimeter of the border ');
Axis ([50,250,50,200]);
Grid on;
Axis on;
20. Morphological Skeleton Extraction
Use MATLAB to achieve the following:
I=imread (' xian.bmp ');
Subplot (2,2,1), Imshow (I);
Title (' original image ');
Axis ([50,250,50,200]);
Axis on;
I1=IM2BW (I);
Subplot (2,2,2), Imshow (I1);
Title (' binary image ');
Axis ([50,250,50,200]);
Axis on;
I2=bwmorph (I1, ' Skel ', 1);
Subplot (2,2,3), Imshow (I2);
Title (' 1-time skeleton extraction ');
Axis ([50,250,50,200]);
Axis on;
I3=bwmorph (I1, ' Skel ', 2);
Subplot (2,2,4), Imshow (I3);
Title (' 2-time skeleton extraction ');
Axis ([50,250,50,200]);
Axis on;
21. Direct extraction of four vertex coordinates
I = Imread (' xian.bmp ');
i = I (:,:, 1);
BW=IM2BW (I);
Figure
Imshow (~BW)
[X,y]=getpts
Paper 55: Image Segmentation Code Summary