首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenCV 3.0.0 beta:绘制等高线错误

OpenCV 3.0.0 beta:绘制等高线错误
EN

Stack Overflow用户
提问于 2015-01-06 05:23:10
回答 1查看 3.3K关注 0票数 1

我在OpenCV 3.0.0 beta文档中取样有关轮廓的入门,andI在尝试使用文档中的等高线代码时遇到了多个错误。我不知道怎么解决这个问题,有人能帮我吗?

我从:begin.html#contours-getting-started复制代码的文档

测试图片以应用轮廓:

http://www.k6-geometric-shapes.com/image-files/pyramid-base-rectangle.jpg

生成错误的代码行:

代码语言:javascript
复制
cv2.drawContours(img, contours, 3, (0,255,0), 3)

错误:

代码语言:javascript
复制
Traceback (most recent call last):
  File "/home/anthony/Documents/Programming/Python/Computer-Vision/OpenCV-Doc/contour-draw.py", line 13, in <module>
    cv2.drawContours(img, contours, 3, (0,255,0), 3)
error: /home/anthony/Downloads/opencv-3.0.0-beta/modules/imgproc/src/drawing.cpp:2160: error: (-215) 0 <= contourIdx && contourIdx < (int)last in function drawContours

还有另一个drawContour函数不能工作。一旦我注释掉上面的一个并在另一个函数中进行注释(下面),它就会产生不同的错误。

代码行:

代码语言:javascript
复制
cnt = contours[4]
cv2.drawContours(img, [cnt], 0, (0,255,0), 3)

错误:回溯(最近一次调用):文件"/home/anthony/Documents/Programming/Python/Computer-Vision/OpenCV-Doc/contour-draw.py",第15行,在cnt = contours4 IndexError:列表索引超出范围

还有另外两个drawContours函数被注释掉。有一个边框可以成功地创建图片的轮廓,但出于某些原因,它会生成窗口的轮廓。

代码:

代码语言:javascript
复制
import numpy as np
import cv2

img = cv2.imread('rectangle-pink.jpg')
imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
_,contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

#Displays the border
#cv2.drawContours(img, contours, -1, (0,255,0), 3)


cv2.drawContours(img, contours, 3, (0,255,0), 3)

#cnt = contours[4]
#cv2.drawContours(img, [cnt], 0, (0,255,0), 3)

cv2.imshow('Contour Pic', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
EN

回答 1

Stack Overflow用户

发布于 2016-07-13 10:12:19

在我的例子中,如果我在某个点使用索引作为len(contours)-1,则len(contours)变为零,并显示了错误:

indexError:列出超出范围的索引

如果我使用以下代码,一切都正常。

代码语言:javascript
复制
if len(contours)>0 :
   cnt=contours[len(contours)-1]
   cv2.drawContours(img, [cnt], 0, (0,255,0), 3)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27792543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档