【问题标题】:Save multiple images with OpenCV and Python使用 OpenCV 和 Python 保存多个图像
【发布时间】:2013-06-15 02:05:42
【问题描述】:

我正在使用 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 

【问题讨论】:

    标签: python opencv webcam


    【解决方案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】:

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

      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
      

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

      【讨论】:

        【解决方案3】:

        基于 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)
        

        【讨论】:

          【解决方案4】:

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

          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
          

          【讨论】:

          • 这很完美,但是当我运行它时,它会继续拍照。有没有办法只拍5张?
          • 然后,使用While True:,而不是for i in range(5):。
          • 是的,成功了。但它只创建 5 个图像,然后一遍又一遍地覆盖相同的 5 个文件。
          • 您在评论中询问了have it only take 5 pictures? 的方式。
          • 如果你想在每次运行中防止覆盖,使文件名使用简单计数器以外的其他名称。 (例如,时间戳)。
          猜你喜欢
          • 2020-07-26
          • 1970-01-01
          • 2022-01-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-26
          • 2021-12-27
          • 1970-01-01
          相关资源
          最近更新 更多