gpt4 book ai didi

python - Python-OpenCv FindContours

转载 作者:行者123 更新时间:2023-12-02 17:53:24 31 4
gpt4 key购买 nike

我尝试在二进制图像中找到轮廓,但是当尝试执行cvFindContours时,它给了我该错误消息

Traceback (most recent call last): File "convert.py", line 30, in contour = cvFindContours(img2, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE) File "/usr/lib/pymodules/python2.7/opencv/cv.py", line 580, in cvFindContours count, seq = cvFindContoursUntyped( *args ) File "/usr/lib/pymodules/python2.7/opencv/cv.py", line 6521, in cvFindContoursUntyped return _cv.cvFindContoursUntyped(*args) RuntimeError: openCV Error: Status=Incorrect size of input array function name=cvStartFindContours error message= file_name=/build/buildd/opencv-2.1.0/src/cv/cvcontours.cpp line=205



我使用全新安装的Ubuntu 11.10和Opencv 2.3.1。

这是我的源代码
from opencv.cv import *
from opencv.highgui import *

image = cvLoadImage('test.png')
def getthresholdedimg(image):
size = cvSize(640, 480)

imghsv=cvCreateImage(cvGetSize(image),8,3)
cvCvtColor(image,imghsv,cv.CV_BGR2HSV)
imgblue=cvCreateImage(cvGetSize(image),8,1)
imgblue2=cvCreateImage(cvGetSize(image),8,1)
imgthreshold=cvCreateImage(cvGetSize(image),8,1)

cvInRangeS(imghsv,cvScalar(100,100,100),cvScalar(120,255,255),imgblue)
cvInRangeS(imghsv,cvScalar(100,100,100),cvScalar(120,255,255),imgblue2)
cvAdd(imgblue,imgblue,imgthreshold)
return imgthreshold


cvFlip(image,image,1)

cvSmooth(image, image, CV_GAUSSIAN, 3, 0)
imgthresh=getthresholdedimg(image)
cvErode(imgthresh,imgthresh,None,3)
cvDilate(imgthresh,imgthresh,None,10)
storage = cvCreateMemStorage(0)
img2=cvCloneImage(imgthresh)
contour = cvFindContours(img2, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE)

我的原始和二进制图像

原始图片
http://tinypic.com/r/34rul9x/6

和蓝色过滤的二进制图像
http://tinypic.com/r/ifbotx/6

最佳答案

在python cv2中查找轮廓

将numpy导入为np
导入cv2

img = cv2.imread('sample_image.jpg')
imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
_,轮廓,层次结构= cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

cv2.drawContours(img,轮廓,-1,(0,255,0),3)
cv2.imshow('img',img)
cv2.waitKey(0)

关于python - Python-OpenCv FindContours,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14085855/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com