Save multiple images with OpenCV and Python

前端 未结 4 1667
再見小時候
再見小時候 2021-02-06 17:52

I\'m using OpenCV and Python to take images. However currently I can only take one picture at a time. I would like to have OpenCV to take multiple pictures. This is my current c

相关标签:
4条回答
  • 2021-02-06 18:30

    i think this wil helpful...

    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
    

    this will save every frame with different name.

    0 讨论(0)
  • 2021-02-06 18:37

    A minimal example of what you'd like to do, based on the c++ binded interface.

    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
    

    A full example of script, able to read from a camera index, or a file. Includes some failsafes and some information about read device.

    usage: record.py [source] [target folder]

    #!/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)
    
    0 讨论(0)
  • 2021-02-06 18:42

    Your code overwrite a file. Save to different file each time. For example:

    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
    
    0 讨论(0)
  • 2021-02-06 18:50

    change the name of the image to be saved to " [image name] [a number which increase after every loop] " By doing this your image will be stored with a new name after every loop.. otherwise all the images will overwrite the same name !

    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
    

    now your images will be saved as pic0.jpg, pic1.jpg, pic2.jpg and so on..

    0 讨论(0)
提交回复
热议问题