Edge detection, one of the image segmentation techniques in matlab

1. Edge detection
(1) Roberts edge operator
(2) Sobel operator
(3) Prewitt operator
(4) Laplacian (Laplacian) operator
(5) LOG (Laplacian-Gauss) operator
(6) Kan Canny operator
(7) uses Hough transform

Image Segmentation Technology Image segmentation is the technology and process of dividing an image into several specific regions with unique properties and extracting objects of interest. In the research and application of images, people are often only interested in some parts of the image (target or background), which generally correspond to specific regions with unique properties in the image.
**(1) Segmentation method of multiple feature fusion: **In addition to using the original grayscale feature of the image, we can also use the gradient feature of the image, geometric features (morphology, coordinates, distance, direction, curvature, etc.), transformation Features (Fourier spectrum, wavelet features, fractal features, etc.) and statistical features (texture, invariant moments, gray mean, etc.) and other high-level features.
**(2) A segmentation method combining multiple segmentation methods: **Due to the uncertainty of target imaging and the diversity of targets, it is difficult for a single segmentation method to achieve ideal segmentation results for images containing complex targets.

1. Edge detection

Edge (Edge) refers to the part of the image where the local brightness changes most significantly. Edges mainly exist between objects, objects and backgrounds, regions and regions (including different colors), and are an important basis for image analysis such as image segmentation, texture feature extraction, and shape feature extraction.
(1) Roberts edge operator
is a gradient calculation method for oblique deviation points. The size of the gradient represents the strength of the edge, and the direction of the gradient is perpendicular to the direction of the edge. The Roberts operation is actually the sum of the differential values ​​in the two directions of rotation ±45°. The Roberts edge operator has high positioning accuracy and works well in the horizontal and vertical directions, but it is sensitive to noise.
Two convolution kernels Gx=[1 0 0 -1], Gy=[0 1 -1 0]

clc;                %clc的作用就是清屏幕
clear;              %clear是删除所有的变量
close all;          %close all是将所有打开的图片关掉。
i=imread('E:\我的桌面\MATLAB\练习\3.jpg'); %读取图像
I=im2gray(i);
BW1=edge(I, 'Roberts',0.04);    %Roberts算子检测边缘
subplot(1,2,1),
imshow(I);
title('原图像')
subplot(1,2,2),
imshow(BW1);
title('Roberts算子检测边缘')

insert image description here
(2) Sobel operator
Sobel operator is a group of direction operators, which detect edges from different directions. The Sobel operator is not a simple average and difference, but strengthens the weight of the four direction pixels of the center pixel, up, down, left, and right, and the result of the operation is an edge image. The Sobel operator usually handles images with grayscale gradients and more noise better.
Two convolution kernels Gx=[-1 0 1 -2 0 2 -1 0 1], Gy=[1 2 1 0 0 0-1 -2 -1]

clc;                %clc的作用就是清屏幕
clear;              %clear是删除所有的变量
close all;          %close all是将所有打开的图片关掉。
i=imread('E:\我的桌面\MATLAB\练习\3.jpg'); %读取图像
I=im2gray(i);
BW1=edge(I, 'Sobel',0.04);  %Sobel算子检测边缘
subplot(1,2,1),
imshow(I);
title('原图像')
subplot(1,2,2),
imshow(BW1);
title('Sobel算子检测边缘')

insert image description here
(3) Prewitt operator The
Prewitt edge operator is an edge template operator, which uses the gray level difference between the upper and lower pixels and the left and right neighbors to detect the edge at the edge, and has a smoothing effect on the noise. Because the gray value of the edge pixel is significantly different from that of the area pixel, differential operators and template matching methods are usually used to detect the edge of the image in practical applications. The Prewitt operator can not only detect edge points, but also suppress the influence of noise, so it can handle images with more grayscale and noise better.
Two convolution kernels Gx=[-1 0 1 -2 0 2 -1 0 1], Gy=[1 2 1 0 0 0-1 -2 -1]

clc;                %clc的作用就是清屏幕
clear;              %clear是删除所有的变量
close all;          %close all是将所有打开的图片关掉。
i=imread('E:\我的桌面\MATLAB\练习\3.jpg'); %读取图像
I=im2gray(i);
BW1=edge(I, 'prewitt',0.04);     % 0.04为梯度阈值
subplot(1,2,1)
imshow(I);
title('原图像')
subplot(1,2,2),
imshow(BW1);
title('Prewitt算子检测边缘')

