The target

In this chapter, we will learn:

  • Find image gradients, edges, etc
  • We’ll see the following functions: CV.Sobel(), CV.Scharr(), CV.Laplacian(), etc

The theory of

OpenCV offers three types of gradient or high-pass filters, namely Sobel, Scharr, and Laplacian. We’ll see each of them.

1. Sobel and Scharr operators

Sobel operator is a combination of Gaussian smoothing and differential operation, so it is more anti-noise. The inverse can specify the direction of the derivative to take, vertical or horizontal (with the arguments yorder and xorder, respectively). The inverse can also specify the size of the kernel with the ksize parameter. If ksize = -1, a 3×3 Scharr filter is used, which gives better results than a 3×3 Sobel filter. Refer to the documentation for the kernel used.

2. The Laplacian operator

$Delta SRC = frac{partial ^2{SRC}}{partial x^2} + frac{partial ^2{SRC}}{partial The Laplace diagram of the graph given by y^2}$is calculated by the Sobel operator for each derivative. If ksize = 1, then use the following kernel for filtering:

? kernel = begin{bmatrix} 0 & 1 & 0 \ 1 & -4 & 1 \ 0 & 1 & 0 end{bmatrix}?

code

The following code shows all operators in a single chart. All cores are 5×5 in size. Output image depth through -1 to get the result of nP.uint8 type.

import numpy as np import cv2 as cv from matplotlib import pyplot as plt img = cv.imread('dave.jpg',0) laplacian = Laplacian(img, CV.CV_64F) sobelx = CV.Sobel(img, CV.CV_64F,1,0,ksize=5) Plt.subplot (2,2,1),plt.imshow(img,cmap = 'gray') plt.title('Original'), plt.xticks([]), Plt.yticks ([]) plt.subplot(2,2,2),plt.imshow(Laplacian,cmap = 'gray') plt.title('Laplacian'), plt.xticks([]), Plt.yticks ([]) plt.subplot(2,2,3),plt.imshow(sobelx,cmap = 'gray') plt.title('Sobel X'), plt.xticks([]), plt.yticks([]) plt.subplot(2,2,3),plt.imshow(sobelx,cmap = 'gray') plt.title('Sobel X'), plt.xticks([]), Plt.yticks ([]) plt.subplot(2,2,4),plt.imshow(sobely,cmap = 'gray') plt.title('Sobel Y'), plt.xticks([]), plt.yticks([]) plt.show()Copy the code

Results:

A matter of importance

In our last example, the output data type is CV.cv_8U or np.uint8. But there’s a small problem. A transition from black to white is considered a positive slope (with a positive value), while a transition from white to black is considered a negative slope (with a negative value). Therefore, when you convert the data to NP.uint8, all negative slopes are set to zero. In short, you will miss this edge information.

If you want to detect two edges, a better choice is to leave the output data type in a higher form, such as Cv.cv_16s, Cv.cv_64F, etc., take its absolute value, and then convert back to Cv.cv_8U. The following code demonstrates this process for horizontal Sobel filters and result differentials.

import numpy as np import cv2 as cv from matplotlib import pyplot as plt img = cv.imread('box.png',0) # Output dtype = CV.CV_8U sobelx8u = CV.Sobel(img, CV.CV_8U,1,0,ksize=5) # Output dtype = CV.CV_64F. Then take its absolute and convert CV.CV_8U sobelx64f = CV.Sobel(img, CV.CV_64F,1,0,ksize=5) abs_sobel64f = np. Absolute (sobelx64f) sobel_8u = Np.uint8 (abs_sobel64f) plt.subplot(1,3,1),plt.imshow(img,cmap = 'gray') plt.title('Original'), plt.xticks([]), Plt.yticks ([]) plt.subplot(1,3,2),plt.imshow(sobelx8u,cmap = 'gray') plt.title('Sobel CV_8U'), plt.xticks([]), plt.subplot(1,3,2),plt.imshow(sobelx8u,cmap = 'gray') plt.title('Sobel CV_8U'), plt.xticks([]), Plt.yticks ([]) plt.subplot(1,3,3),plt.imshow(sobel_8u,cmap = 'gray') plt.title('Sobel abs(CV_64F)'), plt.xticks([]), plt.yticks([]) plt.subplot(1,3,3),plt.imshow(sobel_8u,cmap = 'gray') plt.title('Sobel abs(CV_64F)'), plt.xticks([]), plt.yticks([]) plt.show()Copy the code

View the following results:

Rock and the AI technology blog resources summary station: http://docs.panchuang.net/PyTorch, the official Chinese tutorial station: Chinese official document: http://pytorch.panchuang.net/OpenCV http://woshicver.com/