OpenCV version 4.1.0 drawContours(OpenCV 4.1.0版drawContours)
问题描述
我的以下代码在 OpenCV 3.4.1 上运行良好,但现在不适用于 OpenCV 4.1.0 并出现错误.我不知道如何使代码适应较新的版本,你能帮我吗?非常感谢
I have the following code that worked well with OpenCV 3.4.1 and now is not working with OpenCV 4.1.0 and gives an error. I do not know how to adapt the code with the newer version, can you help me with that? Thanks a lot
def ImageProcessing(image):
image = cv2.absdiff(image, background)
h, gray = cv2.threshold(image, 65, 255, cv2.THRESH_BINARY_INV);
gray = cv2.medianBlur(gray,5)
kernel = np.ones((3,3), np.uint8)
gray = cv2.erode(gray, kernel, iterations=1)#1
des = cv2.bitwise_not(gray)
tmp = cv2.findContours(des,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)
contour, hier = tmp[1], tmp[0]
for cnt in contour:
cv2.drawContours(des,[cnt],0,255,-1)
gray = cv2.bitwise_not(des)
gray = cv2.dilate(gray, kernel, iterations=1)#1
return gray
错误是
cv2.error: OpenCV(4.1.0)/io/opencv/modules/imgproc/src/drawing.cpp:2509: 错误: (-215:Assertion failed) npoints > 0 in function 'drawContours'
cv2.error: OpenCV(4.1.0) /io/opencv/modules/imgproc/src/drawing.cpp:2509: error: (-215:Assertion failed) npoints > 0 in function 'drawContours'
推荐答案
根据 OpenCV 版本,cv2.findContours() 有不同的返回签名.
Depending on the OpenCV version, cv2.findContours() has varying return signatures.
在 OpenCV 3.4.X 中,cv2.findContours() 返回 3 个项目
In OpenCV 3.4.X, cv2.findContours() returns 3 items
image, contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]])
在 OpenCV 4.1.X 中,cv2.findContours() 返回 2 项
In OpenCV 4.1.X, cv2.findContours() returns 2 items
contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]])
无论版本如何,您都可以轻松获取轮廓:
You can easily obtain the contours regardless of the version like this:
tmp = cv2.findContours(des,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)
contours = tmp[0] if len(tmp) == 2 else tmp[1]
这篇关于OpenCV 4.1.0版drawContours的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:OpenCV 4.1.0版drawContours
基础教程推荐
- 求两个直方图的卷积 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 包装空间模型 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
