opencv-python- study notes thirteen (image gradient)

 

What is the gradient? Gradient is simply the derivative. The derivative of the detected edge image.

As used herein, the parameters

src input image.                                                                                                                          
dst An output image having the same size, the same number of channels of input image
ddepth

The depth of the output image, See  Combinations ; for different input image, the output image at different depths, as follows

Input depth (src.depth()) Output depth (ddepth)
CV_8U -1/CV_16S/CV_32F/CV_64F
CV_16U / CV_16S -1 / CV_32F / CV_64F                                                                                                
CV_32F -1 / CV_32F / CV_64F
CV_64F -1 / CV_64F                      
dx order derivative in the x direction, it is possible to dx = 1, dy = 0 in the x direction to achieve derivative
two order derivative y-direction, it is possible to dx = 0, dy = 1 derivative to achieve the y-direction
ksiz to Kernel size, must be taken 1, 3, 5, or 7.
scale Zoom size, default 1
delta Increment value, default 0. optional delta value that is added to the results prior to storing them in dst.
borderType The default boundary type BORDER_DEFAULT, See  BorderTypes

principle

OpenCV provides three different gradient filter, or the high-pass filter: Sobel, Scharr and Laplacian.

Sobel, Scharr in fact, seeking first or second derivative. Scharr is (using a convolution kernel small gradient angle Solution Solution) Optimization of Sobel. Laplacian is the second derivative.

1.Sobel and Scharr Derivatives

sobel joint Gaussian smoothing plus derivative operation, so it has a good noise resistance. You can set the direction of the derivation of (xorder or yorder). May also be used to set the size of the convolution kernel (ksize).

function:

dst=cv.Sobel(src, ddepth, dx, dy[, dst[, ksize[, scale[, delta[, borderType]]]]])

 

Function Description:

Sobel operator using the extended computing a first, second, third mixing image data or pilot. Except a case where all are used ksize × ksize separable core computing derivatives. When the time ksize = 1, use
. 3 × . 1  or  . 1 × . 3 nuclei (i.e., no Gaussian smoothing), ksize = 1 can only be used for the first or second derivative of the x- or y-. = -1, there is a special use value of Scharr ksize 3x3 filter, it is more effective than 3x3 Sobel filter well (and the same speed, so when using a filter 3x3 filter should always use Scharr ), may also be used at this time cv2.Laplacian (src, ddepth, dst, ksize, scale, delta, borderType) function.

 

Sobel operator to combine Gaussian smoothing and differentiation, inhibition of the results of a certain noise. Typically, with the function (xorder = 1, yorder = 0, ksize = 3) or (xorder = 0, yorder = 1, ksize = 3) to calculate the first derivative of the image x or y

 

The first case corresponds to:

The second case corresponds to:

 

2. Laplacian Derivatives

Laplacian defined may be used in the form of the second derivative, which may be assumed to achieve similar second order discrete Sobel derivative, in fact, in calculating the OpenCV call directly Sobel Laplacian operator neutrons. Calculated as follows:

 

If ksize = 1, use the following kernel to filter

function:

 dst=cv.Laplacian(src, ddepth[, dst[, ksize[, scale[, delta[, borderType]]]]])

 

Function Description:

The second function by using the calculated x Sobel operator, y, the derivative is calculated by adding the source image Laplacian:

 

This is done when ksize> 1. When ksize == 1, the through aperture of the image of the 3 × 3 filter calculation is as follows Laplacian matrix:

 

 For example

import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt

img = cv.imread('1.jpg', 0)
laplacian = cv.Laplacian(img, cv.CV_64F)
sobelx = cv.Sobel(img, cv.CV_64F, 1, 0, ksize=5)
sobely = cv.Sobel(img, cv.CV_64F, 0, 1, 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, 4), plt.imshow(sobely, cmap='gray')
plt.title('Sobel Y'), plt.xticks([]), plt.yticks([])
plt.show()

 

 

 Imagine a black and white from the boundary of the derivative is an integer, and a number of border points from white to black lead is negative. If the depth of the original image is np.int8, all the negative values ​​will be truncated becomes zero, in other words, to the border lost out. So if you want these two boundaries detected, the higher the best way is to output data type setting, such as cv2.CV_16S, cv2.CV_64F and so on. Then take the absolute value and then turn it back cv2.CV_8U. The following example demonstrates the different effects caused by different depths of the output image.

 

Guess you like

Origin www.cnblogs.com/blog-xyy/p/11265624.html