Hello I have a problem when grouping the OpenCV’s functions in functions of my own and getting segmentation fault.
Even with code as simple as this
def acquire_imagen():
capture = cv.CaptureFromCAM( 0 )
img = cv.QueryFrame( capture )
return img
img = acquire_image()
print img[0,0]
If I call the same instructions outside the function everything is ok. I have an Idea of what may be happening but not enough knowledge about python to prevent it. I think the object is being freed by the GC.
To prevent the capture object from being garbage-collected, keep a reference to it in a variable until you no longer need the images. In your code: the “capture” variable cannot be a local variable of the function, but a variable outside the function. Or, if you want it to be initialized inside the function, return it along with the captured image, and store it in a variable after the call to the function: