grad_x = cv2.Sobel(image.astype(np.uint8), cv2.CV_32F, 1, 0) #对x求一阶导
grad_y = cv2.Sobel(image.astype(np.uint8), cv2.CV_32F, 0, 1) #对y求一阶导
gradx = cv2.convertScaleAbs(grad_x) #用convertScaleAbs()函数将其转回原来的uint8形式
grady = cv2.convertScaleAbs(grad_y)
d = np.arctan2(grad_y, grad_x)/(2*np.pi)+0.5
print(np.max(d),np.min(d))
1.0 0.051208198
plt.figure(figsize=(10,10))
plt.imshow(d)
plt.show()

本文介绍如何使用OpenCV库对图像进行梯度计算,包括对图像在x和y方向上的一阶导数计算,并将得到的梯度方向转换为可视化图像。通过Sobel算子提取图像的水平和垂直边缘,再利用arctan2函数计算每个像素点的梯度方向,最后展示方向角的分布。
355

被折叠的 条评论
为什么被折叠?



