write sth. upfront
Starting from this section, the computer vision tutorial enters the third chapter - Image Feature Extraction. In this chapter, you will see a simple image contains so many detailed features that you did not notice, and these features will play an extremely important role in more advanced applications in the future. This article explains one of the basic features - image edges.
In this paper, we use object-oriented design to define an edge detection class EdgeDetect to make the application of image edge detection algorithms more concise, for example
import cv2 import numpy as np import as plt Detector = EdgeDetect('') Prewitt = () (Prewitt , 'gray') ()
The constructor for this class is
class EdgeDetect: def __init__(self, img) -> None: = (img) = (, cv2.COLOR_BGR2GRAY)
What is read is the basic information of the image.
1. First-order differential operators
Image edges are high-frequency components of a digital image that correspond to the extremes of the image gradient. On a two-dimensional discrete digital image, the image intensity function differentiation in a certain direction is approximated using the finite difference method, i.e.:
Thus image edge detection is the difference operation on the image.
1.1 Prewitt operator
The Prewitt operator is essentially the difference between neighboring pixels in the x or y direction.
And what do we often mean by image gradient?
It's actually a vector of directions using the difference between neighboring pixels in the x and y directions
In the programming implementation, it is the construction of the above figure of the two directions of the filter operator, and then the x xx, y yy two directions of the edge synthesis is the whole figure of the edge detection results in each direction
def prewitt(self): # Prewitt operator kernelX = ([[1,1,1],[0,0,0],[-1,-1,-1]], dtype=int) kernelY = ([[-1,0,1],[-1,0,1],[-1,0,1]], dtype=int) # Filter the image x = cv2.filter2D(, cv2.CV_16S, kernelX) y = cv2.filter2D(, cv2.CV_16S, kernelY) # to uint8, image fusion absX = (x) absY = (y) return (absX, 0.5, absY, 0.5, 0)
1.2 Sobel operator
The Sobel operator is obtained by taking the derivative of the Gaussian kernel function in the x and y directions and templating it.The Sobel operator has a stronger noise immunity compared to the Prewitt operator because it combines the effects of Gaussian filtering.
In the programming implementation, it is the construction of the above figure of the two directions of the filter operator, and then the x, y two directions of the edge synthesis is the whole figure of the edge detection results in each direction
def sobel(self): # Sobel operator kernelX = ([[1, 2, 1],[0, 0, 0],[-1, -2, -1]],dtype=int) kernelY = ([[-1, -2, -1],[0, 0, 0],[1, 2, 1]],dtype=int) # Filter the image x = cv2.filter2D(, cv2.CV_16S, kernelX) y = cv2.filter2D(, cv2.CV_16S, kernelY) # to uint8, image fusion absX = (x) absY = (y) return (absX, 0.5, absY, 0.5, 0)
2. Second-order differential operators
2.1 Laplace operator
Putting the Laplace operator
Written in the form of a difference equation as
The differential equation is further written in the form of convolution kernel as in Fig. (a), which can be extended to make it isotropic as in Fig. (b). The differential operator is a high-pass filter, which enhances the noise while sharpening the edges, so the Laplace operator has weak noise immunity and cannot detect the edge direction.
In terms of programming implementation, this is the construction of the filtering operator shown above
# Laplace operator def laplace(self): kernel = ([[0, -1, 0], [-1, 4, -1], [0, -1, 0]], dtype=int) img = cv2.filter2D(, cv2.CV_16S, kernel) return (img)
2.2 LoG operator
In order to overcome the problem of weak noise immunity of Laplace operator, Gaussian-Laplace operator (LoG, Laplace of Gaussian) is introduced, i.e., the noise is filtered out by low-pass first, and then the edges are strengthened by high-pass, and the LoG operator is essentially a band-pass filter.
In terms of programming implementation, this is the construction of the filtering operator shown above
# LoG operator def LoG(self): kernel = ([[0, 0, 1, 0, 0], [0, 1, 2, 1, 0], [1, 2, -16, 2, 1], [0, 1, 2, 1, 0], [0, 0, 1, 0, 0]], dtype=int) img = cv2.filter2D(, cv2.CV_16S, kernel) return (img)
Edge detection
Canny edge detection algorithm can be divided into the following steps.
- The original image noise is filtered using the Sobel operator and the gradient map is obtained;
- Apply Non-Maximum Suppression (NMS) to eliminate spurious responses from edge detection and target detection, i.e., as far as possible, there should be a unique and accurate response to the edge or target to be measured.
- Double-Threshold (DT) detection is applied to determine real and potential edges.
The spurious edge response due to noise is resolved using the following dual threshold detection algorithm.
The choice of threshold depends on the content of the given input image. The weak edges are further scrutinized below, i.e.
Typically, a weak edge pixel caused by a true edge will be connected to a strong edge pixel, while the noise response is not connected. In order to track the edge connections, the decision to filter out the weak edge point is made by looking at the 8 neighboring pixels of the weak edge pixel for the presence of a strong edge pixel.
Here are the results of the Canny edge detection algorithm.
To this point, this article on the implementation of image edge detection algorithm in Python is introduced to this article, more related Python image edge detection algorithm content, please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!