insert image description here
(4) Laplacian operator
If the first-order derivative obtained is higher than a certain threshold, the point can be determined as an edge point, which will lead to too many edge points detected. A better method is to find the points corresponding to the local maximum of the gradient and identify them as edge points. If a threshold is used for edge detection, all points between a and b are recorded as edge points. But by removing non-local maxima in the first derivative, more precise edges can be detected.
(5) LOG (Laplacian-Gauss) operator
Gaussian filtering and Laplacian edge detection are combined to form a LOG algorithm, also known as the Laplacian-Gauss algorithm. The LOG operator is an improvement to the Laplacian operator, which needs to consider the processing of the 5×5 neighborhood to obtain better detection results.

clc;                %clc的作用就是清屏幕
clear;              %clear是删除所有的变量
close all;          %close all是将所有打开的图片关掉。
i=imread('E:\我的桌面\MATLAB\练习\3.jpg'); %读取图像
I=im2gray(i);
BW1=edge(I, 'log',0.003,2);                 %sigma=2
subplot(1,3,1);
imshow(I);
title('原图像')
subplot(1,3,2);
imshow(BW1);
title(' sigma =2的LOG算子检测的边缘')
BW1=edge(I, 'log',0.003,3);                 % sigma=3
subplot(1,3,3);
imshow(BW1);
title('sigma =3的LOG算子检测的边缘')

insert image description here
(6) Canny operator
The basic idea of ​​detecting the step edge is to find the pixel with the local maximum gradient magnitude in the image. Most work on detecting step edges focuses on finding numerical approximations to gradients that can be used in real images.

clc;                %clc的作用就是清屏幕
clear;              %clear是删除所有的变量
close all;          %close all是将所有打开的图片关掉。
i=imread('E:\我的桌面\MATLAB\练习\3.jpg'); %读取图像
I=im2gray(i);
BW1=edge(I, 'canny',0.2);      %Canny算子边缘检测
subplot(1,2,1);
imshow(I);
title('原图像')
subplot(1,2,2);
imshow(BW1);
title('Canny算子边缘检测')

insert image description here

%%所有都放一起比较
clc;                %clc的作用就是清屏幕
clear;              %clear是删除所有的变量
close all;          %close all是将所有打开的图片关掉。
i=imread('E:\我的桌面\MATLAB\练习\3.jpg'); %读取图像
I=im2gray(i);
BW1=edge(I, 'Roberts',0.04);        %Roberts算子
BW2=edge(I, 'Sobel',0.04);          %Sobel算子
BW3=edge(I, 'Prewitt',0.04);        %Prewitt算子
BW4=edge(I, 'LOG',0.004);           % LOG算子
BW5=edge(I, 'Canny',0.04);          % Canny算子
subplot(2,3,1),
imshow(I);
title('原图像');
subplot(2,3,2),
imshow(BW1)
title('Roberts ')
subplot(2,3,3),
imshow(BW2)
title(' Sobel ')
subplot(2,3,4),
imshow(BW3)
title(' Prewitt ')
subplot(2,3,5),
imshow(BW4)
title(' LOG ')
subplot(2,3,6),
imshow(BW5)
title('Canny ')

insert image description here
The result of edge extraction by Roberts operator is thicker, and the edge location is not very accurate. The edge location of Sobel operator and Prewitt operator is more accurate, and the result of edge extraction by Laplacian-Gaussian operator is obviously better. For the first three operators, especially the edges are more complete and the positions are more accurate. In comparison, the edge extracted by the Canny operator is the most complete, and the continuity of the edge is very good, and the effect is better than other operators above, mainly because it performs "non-maximum suppression" and morphological connection operations. result.

(7) The algorithm of using Hough transform
to detect the edge of the image. The Hough transform is originally applied to the detection of straight lines, which fully reflects the advantages of the Hough transform, such as clear geometric analysis, certain anti-interference ability and easy parallel processing. .

		clc;                %clc的作用就是清屏幕
		clear;              %clear是删除所有的变量
		close all;          %close all是将所有打开的图片关掉。
		I=imread('E:\我的桌面\MATLAB\练习\3.jpg'); %读取图像
		rotI=rgb2gray(I);
		subplot(2,2,1);
        imshow(rotI);
        title('灰度图像');
        axis([50,250,50,200]);
        grid on;
        axis on;
        BW=edge(rotI, 'prewitt');          %prewitt算子边缘检测
        subplot(2,2,2);
        imshow(BW);
        title('prewitt算子边缘检测后图像');
        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('霍夫变换图');
        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('霍夫变换图像检测');
        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');

insert image description here

Guess you like

Origin blog.csdn.net/qq_55433305/article/details/128139694