From http://blog.sina.com.cn/s/blog_a4034b2801013q5t.html
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); % linear inversion of Image
H = uint8 (j );
Subplot (1, 2), imshow (I );
Subplot (1, 2), imshow (h );
2. gray 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), imshow (I1 );
Title ('grayscale image ');
Axis ([50,250, 50,200]);
Axis on; % display Coordinate System
J = imadjust (I1, [0.1 0.5], []); % partial stretch, the gray scale in [0.1 0.5] is [0 1]
Subplot (2, 2, 3), imshow (j );
Title ('linearly transformed 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 stretch, the gray scale in [0.3 0.7] is [0 1]
Subplot (2, 2, 4), imshow (k );
Title ('linearly transformed image [0.3 0.7] ');
Axis ([50,250, 50,200]);
Grid on; % display grid lines
Axis on; % display Coordinate System
3. nonlinear transformation
The MATLAB program is implemented as follows:
I =imread('xian.bmp ');
I1 = rgb2gray (I );
Subplot (1, 2), 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), imshow (h );
Title ('logarithm transformed 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 );
Imhist (I );
I1 = histeq (I );
Figure;
Subplot (2, 2, 1 );
Imshow (I1 );
Subplot (2, 2 );
Imhist (I1 );
5. linear smoothing filter
Use MATLAB to implement a Domain Average Method to suppress noise program:
I =imread('xian.bmp ');
Subplot (231)
Imshow (I)
Title ('original image ')
I = rgb2gray (I );
I1 = imnoise (I, 'sale & pepper', 0.02 );
Subplot (232)
Imshow (I1)
Title ('add prepaid noise image ')
K1 = filter2 (fspecial ('average', 3), i1)/255; % perform 3*3 Template smoothing filtering
K2 = filter2 (fspecial ('average', 5), i1)/255; % Perform 5*5 template smooth filtering K3 = filter2 (fspecial ('average', 7 ), i1)/255; % for 7*7 Template smooth Filtering
K4 = filter2 (fspecial ('average', 9), i1)/255; % perform 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 smooth filter ');
Subplot (236), imshow (K4); Title ('9*9 template smooth filter ');
6. Median Filter
The median filtering program using Matlab is as follows:
I =imread('xian.bmp ');
I = rgb2gray (I );
J = imnoise (I, 'sale & pepper', 0.02 );
Subplot (231), imshow (I); Title ('original image ');
Subplot (232), imshow (j); Title ('add prepaster noise image ');
K1 = medfilt2 (j); % perform 3*3 Template Median Filter
K2 = medfilt2 (J, [5, 5]); % Perform 5*5 template Median Filter
K3 = medfilt2 (J, [7, 7]); % perform template Median Filter
K4 = medfilt2 (J, [9, 9]); % perform 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 medialist ');
7. Sharpen the image using 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), imshow (I1 );
Title ('binary image ');
Axis ([50,250, 50,200]);
Grid on; % display grid lines
Axis on; % display Coordinate System
H = fspecial ('sobel '); % select the Sobel operator
J = filter2 (H, i1); % convolution
Subplot (2, 2, 3), imshow (j );
Title ('sobel operator sharpen image ');
Axis ([50,250, 50,200]);
Grid on; % display grid lines
Axis on; % display Coordinate System
H = [0 1 0, 1-4, 0 1 0]; % LAPLACE OPERATOR
J1 = conv2 (I1, H, 'same'); % convolution
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 Edge Detection
Use MATLAB to implement 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 );
Imshow (I1 );
Title ('binary image ');
Axis ([50,250, 50,200]);
Grid on; % display grid lines
Axis on; % display Coordinate System
I2 = edge (I1, 'ots ');
Figure;
Subplot (2, 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 Edge Detection
The MATLAB program is implemented as follows:
I =imread('xian.bmp ');
Subplot (2, 2, 1 );
Imshow (I );
Title ('original image ');
I1 = rgb2gray (I );
Subplot (2, 2 );
Imshow (I1 );
Title ('grayscale image ');
I2 = edge (I1, 'log ');
Subplot (2, 2, 3 );
Imshow (I2 );
Title ('Log operator segmentation result ');
10. Detect the edge using the 'yoush' Operator
The MATLAB program is implemented as follows:
I =imread('xian.bmp ');
Subplot (2, 2, 1 );
Imshow (I );
Title ('original image ')
I1 = rgb2gray (I );
Subplot (2, 2 );
Imshow (I1 );
Title ('grayscale image ');
I2 = edge (I1, 'hangzhou ');
Subplot (2, 2, 3 );
Imshow (I2 );
Title ');
11. Border Tracking (bwtraceboundary function)
CLC
Clear all
I =imread('xian.bmp ');
Figure
Imshow (I );
Title ('original image ');
I1 = rgb2gray (I); % convert a color image into a grayscale image
Threshold = graythresh (I1); % calculate the threshold required to convert a grayscale image to a binary image
BW = im2bw (I1, threshold); % convert the grayscale image to a binary image
Figure
Imshow (BW );
Title ('binary image ');
Dim = size (BW );
Col = round (DIM (2)/2)-90; % calculate the coordinate of the Start Column
Row = find (BW (:, col), 1); % calculates the starting point row coordinate
Connectivity = 8;
Number_points = 180;
Contour = bwtraceboundary (BW, [row, Col], 'n', connectivity, num_points );
% Extract Boundary
Figure
Imshow (I1 );
Hold on;
Plot (contour (:, 2), contour (:, 1), 'G', 'linewidth', 2 );
Title ('boundary tracking image ');
12. Hough Transformation
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 );
Imshow (BW );
Title ('prewitt Operator Edge Detection post-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 ('hov transform fig ');
Xlabel ('\ Theta'), ylabel ('\ ro ');
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 ('hov Transform Image moderation ');
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 (1, 1), XY (1, 2), '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', 'cya ');
13. Histogram Threshold Method
Use MATLAB to implement 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); % measure image size parameters
Gp = zeros (1,256); % pre-created vector for storing the gray Probability
For k = 0: 255
GP (k + 1) = length (find (I1 = k)/(m * n); % calculate the probability of gray scale occurrence of each level and store it in the corresponding position of GP
End
Subplot (, 2), bar (, GP, 'G') % plot the Histogram
Title ('grayscale histograms ')
Xlabel ('grayscale value ')
Ylabel ('probability of occurrence ')
I2 = im2bw (I, 150/255 );
Subplot (2, 2, 3), imshow (I2 );
Title ('image segmentation with a threshold of 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 ('image segmentation with a threshold of 200 ')
Axis ([50,250, 50,200]);
Grid on; % display grid lines
Axis on; % display Coordinate System
14. Automatic threshold method: Otsu Method
Use MATLAB to implement the Otsu algorithm:
CLC
Clear all
I =imread('xian.bmp ');
Subplot (1, 2), imshow (I );
Title ('original image ')
Axis ([50,250, 50,200]);
Grid on; % display grid lines
Axis on; % display Coordinate System
Level = graythresh (I); % determine the grayscale threshold
BW = im2bw (I, level );
Subplot (1, 2), imshow (BW );
Title ('otsu-based threshold-based image segmentation ')
Axis ([50,250, 50,200]);
Grid on; % display grid lines
Axis on; % display Coordinate System
15. Expansion operation
I =imread('xian.bmp '); % load the image
I1 = rgb2gray (I );
Subplot (1, 2 );
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 a circular structure element
I2 = imdilate (I1, SE); % expands the image with the generated structural element
Subplot (1, 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 implements corrosion operations
I =imread('xian.bmp '); % load the image
I1 = rgb2gray (I );
Subplot (1, 2 );
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 a circular structure element
I2 = imerode (I1, SE); % use the generated structure element to corrode the image
Subplot (1, 2 );
Imshow (I2 );
Title ('post-corrosive image ');
Axis ([50,250, 50,200]);
Grid on; % display grid lines
Axis on; % display Coordinate System
17. Enable and disable operations
Enabling and closing operations with MATLAB
I =imread('xian.bmp '); % load the 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), 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); % enable operation
I3 = imclose (I1, SE); % Close Operation
Subplot (2, 2, 3), imshow (I2 );
Title ('Operation-enabled image ');
Axis ([50,250, 50,200]);
Axis on; % display Coordinate System
Subplot (2, 2, 4), imshow (I3 );
Title ('closed image after computation ');
Axis ([50,250, 50,200]);
Axis on; % display Coordinate System
18. Enable and close the Combination Operation
I =imread('xian.bmp '); % load the 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); % enable operation
I3 = imclose (I1, SE); % Close Operation
Subplot (3, 2, 3), imshow (I2 );
Title ('Operation-enabled image ');
Axis ([50,250, 50,200]);
Axis on; % display Coordinate System
Subplot (3, 2, 4), imshow (I3 );
Title ('closed image after computation ');
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 implement the following:
I =imread('xian.bmp '); % load the image
Subplot (1, 3), 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 ('binarization image ');
Axis ([50,250, 50,200]);
Grid on; % display grid lines
Axis on; % display Coordinate System
I2 = bwperim (I1); % get the perimeter of the Region
Subplot (1, 3), imshow (I2 );
Title ('boundary perimeter binary image ');
Axis ([50,250, 50,200]);
Grid on;
Axis on;
20. Morphological Skeleton Extraction
Use MATLAB to implement 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), imshow (I1 );
Title ('binary image ');
Axis ([50,250, 50,200]);
Axis on;
I2 = bwmorph (I1, 'skel', 1 );
Subplot (2, 2, 3), imshow (I2 );
Title ('one-time skeleton extraction ');
Axis ([50,250, 50,200]);
Axis on;
I3 = bwmorph (I1, 'skel', 2 );
Subplot (2, 2, 4), imshow (I3 );
Title ('twice extracted skeleton ');
Axis ([50,250, 50,200]);
Axis on;
21. Extract the coordinates of four vertices directly
I = imread('xian.bmp ');
I = I (:,:, 1 );
BW = im2bw (I );
Figure
Imshow (~ BW)
[X, y] = getpts