简体   繁体   中英

cv2.imwrite( ) only saves last image

I am trying to build a short script to take multiple images with my stereo-camera and save them to a directory when I press a button.

But for some reason I only get the last image, even if I've taken multiple pictures. It also shows no errors and prints out the right strings as I've written in my code. But all I get is the last image pair.

I've looked at several posts but none of them have the same problem.

EDIT: I didnt add Seconds to the image names as everyone here suggested, so the images got overwritten as they were taken during the same minute.

Here is my finished code now if anyone wants to use it:

import numpy as np
import cv2
import os
import datetime

# shape of wholeFrame: (240, 640, 3)

cap = cv2.VideoCapture(1)
now = 0
imgs_taken = 0

newpath_l = "Recorded_Images/left_imgs"
newpath_r = "Recorded_Images/right_imgs"
newpath = "Recorded_Images/both_imgs"

if not os.path.exists(newpath_l):
    os.makedirs(newpath_l)
if not os.path.exists(newpath_r):
    os.makedirs(newpath_r)
if not os.path.exists(newpath):
    os.makedirs(newpath)

while 1:
    cap.grab()
    ret, wholeFrame = cap.retrieve()

    if ret:

        leftFrame = wholeFrame[:, 0:320, :]
        rightFrame = wholeFrame[:, 320:640, :]

        # Rectifying images here

        stereoImage = np.concatenate((leftFrame, rightFrame), axis=1)

        cv2.imshow('Take Snapshots', stereoImage)
        key = cv2.waitKey(1) & 0xFF

        # Saving images on keypress with timestamp
        if key == ord('p'):
            now = datetime.datetime.now()
            if not cv2.imwrite(newpath_l + now.strftime("/img_left_%d%m%Y_%H%M%S.png"), leftFrame):
                print("Left Snapshot not taken")
            else:
                print("Left Snapshot taken.")

            if not cv2.imwrite(newpath_r + now.strftime("/img_right_%d%m%Y_%H%M%S.png"), rightFrame):
                print("Right Snapshot not taken")
            else:
                print("Right Snapshot taken.")

            if not cv2.imwrite(newpath + now.strftime("/img_both_%d%m%Y_%H%M%S.png"), stereoImage):
                print("Stereo-Snapshot not taken")
            else:
                print("Stereo-Snapshot taken.")

            imgs_taken = imgs_taken + 1

        if key == ord('x'):
            print("Number of images taken: " + str(imgs_taken))
            break
    else:
        break

cv2.destroyAllWindows()
cap.release()

There is no problem with cv2.imwrite itself, but rather in how you name the frames that you are saving. You are naming the frames as Day + Month + Year _ Hour + Minute. This means any frame you save within a given minute will be overridden by the last frame saved in that minute. Saving a frame at, say, 19:00:23 will be overridden by a frame saved at 19:00:34. Depending on your use-case, you could add + now.strftime("%S") to be able to save one frame every second, or you could even add + now.strftime("%S_%f") for millisecond precision.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM