针对arcLength的Python错误的OpenCV [英] OpenCV with Python error for arcLength
问题描述
我的代码中存在问题,无法为其找到合适的解决方案。我使用的是Python 2.7.10和OpenCV 3.0。我读了两张图片,想要将其中一张图片(模板)与另一张图片相匹配,但我收到以下错误:
I have an issue in my code and can't find a proper solution for it. I am using Python 2.7.10 and OpenCV 3.0. I read two images and want to match one of the pictures(a template) with the contours from the other but I get the following error:
error: (-215) count >= 0 && (depth == CV_32F || depth == CV_32S) in function cv::arcLength
我的代码看起来像这样:
My code looks like this:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.bilateralFilter(gray, 11, 10, 17)
edges = cv2.Canny(gray, 100, 20)
contours,hierarchy, _ = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
ret = cv2.matchShapes(c, compare, 1, 0.0)
if ret < 0.5:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
灰度和比较图像均为灰度。错误显然是说我需要我的数组是浮点数或双数,但我不知道如何在python中转换它,我发现很多例子,函数工作,代码似乎几乎相同。
Both gray and compare images are grayscale. The error is obviously saying that I need my array to be of floats or double, but I got no idea how to convert it in python and I've found many examples with the function working and the code seems almost the same.
此外,在大多数应用程序中,我注意到在大多数示例中,findContours()函数返回2个值,但如果我不给它3,我会收到错误。
Furthermore, in most applications I've noticed that on most examples the findContours() functions returns 2 values, but I get an error if I don't give it 3.
请帮我找到问题!
推荐答案
这个错误实际意味着什么是你的图像是32位浮点数而不是整数。特别是8位无符号整数。我相信,这对应于OpenCV所理解的灰度图像。您可以使用其他类型的基础数字类型,它仍将以灰度模式显示在查看器中,但要了解您使用的查看器将重新调整它(安静地)以显示它。
What that error actually means is that your image is in 32 bit floats and not integers. Specifically 8 bit unsigned integers. I believe, this corresponds to a grayscale image as its understood by OpenCV. You can have other types of underlying number types and it will still display in grayscale mode in a viewer but understand that the viewer you use will rescale it (quietly) to display it.
我建议您尝试的是:
gray_image = cv2.convertScaleAbs(img)
不知道返回的东西是怎么回事。
Not sure what is going on with the return thing.
我也道歉,我误解你要求cv2并且你问cv3。我认为它应该足够相似,给我一些时间来检查它。
Also I apologize, I misread that you're asking for cv2 and you're asking cv3. I think it should be similar enough, give me some time to check it out.
OpenCV3有berak在这个问题中回答了一些不同的语法:
opencv 3 beta / python中的findContours和drawContours错误
OpenCV3 has a bit of a different syntax as answered in this question by berak: findContours and drawContours errors in opencv 3 beta/python
opencv 3略有此处更改
语法
,返回值差异:
opencv 3 has a slightly changed syntax here, the return values differ:
cv2.findContours(image, mode, method[, contours[, hierarchy[, offset]]]) → image, contours, hierarchy
这篇关于针对arcLength的Python错误的OpenCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!