Python OpenCV迭代通过轮廓错误

时间:2012-06-29 18:11:50

标签: python opencv

我正在使用OpenCV来分析网络摄像头拍摄的照片。我然后裁剪,并将图像转换为只有黑色和白色。然后我想找到图像中的所有轮廓,并确定最大的轮廓。

我在Ubuntu OS的终端窗口中运行代码

以下是相关代码:

num_contours, contours = cvFindContours(contourimg, # num_contours - # of contours
                          cvCreateMemStorage(0),    # and contours are the actual contours
                          sizeof_CvContour)         # cvFindContours() destroys contourimg

max_size = float("-inf")
max_contour = None

for contour in contours.hrange():           # contours.hrange() returns a list of all contours
    print contour
    size = abs(cvContourArea(contour))      # calculates the area of the bounding rectangle
    if size > max_size:                             
        max_size = size                     # update max_size if larger contour found
        max_contour = contour               # update max_contour if larger contour found

当我运行代码时,我得到了不同的错误。一个是Segmentation故障,它退出python并返回终端,但另一个是:

 File "fitness.py", line 74, in boundingimg
size = abs(cvContourArea(contour))      # calculates the area of the bounding rectangle
File "/usr/lib/pymodules/python2.6/opencv/cv.py", line 6741, in cvContourArea
return _cv.cvContourArea(*args)

RuntimeError:  openCV Error:
    Status=Bad argument
    function name=cvPointSeqFromMat
    error message=Input array is not a valid matrix
    file_name=/build/buildd/opencv-2.1.0/src/cv/cvutils.cpp
    line=53

我发现这很奇怪,因为我有所有轮廓的打印声明,并且它们都工作正常,for循环持续很长时间才会抛出此错误。我不确定这个轮廓与其他轮廓有什么不同。

此外,有时(通常不会)代码有效,因此每次都不会发生错误。

0 个答案:

没有答案