原文链接:http://www.juzicode.com/opencv-error-laplacian-kernel-size-must-be-odd-not-larger-than-31
错误提示:
OpenCV Laplacian()提示deriv.cpp:104: error: (-211:One of the arguments’ values is out of range) The kernel size must be odd and not larger than 31 in function ‘cv::getSobelKernels’
#VX公众号:桔子code / juzicode.com
import cv2
print('cv2.__version__:',cv2.__version__)
img = cv2.imread('lena.jpg')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img_edge = cv2.Laplacian(img_gray,cv2.CV_8U,ksize=4)
cv2.imshow('img_edge',img_edge)
cv2.waitKey(0)
cv2.destroyAllWindows()
==========运行结果:
cv2.__version__: 4.5.2
---------------------------------------------------------------------------
error Traceback (most recent call last)
<ipython-input-4-16cba4f12541> in <module>
4 img = cv2.imread('lena.jpg')
5 img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
----> 6 img_edge = cv2.Laplacian(img_gray,cv2.CV_8U,ksize=4)
7 cv2.imshow('img_edge',img_edge)
8 cv2.waitKey(0)
error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-kuwfz3h3\opencv\modules\imgproc\src\deriv.cpp:104: error: (-211:One of the arguments' values is out of range) The kernel size must be odd and not larger than 31 in function 'cv::getSobelKernels'
错误原因:
1、Laplacian()、Sobel()等计算图像边缘时ksize必须为奇数,且不能大于31
解决方法:
1、ksize设置为奇数,比如5:
#VX公众号:桔子code / juzicode.com
import cv2
print('cv2.__version__:',cv2.__version__)
img = cv2.imread('lena.jpg')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img_edge = cv2.Laplacian(img_gray,cv2.CV_8U,ksize=5) #ksize设置为奇数
cv2.imshow('img_edge',img_edge)
cv2.waitKey(0)
cv2.destroyAllWindows()
扩展内容:
如果本文还没有完全解决你的疑惑,你也可以在微信公众号“桔子code”后台给我留言,欢迎一起探讨交流。