Python错误集锦:OpenCV Canny()计算图像边缘时提示canny.cpp:846: error: (-206:Bad flag (parameter or structure field)) Aperture size should be odd between 3 and 7 in function ‘cv::Canny’

原文链接:http://www.juzicode.com/python-error-opencv-canny-aperture-size-should-be-odd-between-3-and-7

错误提示:

opencv用Canny()计算图像边缘时提示canny.cpp:846: error: (-206:Bad flag (parameter or structure field)) Aperture size should be odd between 3 and 7 in function ‘cv::Canny’

#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.Canny(img_gray,80,160,apertureSize=6)
cv2.imshow('img_edge',img_edge)
cv2.waitKey(0)
cv2.destroyAllWindows()
==========运行结果:
cv2.__version__: 4.5.2
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
&lT;ipython-input-8-c6fe439e694d> in &lT;module>
      4 img = cv2.imread('lena.jpg')
      5 img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
----> 6 img_edge = cv2.Canny(img_gray,80,160,apertureSize=6)
      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\canny.cpp:846: error: (-206:Bad flag (parameter or structure field)) Aperture size should be odd between 3 and 7 in function 'cv::Canny'

错误原因:

1、apertureSize入参必须是3-7的奇数,也就是只能取值为3,5,7

解决方法:

1、修改apertureSize为3,5或7:

#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.Canny(img_gray,80,160,apertureSize=5) #修改apertureSize为3,5或7
cv2.imshow('img_edge',img_edge)
cv2.waitKey(0)
cv2.destroyAllWindows()

扩展内容:

  1. OpenCV-Python教程


如果本文还没有完全解决你的疑惑,你也可以在微信公众号“桔子code”后台给我留言,欢迎一起探讨交流。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注