《python+opencv3.3视频教学 基础入门》图像梯度 笔记
作者:互联网
参考:https://blog.csdn.net/saltriver/article/details/78987096
Sobel算子
参考:https://blog.csdn.net/qq_29540745/article/details/51918004
拉普拉斯算子
图像深度
视频示例:
import cv2 as cv
import numpy as np
# Sobel算子
def sobel_demo(image):
grad_x = cv.Sobel(image, cv.CV_32F, 1, 0) # 因为计算后的梯度可能会超过像素最大值255,所以这里需要指定合适的图像深度,如cv.CV_32F
grad_y = cv.Sobel(image, cv.CV_32F, 0, 1)
# grad_x = cv.Scharr(image, cv.CV_32F, 1, 0) # Scharr算子是Sobel算子的增强版本,当Sobel得到的轮廓不明显时可考虑Scharr算子,然后做图像二值化,即可提取到边缘
# grad_y = cv.Scharr(image, cv.CV_32F, 0, 1) # 但是Scharr算子对噪声比较敏感,当用Scharr算子得到轮廓后,要想方设法把噪声降下来
gradx = cv.convertScaleAbs(grad_x) # 把梯度取绝对值,并 scaling 到无符号8位最大值255的范围内
grady = cv.convertScaleAbs(grad_y)
cv.imshow("gradient-x", gradx) # 查看梯度图效果
cv.imshow("gradient-y", grady)
gradxy = cv.addWeighted(gradx, 0.5, grady, 0.5, 0)
cv.imshow("gradient-xy", gradxy)
# Laplas算子
def laplacian_demo(image):
dst = cv.Laplacian(image, cv.CV_32F)
lpls = cv.convertScaleAbs(dst)
cv.imshow("laplacian_demo", lpls)
def custom_kernel_demo(image):
# kernel = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]]) # 这个核就是拉普拉斯核,会看到效果和laplacian_demo()的一样
kernel = np.array([[1, 1, 1], [1, -8, 1], [1, 1, 1]])
dst = cv.filter2D(image, cv.CV_32F, kernel=kernel)
lpls = cv.convertScaleAbs(dst)
cv.imshow("laplacian_demo", lpls)
print("-----------Python OpenCV Tutorial--------------")
src = cv.imread("C:/cv-samples/data/lena.jpg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
# sobel_demo(src)
# laplacian_demo(src)
custom_kernel_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()
标签:32F,python,梯度,image,算子,CV,demo,cv,opencv3.3 来源: https://blog.csdn.net/rrrrrr89098087/article/details/110195073