简体   繁体   中英

TypeError: expected a single-segment buffer object

I am trying to use OpenCV and Python to detect circular shapes in a webcam capture. Im using a Hough transform for the circle detection, which in it self took several hours for me to figure out (and which I still am unsure if I really have). Anyways, my current problem lies in using the correct type of objects in different function calls. I have posted my code below for reference. When I run this code i get the following error

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

What does this mean? I have tried to look around different threads to figure this oout, but I can't seem to find a good explanation.

I am new to OpenCV and would really appreciate any simple elaboration on what could be the cause my problem. Thanks in advance.

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)

Incomplete answer, but the error means that the function cv.Canny() is expecting one of its first two arguments to be a "single-segment buffer", which is an obscure name to mean "a piece of memory that is contiguous". It might mean that one of the arguments should actually be just a string, for example. It might also mean that it's ok to pass in images created with cv.CreateImage() but only as long as there is no padding on the lines (if there is padding, it can't be represented as a single contiguous piece of memory). Try with an image width that is a multiple of some power of 2, eg a multiple of 16.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM