## Python + opencv: image gradient

Machine vision 001 2020-11-16 01:29:22

### theory

OpenCV Three types of gradient filters or high pass filters are available ,Sobel, Scharr and Laplacian.

1. Sobel and Scharr derivative

Sobel The operator is a joint Gaussian smoothing plus differential operation , It has strong anti noise ability .

You can specify the direction of the derivative to take , Vertical or horizontal ( Through the parameters yorder and xorder).

You can also use parameters ksize To specify the size of the kernel .

If ksize = -1, Then use 3x3 Of Scharr filter , The result is better than 3x3 Of Sobel filter .

Check out the kernel used in the documentation .

2. Laplacian derivative

It computes the Laplacian operator of the image given by the relation , Each of these derivatives uses Sobel Derivative finding .

If ksize = 1, Then use the following kernel to filter ：

### Example

####################################################################################################
"""
"""
image = lmc_cv.cvtColor(image, lmc_cv.COLOR_BGR2GRAY)
laplacian_image = lmc_cv.Laplacian(image, lmc_cv.CV_64F, ksize=3, scale=1, delta=0,
borderType=lmc_cv.BORDER_DEFAULT)
sobelx_image = lmc_cv.Sobel(image, lmc_cv.CV_64F, 1, 0, ksize=5, scale=1, delta=0,
borderType=lmc_cv.BORDER_DEFAULT)
sobely_image = lmc_cv.Sobel(image, lmc_cv.CV_64F, 0, 1, ksize=5, scale=1, delta=0,
borderType=lmc_cv.BORDER_DEFAULT)
# Display images
pyplot.figure('Image Display')
titles = ['Original', 'Laplacian', 'Sobel X', 'Sobel Y']
images = [image, laplacian_image, sobelx_image, sobely_image]
for i in range(4):
pyplot.subplot(2, 2, i + 1)
pyplot.imshow(images[i], 'gray')
pyplot.title(titles[i])
pyplot.xticks([])
pyplot.yticks([])
pyplot.show()
# Save images based on user input
if ord("q") == (lmc_cv.waitKey(0) & 0xFF):
# Destruction of the window
pyplot.close()
return

### emphasize

In the previous example , The output data type is cv.CV_8U or np.uint8. But there's a small problem , The transition from black to white is considered a positive slope ( It has a positive value ), The transition from white to black is considered a negative slope ( It has a negative value ).

So when you convert data into np.uint8, All negative slopes are zero . In short , You missed this side .

If you want to detect these two edges , A better choice is to keep the output data type in some higher form , Such as cv.CV_16S, cv.CV_64F etc. , Take its absolute value , And then switch back to cv.CV_8U.

The following code demonstrates the level Sobel The process of the filter is different from the result .

####################################################################################################
# Positive and negative gradients (Positive-Negative slope)
def lmc_cv_positive_negative_slope():
"""
The functionality : Positive and negative gradients (Positive-Negative slope).
"""
image = lmc_cv.cvtColor(image, lmc_cv.COLOR_BGR2GRAY)
# Positive and negative gradients (Positive-Negative slope)
# Output dtype = cv.CV_8U
sobelx8u_image1 = lmc_cv.Sobel(image, lmc_cv.CV_8U, 1, 0, ksize=5, scale=1, delta=0,
borderType=lmc_cv.BORDER_DEFAULT)
# Output dtype = cv.CV_64F. Then take its absolute and convert to cv.CV_8U
sobelx64f_image = lmc_cv.Sobel(image, lmc_cv.CV_64F, 1, 0, ksize=5, scale=1, delta=0,
borderType=lmc_cv.BORDER_DEFAULT)
abs_sobel64f_image = np.absolute(sobelx64f_image)
sobelx8u_image2 = np.uint8(abs_sobel64f_image)
# Display images
pyplot.figure('Image Display')
titles = ['Original', 'Sobel CV_8U', 'Sobel abs(CV_64F)']
images = [image, sobelx8u_image1, sobelx8u_image2]
for i in range(3):
pyplot.subplot(1, 3, i + 1)
pyplot.imshow(images[i], 'gray')
pyplot.title(titles[i])
pyplot.xticks([])
pyplot.yticks([])
pyplot.show()
# Save images based on user input
if ord("q") == (lmc_cv.waitKey(0) & 0xFF):
# Destruction of the window
pyplot.close()
return