我正在尝试使用 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 时捕获图像。它不起作用,我找不到使其起作用的文档。
求助! 多谢, 亚历克斯
最佳答案
如下更改您的保存部分:
if k == 0x63 or k == 0x43:
print 'capturing!'
cv.SaveImage("test.jpg",frame)
它对我很有用。由于您已经捕获了用于检测的帧,因此您需要再次捕获它以保存此帧。
cv.CaptureFromCam() 和 cv.VideoCapture() 也不同。它们不能混淆。
关于python - 使用 opencv python 绑定(bind)将帧从网络摄像头保存到磁盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11372718/