我正在尝试使用OpenCV和Python来检测网络摄像头捕获中的圆形形状。我使用霍夫变换进行圆检测,其中我自己需要花费几个小时来弄清楚(我还不确定我是否真的有)。无论如何,我目前的问题在于在不同的函数调用中使用正确类型的对象。我在下面发布了我的代码以供参考。当我运行此代码时,我收到以下错误
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
这是什么意思?我试图环顾不同的线程来计算这个oout,但我似乎无法找到一个好的解释。
我是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 :(得分:0)
答案不完整,但错误意味着函数cv.Canny()
期望其前两个参数中的一个是“单段缓冲区”,这是一个晦涩的名称,意思是“一段内存是连续的”。例如,这可能意味着其中一个参数实际上应该只是一个字符串。它也可能意味着传递用cv.CreateImage()
创建的图像是可以的,但只要行上没有填充(如果有填充,它不能表示为单个连续的内存) 。尝试使用2的某个幂的倍数的图像宽度,例如, 16的倍数。