使用OpenCV和Python保存多个图像

时间:2013-06-15 02:05:42

标签: python opencv webcam

我正在使用OpenCV和Python来拍摄图像。但是目前我一次只能拍一张照片。我想让OpenCV拍多张照片。这是我目前的代码。

import cv2.cv as cv
import time

cv.NamedWindow("camera", 1)

capture = cv.CaptureFromCAM(0)

while True:
    img = cv.QueryFrame(capture)
    cv.ShowImage("camera", img)
    cv.SaveImage('pic.jpg', img)
    if cv.WaitKey(10) == 27:
        break 

4 个答案:

答案 0 :(得分:3)

您的代码会覆盖文件。每次保存到不同的文件。 例如:

import cv2.cv as cv
import time

cv.NamedWindow("camera", 1)

capture = cv.CaptureFromCAM(0)

i = 0
while True:
    img = cv.QueryFrame(capture)
    cv.ShowImage("camera", img)
    cv.SaveImage('pic{:>05}.jpg'.format(i), img)
    if cv.WaitKey(10) == 27:
        break
    i += 1

答案 1 :(得分:1)

将要保存的图像名称更改为“[图像名称] [每次循环后增加的数字]” 通过这样做,您的图像将在每个循环后以新名称存储..否则所有图像将覆盖相同的名称!

import cv2.cv as cv
import time

cv.NamedWindow("camera", 1)

capture = cv.CaptureFromCAM(0)
num = 0
while True:
    img = cv.QueryFrame(capture)
    cv.ShowImage("camera", img)
    cv.SaveImage('pic'+str(num)+'.jpg', img)
    if cv.WaitKey(10) == 27:
        break
    num += 1

现在您的图片将保存为pic0.jpg,pic1.jpg,pic2.jpg等等。

答案 2 :(得分:1)

基于c ++绑定界面,您想要做的最小示例。

import cv2

cpt = 0
maxFrames = 5 # if you want 5 frames only.

try:
    vidStream = cv2.VideoCapture(0) # index of your camera
except:
    print "problem opening input stream"
    sys.exit(1)

while cpt < maxFrames:
    ret, frame = vidStream.read() # read frame and return code.
    if not ret: # if return code is bad, abort.
        sys.exit(0)
    cv2.imshow("test window", frame) # show image in window
    cv2.imwrite("image%04i.jpg" %cpt, frame)
    cpt += 1

脚本的完整示例,能够从相机索引或文件中读取。包括一些故障和一些有关读取设备的信息。

用法:record.py [来源] [目标文件夹]

#!/usr/bin/env python
import cv2
import sys
import os

cpt = 0
maxFrames = 30

try:
    targetDir = sys.argv[2]
except:
    targetDir = "" # if no argument, then use current directory

try: # read input. eval if to transform video index to int
    vidStream = cv2.VideoCapture(eval(sys.argv[1]))
except:
    print "problem opening input stream"
    sys.exit(1)
if not vidStream.isOpened():
    print "capture stream not open"
    sys.exit(1)

# informations in case the input is a video file.
nFrames = vidStream.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)
print "frame number: %s" %nFrames
fps = vidStream.get(cv2.cv.CV_CAP_PROP_FPS)
print "FPS value: %s" %fps

# note that we could use frame number here, or "while 1"
# so we could read from a live written file or capture devide.
while cpt < maxFrames:
    ret, frame = vidStream.read() # read frame and return code.
    if not ret:
        print "end of stream"
        sys.exit(0)
    cv2.imshow("test window", frame) # show image in window
    cv2.imwrite(os.path.join(targetDir, "image_%04i.jpg" %cpt), frame)
    cpt += 1
    keyPressed = cv2.waitKey(1) # time to wait between frames
    if keyPressed != -1: # if user pressed a key, stop recording.
        sys.exit(0)

答案 3 :(得分:1)

我认为这会有所帮助...

import cv2

vid = cv2.VideoCapture("video.mp4")
d = 0
ret, frame = vid.read()

while ret:
    ret, frame = vid.read()
    filename = "images/file_%d.jpg"%d
    cv2.imwrite(filename, frame)
    d+=1

这将以不同的名称保存每一帧。