(Digital Image Processing MATLAB+Python) Chapter 7 Image Sharpening - Section 3: Gaussian Filtering and Edge Detection

One: Gaussian function

(1) Definition

Gaussian function : It is a common continuous function, usually with the symbol G ( x ) G(x)G ( x ) means. It can be defined with the following formula

G ( x ) = 1 σ 2 π e − x 2 2 σ 2 G(x)=\frac{1}{\sigma\sqrt{2\pi}}e^{-\frac{x^{2}} {2\sigma^{2}}}G(x)=p2 p.m 1e2 p2x2

Among them, xxx is the independent variable,σ \sigmaσ is a positive real number representing the standard deviation of the Gaussian function. Gaussian functions have the following properties:

  • The Gaussian function is an even function , that is, G ( − x ) = G ( x ) G(-x) = G(x)G(x)=G(x)
  • The curve of the Gaussian function is bell-shaped , and at x = 0 x=0x=0 to get the peak value. Standard deviationσ \sigmaThe smaller σ , the higher the peak of the Gaussian function and the steeper the curve; the standard deviationσ \sigmaThe larger the σ , the lower the peak of the Gaussian function and the gentler the curve
  • The area of ​​the Gaussian function is 1 11,即 ∫ − ∞ ∞ G ( x ) d x = 1 \int_{-\infty}^{\infty} G(x) dx = 1 G(x)dx=1
  • σ \sigmaσ tends to0 0When 0 ,the peak value of the Gaussian function approaches infinity, and the curve becomes steeper and steeper, approaching aδ \deltaδ function。δ \deltaThe delta function is a function similar to an impulse, when the independent variable is equal to0 0When 0 , the function value is infinite, while at other positions the function value is0 00

The following is the Gaussian function and its first derivative form
insert image description here

The following is the form of the second derivative of the Gaussian function

insert image description here

(2) Features

insert image description here

  • Weights gradually decrease to zero as you move away from the origin , indicating that image values ​​closer to the center are more important than those farther away
  • Standard deviation σ \sigmaσ determines the scope of the neighborhood,95% of the total weight is contained in 2 σ 2\sigmaIn the middle range of 2 σ
  • The second derivative has a smooth middle overhang with negative function values ​​and two smooth side overhangs with positive values
  • Zero crossing at − σ -\sigmaσ+ σ +\sigma+ σ , andg ( x ) g(x)The inflection point of g ( x ) andg ′ ( x ) g^{\prime}(x)gThe extremum point of (x)
  • The 1D form is rotated about the vertical axis so that the 2D functional form is isotropic (with the same 1D Gaussian section on any tangent plane passing through the origin), and its second derivative form is like a sombrero or a sombrero
  • From mathematical derivation, the cavity edge of the hat z = g ( x , y ) z=g(x,y)z=g(x,y ) axial direction, in the application of display and filtering, the cavity mouth is generally facing downward, that is, the protruding part in the middle is positive, and the cap edge is negative

Two: LOG operator

(1) Definition

LOG operator : Marr uses Gaussian function to smooth the image first, and then uses Laplacian operator to detect edges, referred to as LOG filter

LoG ⁡ ( x , y ) = 1 π σ 4 [ 1 − x 2 + y 2 2 σ 2 ] e − x 2 + y 2 2 σ 2 \operatorname{LoG}(x, y)=\frac{1} {\pi \sigma^{4}}\left[1-\frac{x^{2}+y^{2}}{2 \sigma^{2}}\right] e^{-\frac{x ^{2}+y^{2}}{2 \sigma^{2}}}LoG(x,y)=p. p41[12 p2x2+y2]e2 p2x2+y2

The LOG operator can be seen as combining the Gaussian function and the Laplacian operator (that is, the second-order derivative operator) , so its output is the result of the Laplace transform and Gaussian filtering of the original image. It can be used to detect features such as edges and corners in an image. will ggg and image functionfff convolution, get a smooth image function, perform Laplacian operation on the function, and extract the edge

can prove

∇ 2 [ f ( x , y ) ∗ g ( x , y ) ] = f ( x , y ) ∗ ∇ 2 g ( x , y ) \nabla^{2}[f(x, y) * g(x , y)]=f(x, y) * \nabla^{2} g(x, y)2[f(x,y)g(x,y)]=f(x,y)2g(x,y)

∇ 2 g ( x , y ) = ∂ 2 g ∂ x 2 + ∂ 2 g ∂ y 2 = 1 π σ 4 ( x 2 + y 2 2 σ 2 − 1 ) exp ⁡ ( − x 2 + y 2 2 σ 2 ) \nabla^{2} g(x, y)=\frac{\partial^{2} g}{\partial x^{2}}+\frac{\partial^{2} g}{\partial y^{2}}=\frac{1}{\pi \sigma^{4}}\left(\frac{x^{2}+y^{2}}{2 \sigma^{2}}-1\right) \exp \left(-\frac{x^{2}+y^{2}}{2 \sigma^{2}}\right) 2g(x,y)=x22g+y22g=p. p41(2 p2x2+y21)exp(2 p2x2+y2)

