我正在尝试使用 opencv 将来自网络摄像头的帧保存为 jpg 或 png 或其他格式。事实证明它比我想象的要困难,尽管这里有这样的例子:Capturing a single image from my webcam in Java or Python
我正在尝试这样做:
if __name__ == "__main__":
print "Press ESC to exit ..."
# create windows
cv.NamedWindow('Raw', cv.CV_WINDOW_AUTOSIZE)
cv.NamedWindow('Processed', cv.CV_WINDOW_AUTOSIZE)
# create capture device
device = 0 # assume we want first device
capture = cv.CaptureFromCAM(0)
cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, 640)
cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, 480)
# check if capture device is OK
if not capture:
print "Error opening capture device"
sys.exit(1)
while 1:
# do forever
# capture the current frame
frame = cv.QueryFrame(capture)
if frame is None:
break
# mirror
cv.Flip(frame, None, 1)
# face detection
detect(frame)
# display webcam image
cv.ShowImage('Raw', frame)
# handle events
k = cv.WaitKey(10)
if k == 0x1b: # ESC
print 'ESC pressed. Exiting ...'
break
if k == 0x63 or k == 0x43:
print 'capturing!'
s, img = capture.read()
if s:
cv.SaveImage("r'C:\test.jpg", img)
如您所见,我尝试使用 Froyo 在另一个问题中建议的代码的修改,使其在按下字母 c 时捕获图像。它不起作用,我找不到使它起作用的文档。
请帮忙!非常感谢,亚历克斯