OpenCV3.3.0 findContours错误 [英] OpenCV3.3.0 findContours error

查看:448
本文介绍了OpenCV3.3.0 findContours错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天重新安装了opencv,并运行我之前编写的代码. 我得到了错误:

I reinstall opencv today, and run my code I've written before. I got the error:

OpenCV错误:在findContours文件/tmp/opencv-20170916-中断言失败(_contours.empty()||(_contours.channels()== 2& _amp__contours.depth()== CV_32S)) 1776行,87764-1y5vj25/opencv-3.3.0/modules/imgproc/src/contours.cpp 追溯(最近一次通话): 在第12行的文件"pokedex.py"中 (cnts,_)= cv2.findContours(灰色,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE,(2,2)) cv2.error:/tmp/opencv-20170916-87764-1y5vj25/opencv-3.3.0/modules/imgproc/src/contours.cpp:1894:错误:(-215)_contours.empty()||函数findContours中的(_contours.channels()== 2& __ contours.depth()== CV_32S)

OpenCV Error: Assertion failed (_contours.empty() || (_contours.channels() == 2 && _contours.depth() == CV_32S)) in findContours, file /tmp/opencv-20170916-87764-1y5vj25/opencv-3.3.0/modules/imgproc/src/contours.cpp, line 1894 Traceback (most recent call last): File "pokedex.py", line 12, in (cnts, _) = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE, (2,2)) cv2.error: /tmp/opencv-20170916-87764-1y5vj25/opencv-3.3.0/modules/imgproc/src/contours.cpp:1894: error: (-215) _contours.empty() || (_contours.channels() == 2 &&_contours.depth() == CV_32S) in function findContours

该代码在opencv2.4.1.3.3.中可以正常工作.

The code works fine with opencv2.4.13.3.

image = cv2.imread("test.jpg")

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)    // `len(gray.shape)` is 2.

(cnts, _) = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE, (2,2))

版本信息:opencv 3.3.0,python 2.7.13,macOS 10.13

推荐答案

  1. 什么是(2,2)? findContours()的第四个位置参数是输出contours数组.但是,您没有为contours数组(这是点数组)传递有效格式.如果应该是offset,并且您不想提供其他位置参数,则需要使用诸如offset=(2,2)之类的关键字来调用它.这是实际错误的原因.我不确定为什么它在以前的版本中有效,因为它以相同的顺序接受了相同的参数,而Python一直都是这样.如果参数是可选的,则需要提供足够的位置参数直到参数为止,或者为其提供关键字.

  1. What is (2,2)? The fourth positional argument for findContours() is the output contours array. But you're not passing it a valid format for the contours array (which is an array of points). If it's supposed to be the offset and you don't want to provide the additional positional arguments, you need to call it by keyword like offset=(2,2). This is the reason for the actual error. I'm not sure why this worked in the previous version, as it accepted the same arguments in the same order, and Python has been like this forever; if the arguments are optional, you need to provide enough positional arguments up to the argument, or provide it a keyword.

findContours()在OpenCV 3中返回三个值(在OpenCV 2中,它只是两个值),contours是第二个返回值;应该是

findContours() returns three values in OpenCV 3 (in OpenCV 2 it was just two values), contours is the second return value; should be

_, contours, _ = findContours(...) 

此外,您不必包装在python中的tuple中即可进行分配,您只需执行x, y, z = fun()即可,而无需执行(x, y, z) = fun().另外,您可以通过将结果编入索引来要求第二个返回值,例如

Also you don't have to wrap into a tuple in python for assignment, you can just do x, y, z = fun(), no need to do (x, y, z) = fun(). Additionally you could just ask for the second return value by indexing the result like

contours = cv2.findContours(...)[1]

所以这应该使您神清气爽

So this should clear you up:

cnts = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE, offset=(2,2))[1]

这些针对OpenCV 3的文档具有Python语法,因此如果您之前的其他代码有任何中断,则可以浏览那里,并查看语法是否已更改.

These docs for OpenCV 3 have the Python syntax, so you can browse there if any of your other prior code breaks, and see if the syntax has changed.

这篇关于OpenCV3.3.0 findContours错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