I am a beginner, if you find errors in the text, please leave a message to tell me, thank you
If you need to detect the edges in the image, first we need to know what features are at the edges.
For a grayscale image, the gray values on both sides of the edge must be different so that we can tell where the edge is and where it is not.
So if we need to detect the edges of a grayscale image, we need to find out where the grayscale changes most. Obviously, the larger the gray change, the stronger the contrast, the more obvious the edge.
So the question comes, how do we know where the gray change is big, where the gray change is small?
Derivative, gradient, edge information
In mathematics, the derivative is related to the rate of change.
If the pixels of the grayscale image are continuous (not actually), then we can take the derivative of the X-direction and y-direction separately from the original image G
,
Obtains the derivative image Gy in the x direction in the GX and y directions. The GX and GY respectively imply the gray-level change information in the X and Y directions, and the edge information is implied.
If you want to include two-directional edge information on the same image, we can use a gradient. (gradient is a vector)
The gradient vector of the original image is gxy (gx,gy), and the size and direction of the gradient vector can be calculated with the following two formulas
The angle value seems to need to be appropriate +PI or-PI depending on the quadrant of the vector.
The gradient vector size contains the X-direction and y-direction edge information.
Image derivative
In fact, the image matrix is discrete.
The variation rate of the continuous function is the derivative, while the discrete function is used to calculate the change rate with the difference.
The concept of difference is easy to understand, that is, the difference between the two adjacent numbers to represent the rate of change.
The following formula is backward differential
Differential in x direction: Gx (n,y) = G (N,y)-G (N-1,y)
Differential in y direction: Gy (x,n) = G (X,n)-G (x,n-1)
When we actually calculate the derivative of the image, we do it through a convolution of the original image and an operator ( This method is to find the approximate derivative of the image ).
The simplest operator for image derivative is the Prewitt operator :
The Prewitt operator in the x direction is
The Prewitt operator in the y direction is
---------------------------------------------
the approximate process for convolution of the original image and an operator is as follows
If an area in the image matrix is
Then the derivative of the X-direction in the X5 is that the center and X5 of the X-direction operator are coincident, then the corresponding elements are multiplied and then summed, i.e.
The number of X-side wizards at X5 is x3+x6+x9-x1-x4-x7
The above calculation of all the elements in a matrix is the process of convolution.
--------------------------------------------
Therefore, the X-side wizard number Matrix GX can be obtained by using the original image and the X-direction Prewitt operator to convolution.
Using the original image and the Y-direction Prewitt operator to convolution, we can get the Y-party wizard number matrix Gy of the image.
Using formulas
You can get the gradient matrix Gxy of the image, which contains the edge information of the X-direction and y-direction of the image.
Python implementation of convolution and edge detection of Prewitt operators
First, we encapsulate the image convolution function in a function called Imconv.
ImportNumPy as NP fromPILImportImagedefImconv (Image_array,suanzi):" "Computed convolution parameters Image_array original gray image matrix Suanzi operator returns the result matrix after the original image and operator convolution" "Image= Image_array.copy ()#deep copy of the original image matrixdim1,dim2=Image.shape#multiply each element and operator and sum it (ignoring the most outer border pixels) forIinchRange (1,dim1-1): forJinchRange (1,dim2-1): Image[i,j]= (image_array[(i-1):(i+2), (j-1):(j+2)]*suanzi). SUM ()#because the gray value of the convolution is not necessarily between 0-255, unified into 0-255Image = image* (255.0/Image.max ())#return result Matrix returnImage
Then we use the Prewitt operator to calculate the X-side wizard number matrix Gx,y-side wizard number matrix Gy, and the gradient matrix Gxy.
ImportNumPy as NPImportMatplotlib.pyplot as Plt#Prewitt operator in x directionsuanzi_x = Np.array ([-1, 0, 1], [ -1, 0, 1], [ -1, 0, 1]])#Prewitt operator in y directionsuanzi_y = Np.array ([[ -1,-1,-1], [0, 0, 0], [1, 1, 1]])#Open the image and turn it into a grayscale imageImage = Image.open ("pika.jpg"). CONVERT ("L")#transform into an image matrixImage_array =Np.array (image)#get the X-direction matriximage_x =Imconv (image_array,suanzi_x)#get the Y-direction matrixImage_y =Imconv (image_array,suanzi_y)#get the gradient matrixImage_xy = Np.sqrt (image_x**2+image_y**2)#Unified gradient Matrix to 0-255Image_xy = (255.0/image_xy.max ()) *Image_xy#Drawing an imagePlt.subplot (2,2,1) plt.imshow (Image_array,cmap=Cm.gray) Plt.axis ("off") Plt.subplot (2,2,2) plt.imshow (Image_x,cmap=Cm.gray) Plt.axis ("if") Plt.subplot (2,2,3) plt.imshow (Image_y,cmap=Cm.gray) Plt.axis ("if") Plt.subplot (2,2,4) plt.imshow (Image_xy,cmap=Cm.gray) Plt.axis ("off") plt.show ()
The results of the Prewitt operator are as shown
Above: The image on the left is the original, and the X-side wizard image on the right
Below: The left is the Y-side wizard number image, the right image is the gradient image
It can be seen that the Prewitt operator can detect the edge of the image, but the detection result is coarser and has a lot of noise.
Sobel operator of approximate derivative
Sobel operators are similar to Prewitt, but they are better than Prewitt operators.
The Sobel operator in the x direction is
The Sobel operator in the y direction is
The Python code only needs to change the Prewitt operator in the code above into the Sobel operator.
# Sobel operator in X-direction suanzi_x = Np.array ([[-1, 0, 1] ,-2, 0, 2] ,1, 0, 1]])# Sobel operator in Y-direction suanzi_y = Np.array ([[ -1,-2,-1], [0, 0, 0] ,1, 2, 1])
The results of the Sobel operator are as shown
Above: The image on the left is the original, and the X-side wizard image on the right
Below: The left is the Y-side wizard number image, the right image is the gradient image
It is seen that comparing the Prewitt operator and the Sobel operator, the Sobel operator slightly reduces the noise, but the noise is still more.
Not finished, to be continued
Reference list
1. "Python Computer Vision Programming"
Python Computer vision 2: Image edge Detection