2013-05-05 83 views
1

我正尝试使用OpenCV和Python来检测摄像头捕获中的圆形形状。我使用霍夫变换来进行圆圈检测,在这个过程中,我花了几个小时才弄清楚(而且我还不确定是否真的有)。无论如何,我目前的问题在于在不同的函数调用中使用正确类型的对象。我已经发布了我的代码以供参考。当我运行此代码我碰到下面的错误TypeError:预期单段缓冲区对象

Traceback (most recent call last): File "test1.py", line 19, in <module> cv.Canny(gray, edges, 50, 200, 3) TypeError: expected a single-segment buffer object

这是什么意思?我试图围绕不同的线索来找出这个问题,但我似乎无法找到一个好的解释。

我是OpenCV的新手,非常感谢任何关于可能是我的问题的原因的简单阐述。提前致谢。

import cv 
import cv2 
import numpy as np 

#Starting camera capture 
capture = cv.CaptureFromCAM(0) 

while True: 
    img = cv.QueryFrame(capture) 

    #Allocating grayscale- and edge-images 
    gray = cv.CreateImage(cv.GetSize(img), 8, 1) 
    edges = cv.CreateImage(cv.GetSize(img), 8, 1) 

    #Transforming frame to grayscale image 
    cv.CvtColor(img, gray, cv.CV_BGR2GRAY) 

    #Preprocessing and smoothing 
    cv.Erode(gray, gray, None, 2) 
    cv.Dilate(gray, gray, None, 2) 
    cv.Smooth(gray, gray, cv.CV_GAUSSIAN, 9, 9) 

    #Edge detection (I believe this is where the exception is thrown) 
    cv.Canny(gray, edges, 50, 200, 3) 

    #Transforming original frame and grayscale image to numpy arrays 
    img = np.asarray(img[:,:]) 
    gray = np.asarray(gray[:,:]) 

    #Detecting circles and drawing them 
    circles = cv2.HoughCircles(gray,cv.CV_HOUGH_GRADIENT,1,10,100,30,5,20) 
    circles = np.uint16(np.around(circles)) 
    for i in circles[0,:]: 
     cv2.circle(img,(i[0],i[1]),i[2],(0,255,0),1) # draw the outer circle 
     cv2.circle(img,(i[0],i[1]),2,(0,0,255),3)  # draw the center of the circle 

    #Transforming original frame back to iplimage format for showing 
    img = cv.fromarray(img) 

    #Showing image and edge image 
    cv.ShowImage("Camera", img) 
    cv.ShowImage("Edges",edges) 

回答

0

不完整的答案,但该误差意味着cv.Canny()期待其头两个参数之一的功能是一个“单段缓冲”,这是一个不起眼的名称来表示“一块内存即连续的”。例如,这可能意味着其中一个参数实际上应该只是一个字符串。这也可能意味着传递用cv.CreateImage()创建的图像是可以的,但只要线条上没有填充(如果存在填充,则不能将其表示为单个连续片段的存储器)。尝试将图像宽度设置为2的某个幂的倍数,例如16的倍数。