∇ 2 g ( x , y ) \nabla^{2}g(x,y)2g(x,y ) is the LOG filter, also known as the Marr-Hildrech operator

  • σ \sigma σ is a scale factor, a large value can be used to detect blurred edges, and a small value can be used to detect well-focused image details

The shape of the LOG operator is shown in the figure below

insert image description here

The size of the filter is given by σ \sigmaThe value of σ or equivalently byω 2 D \omega_{2D}oh2D _value to determine. In order not to cause the function to be truncated excessively, the calculation should be performed in a sufficiently large window, and the window width is usually taken as ω ≥ 3.6 ω 2 D \omega \geq 3.6\omega_{2D}oh3.6 o2D _

σ \sigmaWhen σ takes different values, it corresponds to different templates
insert image description here

(2) Program

insert image description here


matlab implementation :

Image=im2double(rgb2gray(imread('lotus.jpg')));
figure,imshow(Image),title('原图像');
H=fspecial('laplacian',0);
R=imfilter(Image,H);
edgeImage=abs(R);
figure,imshow(edgeImage),title('Laplacian梯度图像');
H1=[0 -1 0;-1 5 -1;0 -1 0];
sharpImage=imfilter(Image,H1);
figure,imshow(sharpImage),title('Laplacian锐化图像');


Python implementation :

import cv2
import matplotlib.pyplot as plt
import numpy as np

# 读入图像
Image = cv2.imread('lotus.jpg')
Image = cv2.cvtColor(Image, cv2.COLOR_BGR2GRAY)
Image = Image.astype(np.float64) / 255.0

# 显示原图像
plt.imshow(Image, cmap='gray')
plt.title('原图像')
plt.show()

# 计算Laplacian梯度图像
H = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]], dtype=np.float64)
R = cv2.filter2D(Image, -1, H)
edgeImage = np.abs(R)

# 显示Laplacian梯度图像
plt.imshow(edgeImage, cmap='gray')
plt.title('Laplacian梯度图像')
plt.show()

# 计算Laplacian锐化图像
H1 = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], dtype=np.float64)
sharpImage = cv2.filter2D(Image, -1, H1)

# 显示Laplacian锐化图像
plt.imshow(sharpImage, cmap='gray')
plt.title('Laplacian锐化图像')
plt.show()

Three: Canny operator

(1) Optimal edge detection

Optimal edge detection : refers to the algorithm that can obtain the best edge detection results among all edge detection algorithms. However, no edge detection algorithm can perform optimally in all situations, because different algorithms are suitable for different types of images and different application scenarios. Therefore, we need to choose the most suitable edge detection algorithm according to the specific situation. Three main evaluation criteria for optimal edge detection

  • Low error rate : Identify as many actual edges as possible while minimizing false positives from noise.
  • Accurate edge positioning : The marked edge should be as close as possible to the actual edge in the image.
  • Minimal Response : Edges in the image are preferably only identified once, and potentially noisy parts of the image should not be identified as edges

(2) Canny operator

Canny operator : It is a widely used edge detection algorithm proposed by John Canny in 1986. It can detect edges with low error rate and high positioning accuracy, and is considered to be one of the best edge detection algorithms currently. The algorithm steps are as follows

  • Denoising : First, the image should be Gaussian filtered to remove noise
  • Calculate the gradient : Then, calculate the gradient of the image by using the Sobel operator to detect the strength and direction of the edge
  • Non-maximum suppression : Next, non-maximum suppression is performed in the gradient direction to eliminate small edge responses and edge widths, and retain local maxima in the gradient direction
  • Double-thresholding : Then, double-thresholding should be performed according to the gradient value, and the pixels are divided into three categories: strong edge, weak edge and non-edge
  • Edge connection : Finally, a complete edge should be formed by connecting strong edge pixels with weak edge pixels adjacent to it. The process of connecting edges can use methods based on depth-first search or connectivity analysis

The advantage of Canny operator is that it has good edge connectivity, single response and low error rate. The disadvantage is that the amount of calculation is large. In addition, the Canny operator is very sensitive to the selection of parameters and needs to be adjusted according to specific application scenarios

(3) Program

insert image description here


matlab implementation :

Image=im2double(rgb2gray(imread('lotus.jpg')));
figure,imshow(Image),title('原图像');
BW= edge(Image,'canny');
figure,imshow(BW),title('Canny边缘检测');

Python implementation :

Image=im2double(rgb2gray(imread('lotus.jpg')));
figure,imshow(Image),title('原图像');
BW= edge(Image,'canny');
figure,imshow(BW),title('Canny边缘检测');

Guess you like

Origin blog.csdn.net/qq_39183034/article/details/130510502