我正在尝试使用 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
这是什么意思?我试图环顾不同的线程来解决这个问题,但我似乎找不到一个好的解释。
我是 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)