Opencv-python image gradient calculation-Sobel operator, Scharr operator, laplacian operator

1 Sobel operator

insert image description here
dst = cv2.Sobel(src, ddepth, dx, dy, ksize)

  • ddepth: the depth of the image
  • dx and dy represent the horizontal and vertical directions, respectively
  • ksize is the size of the Sobel operator
import cv2
import numpy as np

img = cv2.imread('data/pie.png', cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (400, 400))


def cv_show(img, name):
    cv2.imshow(name, img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


sobelX = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
cv_show(sobelX, 'sobelX')

insert image description here
take the absolute value

import cv2
import numpy as np

img = cv2.imread('data/pie.png', cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (400, 400))


def cv_show(img, name):
    cv2.imshow(name, img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


sobelX = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
sobelY = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)
# 白到黑是正数,黑到白是负数,所有的负数会被截断成0,所以要取绝对值
sobelX = cv2.convertScaleAbs(sobelX)
sobelY = cv2.convertScaleAbs(sobelY)
cv_show(sobelX, 'sobelX')
cv_show(sobelY, 'sobelY')

insert image description here

insert image description here

import cv2
import numpy as np


def cv_show(im, name):
    cv2.imshow(name, im)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


img = cv2.imread('data/test2.jpg', cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (400, 400))

sobelX = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
sobelY = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)

# 白到黑是正数,黑到白是负数,所有的负数会被截断成0,所以要取绝对值
sobelX = cv2.convertScaleAbs(sobelX)
sobelY = cv2.convertScaleAbs(sobelY)
cv_show(sobelX, 'sobelX')
cv_show(sobelY, 'sobelY')

# 分别计算x和y,再求和
sobelXY = cv2.addWeighted(sobelX, 0.5, sobelY, 0.5, 0)
cv_show(sobelXY, 'sobelXY')

# 直接计算x和y(不推荐)
sobel_XY = cv2.Sobel(img, cv2.CV_64F, 1, 1, ksize=3)
cv_show(sobel_XY, 'sobel_XY')

Only calculate the X direction
insert image description here
Only calculate the Y direction
insert image description here
X direction + Y direction
insert image description here
X direction and Y direction are calculated together
insert image description here

2 Scharr operator

more sensitive to change
insert image description here

3 Laplacian operator

Second-order derivation, sensitive to noise
insert image description here

import cv2
import numpy as np


def cv_show(im, name):
    cv2.imshow(name, im)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


img = cv2.imread('data/test2.jpg', cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (400, 400))

sobelX = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
sobelY = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)

# 白到黑是正数,黑到白是负数,所有的负数会被截断成0,所以要取绝对值
sobelX = cv2.convertScaleAbs(sobelX)
sobelY = cv2.convertScaleAbs(sobelY)

# 分别计算x和y,再求和
sobelXY = cv2.addWeighted(sobelX, 0.5, sobelY, 0.5, 0)

# 直接计算x和y(不推荐)
scharrX = cv2.Scharr(img, cv2.CV_64F, 1, 0)
scharrY = cv2.Scharr(img, cv2.CV_64F, 0, 1)
scharrX = cv2.convertScaleAbs(scharrX)
scharrY = cv2.convertScaleAbs(scharrY)
scharrXY = cv2.addWeighted(scharrX, 0.5, scharrY, 0.5, 0)

laplacian = cv2.Laplacian(img, cv2.CV_64F)
laplacian = cv2.convertScaleAbs(laplacian)

res = np.hstack([img, sobelXY, scharrXY, laplacian])
cv_show(res, 'res')

insert image description here

Guess you like

Origin blog.csdn.net/qq_40507857/article/details/126679273