我正嘗試使用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)