Opencv之图像梯度函数

Image Gradients(图像梯度)

1、Sobel and Scharr Derivatives

函数1cv2.Sobel()

函数原型:Sobel(InputArray src,OutputArray dst,int ddepth,int dx,int dy,int ksize = 3,double scale = 1,double delta = 0,int borderType = BORDER_DEFAULT)

参数解析

src    input image.

dst   output image of the same size and the same number of channels as src .

ddepth     output image depth, see combinations; in the case of 8-bit input images it will result in truncated derivatives.

dx     order of the derivative x.

dy     order of the derivative y.

ksize         size of the extended Sobel kernel; it must be 1, 3, 5, or 7.

scale         optional scale factor for the computed derivative values; by default, no scaling is applied (see cv::getDerivKernels for details).

delta         optional delta value that is added to the results prior to storing them in dst.

borderType       pixel extrapolation method, see cv::BorderTypes

特点The Sobel operators combine Gaussian smoothing and differentiation, so the result is more or less resistant to the noise.



函数2cv2.Scharr()

函数原型:Scharr(InputArray src,OutputArray dst,int ddepth,int dx,int dy,double scale = 1,double delta = 0,int borderType = BORDER_DEFAULT)

参数解析

src    input image.

dst   output image of the same size and the same number of channels as src.

ddepth    output image depth, see combinations

dx     order of the derivative x.

dy    order of the derivative y.

scale         optional scale factor for the computed derivative values; by default, no scaling is applied (see getDerivKernels for details).

delta         optional delta value that is added to the results prior to storing them in dst.

borderType     pixel extrapolation method, see cv::BorderTypes

等价函数如下:

Scharr(src, dst, ddepth, dx, dy, scale, delta, borderType)

Sobel(src, dst, ddepth, dx, dy, CV\_SCHARR, scale, delta, borderType).

 

2、Laplacian Derivatives

函数cv2.Laplacian()

函数原型:Laplacian(InputArray src,OutputArray dst,int ddepth,int ksize = 1,double scale = 1,double delta = 0,int borderType = BORDER_DEFAULT)

参数理解:

src    Source image.

dst   Destination image of the same size and the same number of channels as src .

ddepth     Desired depth of the destination image.

ksize         Aperture size used to compute the second-derivative filters. See getDerivKernels for details. The size must be positive and odd.

scale         Optional scale factor for the computed Laplacian values. By default, no scaling is applied. See getDerivKernels for details.

delta         Optional delta value that is added to the results prior to storing them in dst .

borderType       Pixel extrapolation method, see cv::BorderTypes

 

#  -*- coding: utf-8 -*-
# http://docs.opencv.org/3.3.0/d5/d0f/tutorial_py_gradients.html
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('../../image/dushu-opencv.png',0)
laplacian = cv2.Laplacian(img,cv2.CV_64F)
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5)
sobely = cv2.Sobel(img,cv2.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()

3、One Important Matter.

在上面几个梯度转换过程中,有个输出类型的参数,比如cv2.CV_8U或者np.uint8。这个参数有个地方需要注意,详细解析请参考如下:

Black-to-White transition is taken as Positive slope (it has a positive value) while White-to-Black transition is taken as a Negative slope (It has negative value). So when you convert data to np.uint8, all negative slopes are made zero. In simple words, you miss that edge.

If you want to detect both edges, better option is to keep the output datatype to some higher forms, like cv2.CV_16S, cv2.CV_64F etc, take its absolute value and then convert back to cv2.CV_8U.

 

import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('../../image/black-and-white.png',0)
# Output dtype = cv2.CV_8U
sobelx8u = cv2.Sobel(img,cv2.CV_8U,1,0,ksize=5)
# abs(CV_8U)
sobel_8u = np.absolute(sobelx8u)
sobel_8u_2 = np.uint8(sobel_8u)

# Output dtype = cv2.CV_64F. Then take its absolute and convert to cv2.CV_8U
sobelx64f = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5)
# abs(CV_64F)
abs_sobel64f = np.absolute(sobelx64f)
abs_sobel64f_2 = np.uint8(abs_sobel64f)

plt.subplot(3,3,1),plt.imshow(img,cmap = 'gray')
plt.title('Original'), plt.xticks([]), plt.yticks([])

plt.subplot(3,3,2),plt.imshow(sobelx8u,cmap = 'gray')
plt.title('Sobel CV_8U'), plt.xticks([]), plt.yticks([])

plt.subplot(3,3,3),plt.imshow(sobel_8u,cmap = 'gray')
plt.title('Sobel abs(CV_8U)'), plt.xticks([]), plt.yticks([])

plt.subplot(3,3,4),plt.imshow(sobel_8u_2,cmap = 'gray')
plt.title('Sobel abs(CV_8U) to uint8'), plt.xticks([]), plt.yticks([])

plt.subplot(3,3,5),plt.imshow(sobelx64f,cmap = 'gray')
plt.title('Sobel CV_64F'), plt.xticks([]), plt.yticks([])

plt.subplot(3,3,6),plt.imshow(abs_sobel64f,cmap = 'gray')
plt.title('Sobel abs(CV_64F)'), plt.xticks([]), plt.yticks([])

plt.subplot(3,3,7),plt.imshow(abs_sobel64f_2,cmap = 'gray')
plt.title('Sobel abs(CV_64F) to uint8'), plt.xticks([]), plt.yticks([])
plt.show()

源码地址:E:\IdeaProjects_Python\faceprojects\imageProcessing\imageGradients

4、Reference Materials

资料1(Sobel函数):

http://docs.opencv.org/3.3.0/d4/d86/group__imgproc__filter.html#gacea54f142e81b6758cb6f375ce782c8d

资料2(Scharr函数):

http://docs.opencv.org/3.3.0/d4/d86/group__imgproc__filter.html#gaa13106761eedf14798f37aa2d60404c9

资料3(Laplaceian函数):

http://docs.opencv.org/3.3.0/d4/d86/group__imgproc__filter.html#gad78703e4c8fe703d479c1860d76429e6

资料4:

http://docs.opencv.org/3.3.0/d5/d0f/tutorial_py_gradients.html

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: