The basis of image segmentation algorithm and implementation examples
Reprinted from http://www.elecfans.com/emb/app/20171218604957.html
Image segmentation is the specific image into a plurality of areas with unique properties and propose techniques and procedures object of interest. It is a critical step in the process by the image analysis to the image. Conventional image segmentation method is divided into the following categories:
Segmentation threshold based region segmentation method based on segmentation and edge segmentation method based on a particular theory. Since 1998, researchers continue to improve the existing image segmentation and put some new theories and methods from other disciplines for image segmentation, we made a lot of new segmentation method. After the extracted target image segmentation may be used to identify semantic image, like the image search field.
Image segmentation as yet no general theory itself. With the proposed new theories and methods of many disciplines, there have been many image segmentation methods and specific theory, method of combining.
The basis of image segmentation algorithm and implementation examples
Recent project involves the field of image processing, a little research about it, while collecting data to achieve a number of basic functions.
First, the image reversal
[plain] view plain copyI=imread(‘input_image.jpg’);
J=double(I);
J = -J + (256-1);% image reversal linear transformation
H=uint8(J);
subplot(3,3,4),imshow(H);
TI TLE ( 'image reversal linear transformation');
axis([50,250,50,200]);
axis on;
Second, the linear transformation gradation
[plain] view plain copyI=imread(‘input_image.jpg’);
subplot(3,3,1),imshow(I);
TI TLE ( 'original image');
axis([50,250,50,200]);
axis on;
I1 = rgb2gray(I);
subplot(3,3,2),imshow(I1)
TI TLE ( "gradation image")
axis([50,250,50,200]);
grid on;
axis on;
K=imadjust(I1,[0.3 0.7],[]);
subplot(3,3,3),imshow(K);
TI TLE ( 'linearly transformed image [0.3 0.7]');
axis([50,250,20,200]);
grid on;
axis on;
Third, non-linear transformation
[plain] view plain copyI=imread(‘input_image.jpg’);
I1 = rgb2gray(I);
subplot(3,3,5),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(3,3,6),imshow(H);
title ( 'logarithmic transformed images');
axis([50,250,50,200]);
grid on;% display grid lines
axis on;% display coordinate system
Results above code:
Fourth, the histogram equalization
[plain] view plain copyI=imread(‘input_image.jpg’);
figure;
I = rgb2gray (I);
subplot(2,2,1);
imshow(I);
subplot(2,2,2);
imhist(I);
title ( 'image histogram equalization');
I1 = histeq (I);
subplot(2,2,3);
imshow(I1);
subplot(2,2,4);
imhist (I1);
Results above code:
Fifth, the linear smoothing filter
[plain] view plain copyI=imread(‘input_image.jpg’);
figure;
subplot(231)
imshow(I)
title ( 'original image')
I = rgb2gray (I);
I1=imnoise(I,‘salt & pepper’,0.02);
subplot(232)
imshow(I1)
title ( 'salt and pepper noise added image')
k1 = filter2 (fspecial ( 'average', 3), I1) / 255;% 3 * 3 templates for smoothing
k2 = filter2 (fspecial ( 'average', 5), I1) / 255;% * 5 5 for smoothing Template
k3 = filter2 (fspecial ( 'average', 7), I1) / 255;% 7 * 7 for smoothing Template
k4 = filter2 (fspecial ( 'average', 9), I1) / 255;% 9 * 9 for smoothing Template
subplot (233), imshow (k1); title ( '3 * 3 template smoothing');
subplot (234), imshow (k2); title ( '5 * 5 smoothing template');
subplot (235), imshow (k3); title ( '7 * 7 smoothing template');
subplot (236), imshow (k4); title ( '9 * 9 template smoothing');
Results above code:
Six, median filter
[plain] view plain copyfigure;
I=imread(‘input_image.jpg’);
I = rgb2gray (I);
subplot(231),imshow(I);
title ( 'original image');
J=imnoise(I,‘salt & pepper’,0.02);
subplot(232),imshow(J);
title ( 'salt and pepper noise added image');
k1 = medfilt2 (J);% for 3 * 3 median filter template
k2 = medfilt2 (J, [5,5]);% for 5 * 5 median filter template
k3 = medfilt2 (J, [7,7]);% 7 * 7 for median filtering template
k4 = medfilt2 (J, [9,9]);% 9 * 9 for median filtering template
subplot (233), imshow (k1); title ( '3 * 3 median filter template');
subplot (234), imshow (k2); title ( '5 * 5 median filter template');
subplot (235), imshow (k3); title ( '7 * 7 median filter template');
subplot (236), imshow (k4); title ( '9 * 9 median filtering template');
Results above code:
Seven, Sobel operator and Laplacian image sharpening
[plain] view plain copyfigure;
I=imread(‘input_image.jpg’);
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 sharpened image');
axis([50,250,50,200]);
grid on;% display grid lines
axis on;% display coordinate system
I1 = double(I1);
h = [0 1 0,1 -4 1,0 1 0];% Laplacian
J1 = conv2 (I1, h, 'same');% convolution operation
subplot(2,2,4),imshow(J1);
title ( 'Laplacian image sharpening');
axis([50,250,50,200]);
grid on;% display grid lines
axis on;% display coordinate system
Results above code:
Eight, gradient edge detection operator
[plain] view plain copyfigure;
I=imread(‘input_image.jpg’);
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’);
subplot(2,3,3);
imshow(I2);
title ( 'roberts operator division 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 division 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 division result');
axis([50,250,50,200]);
grid on;% display grid lines
axis on;% display coordinate system
Nine, LOG edge detection operator
[plain] view plain copyI1=rgb2gray(I);
I2=edge(I1,‘log’);
subplot(2,3,6);
imshow(I2);
title ( 'log operator division result');
Results above code:
Ten, Canny edge detection operator
[plain] view plain copyfigure;
I=imread(‘input_image.jpg’);
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 division result');
Results above code:
XI boundary tracking (bwtraceboundary function)
[plain] view plain copyI=imread(‘input_image.jpg’);
figure
subplot(2,2,1);
imshow(I);
title ( 'original image');
I1 = rgb2gray (I);% conversion of the color image is a grayscale image
threshold = graythresh (I1);% calculated grayscale image into a binary image of a desired threshold
BW = im2bw (I1, threshold);% grayscale image into a binary image
subplot(2,2,2);
imshow(BW);
title ( 'binary image');
dim=size(BW);
col = round (dim (2) / 2) -90;% calculated starting column coordinate
row = find (BW (:, col), 1);% calculated coordinates of the starting point of the line
connectivity=8;
num_points=180;
contour=bwtraceboundary(BW,[row,col],‘N’,connectivity,num_points);
% Extraction border
subplot(2,2,3);
imshow(I1);
hold on;
plot(contour(:,2),contour(:,1), ‘g’,‘LineWidth’ ,2);
title ( 'boundary tracking image ");
Results above code:
Twelve, Hough transform
[plain] view plain copyfigure;
I=imread(‘input_image.jpg’);
rotI=rgb2gray(I);
subplot(2,2,1);
imshow (bread);
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 after 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 in FIG.');
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 detector');
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’,‘cyan’);
Results above code:
XIII histogram method
[plain] view plain copyfigure;
I=imread(‘input_image.jpg’);
I1=rgb2gray(I);
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);% measured image size parameter
GP = zeros (1,256);% pre-stored vector to create the probability gradation
for k=0:255
GP (k + 1) = length (find (I1 == k)) / (m * n);% calculated probability of occurrence of each gray level, which is stored in the corresponding position GP
end
subplot (2,2,2), bar (0: 255, GP, 'g')% histogrammed
title ( 'histogram')
the xlabel ( 'gradation values')
ylabel ( 'probability of occurrence')
I2=im2bw(I,150/255);
subplot(2,2,3),imshow(I2);
title ( 'thresholding the image 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 ( 'thresholding image 200')
axis([50,250,50,200]);
grid on;% display grid lines
axis on;% display coordinate system
Results above code:
XIV Automatic Threshold: Otsu method
[plain] view plain copyclc
clear all
figure;
I=imread(‘input_image.jpg’);
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);% gray threshold value is determined
BW=im2bw(I,level);
subplot(1,2,2),imshow(BW);
title ( 'Otsu thresholding method of image')
axis([50,250,50,200]);
grid on;% display grid lines
axis on;% display coordinate system
Results above code:
XV dilation
[plain] view plain copyfigure;
I=imread(‘input_image.jpg’);
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);% generating circular configuration element
I2 = imdilate (I1, se);% of structural elements generated by the image expansion
subplot(1,2,2);
imshow(I2);
title ( 'post-expanded image ");
axis([50,250,50,200]);
grid on;% display grid lines
axis on;% display coordinate system
Results above code:
Sixteen, corrosion operation
[plain] view plain copyfigure;
I=imread(‘input_image.jpg’);
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);% generating circular configuration element
I2 = imerode (I1, se);% of structural elements generated by the image corrosion
subplot(1,2,2);
imshow(I2);
title ( 'image after etching');
axis([50,250,50,200]);
grid on;% display grid lines
axis on;% display coordinate system
Results above code:
XVII opening and closing operation
[plain] view plain copyfigure;
I=imread(‘input_image.jpg’);
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);% using a circle of radius 1 as a structural element
I2 = imopen (I1, se);% open operation
I3 = imclose (I1, se);% closing operation
subplot(2,2,3),imshow(I2);
title ( 'operation after turning the image');
axis([50,250,50,200]);
axis on;% display coordinate system
subplot(2,2,4),imshow(I3);
title ( 'close operation after image');
axis([50,250,50,200]);
axis on;% display coordinate system
Results above code:
Eighteen, opening and closing operation of the combination
[plain] view plain copyfigure;
I=imread(‘input_image.jpg’);
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
the shot = ( 'Drive', 1);
I2 = imopen (I1, se);% open operation
I3 = imclose (I1, se);% closing operation
subplot(3,2,3),imshow(I2);
title ( 'operation after turning the image');
axis([50,250,50,200]);
axis on;% display coordinate system
subplot(3,2,4),imshow(I3);
title ( 'close operation after image');
axis([50,250,50,200]);
axis on;% display coordinate system
the shot = ( 'Drive', 1);
I4 = imopen (I1, is);
I5=imclose(I4,se);
subplot (3,2,5), imshow (I5);% opening and - closing operation image
title ( 'opening and - closing operation image ");
axis([50,250,50,200]);
axis on;% display coordinate system
I6=imclose(I1,se);
I7 = imopen (I6, is);
subplot (3,2,6), imshow (I7);% closing - opening operation image
title ( 'closed - open operation image ");
axis([50,250,50,200]);
axis on;% display coordinate system
Results above code:
Ninth, morphological boundary extraction
[plain] view plain copyfigure;
I=imread(‘input_image.jpg’);
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 = bwperim (I1);% acquired circumferential length of the region
subplot(2,3,3),imshow(I2);
title ( 'perimeter border binary image');
axis([50,250,50,200]);
grid on;
axis on;
Bwmorph I3 = (I1, 'boundaries', 1);
subplot(2,3,4),imshow(I3);
title ( '1 times skeleton extraction');
axis([50,250,50,200]);
axis on;
I4 = bwmorph (I1, 'boundaries', 2);
subplot(2,3,5),imshow(I4);
title ( '2 times skeleton extraction');
axis([50,250,50,200]);
axis on;
Results above code: