Python OpenCV 报错 error: (-215:Assertion failed)
error: (-215:Assertion failed) !_image.empty() && _image.type() == (((0) & ((1 << 3) - 1)) + (((1)-1) << 3)) && (_image.isMat() || _image.isUMat()) in function 'cv::Hou
·
error: (-215:Assertion failed) !_image.empty() && _image.type() == (((0) & ((1 << 3) - 1)) + (((1)-1) << 3)) && (_image.isMat() || _image.isUMat()) in function 'cv::HoughCircles'
出现在用Python cv2 实现霍夫圆检测过程中
原代码
img = cv2.imread("pipe.jpg")
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 5)
出现这种错误,原因是没有正确导入图像
可能原因:
- 图片路径不正确
- 路径或文件名有中文
- 文件数量或格式不符
这里的原因是第三点,经过试验,霍夫圆检测文件输入应为灰度图,所以有以下两种方式:
cv2.imread时flag=0
img = cv2.imread("pipe.jpg", 0)
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 5)
或者进行灰度转换
img = cv2.imread("pipe.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 5)
更多推荐




所有评论(0)