原文链接:http://www.juzicode.com/python-error-opencv-sobel-ksize-must-be-larger-than-order
错误提示:
OpenCV Sobel计算图像边沿报error: (-215:Assertion failed) ksize > order in function ‘cv::getSobelKernels’
#VX公众号:桔子code / juzicode.com
import cv2
print('cv2.__version__:',cv2.__version__)
img_src = cv2.imread('sudoku.png' ,cv2.IMREAD_GRAYSCALE)
grad = cv2.Sobel(img_src,cv2.CV_16S,3,0,ksize=3)
abs_grad = cv2.convertScaleAbs(grad)
cv2.imshow('abs_grad',abs_grad)
cv2.waitKey(0)
cv2.destroyAllWindows()
==========运行结果:
cv2.__version__: 4.5.3
---------------------------------------------------------------------------
error Traceback (most recent call last)
<ipython-input-8-d4c225430785> in <module>
4
5 img_src = cv2.imread('sudoku.png' ,cv2.IMREAD_GRAYSCALE)
----> 6 grad = cv2.Sobel(img_src,cv2.CV_16S,3,0,ksize=3)
7 abs_grad = cv2.convertScaleAbs(grad)
8 cv2.imshow('abs_grad',abs_grad)
error: OpenCV(4.5.3) E:\juzicode\opencv-4.5.3\modules\imgproc\src\deriv.cpp:115: error: (-215:Assertion failed) ksize > order in function 'cv::getSobelKernels'
错误原因:
1、计算sobel kernel时,必须要求ksize的值大于dx或dy的值。
解决方法:
1、修改Sobel()调用,将ksize的值设置为大于dx的值:cv2.Sobel(img_src,cv2.CV_16S,3,0,ksize=5)
#VX公众号:桔子code / juzicode.com
import cv2
print('cv2.__version__:',cv2.__version__)
img_src = cv2.imread('sudoku.png' ,cv2.IMREAD_GRAYSCALE)
grad = cv2.Sobel(img_src,cv2.CV_16S,3,0,ksize=5) # ksize大于dx
abs_grad = cv2.convertScaleAbs(grad)
cv2.imshow('abs_grad',abs_grad)
cv2.waitKey(0)
cv2.destroyAllWindows()
扩展内容:
如果本文还没有完全解决你的疑惑,你也可以在微信公众号“桔子code”后台给我留言,欢迎一起探讨交流。