Image frame saving from Video without losing resolution and quality

Viewed 789

I want to save the images extracted from the video without losing any information, resolution and quality. I have saved using OpenCV in four formats png, bmp, jpg, tif The code is as below

    file = "video.MP4"
    video = cv2.VideoCapture(file)    
    # Find OpenCV version
    (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
    
    # With webcam get(CV_CAP_PROP_FPS) does not work.
    # Let's see for ourselves.
    
    if int(major_ver)  < 3 :
        fps = video.get(cv2.cv.CV_CAP_PROP_FPS)
        print ("Frames per second using video.get(cv2.cv.CV_CAP_PROP_FPS): {0}".format(fps))
    else :
        fps = video.get(cv2.CAP_PROP_FPS)
        print ("Frames per second using video.get(cv2.CAP_PROP_FPS) : {0}".format(fps))
    

    # Number of frames to capture
    num_frames = 120;
    
    
    print ("Capturing {0} frames".format(num_frames))

    # Start time
    start = time.time()
    
    data = []
    # Grab a few frames
    for i in range(0, num_frames) :
        ret, frame = video.read()
        data.append(frame)
    print("Shape",frame.shape)
    # End time
    end = time.time()

    # Time elapsed
    seconds = end - start
    print ("Time taken : {0} seconds".format(seconds))

    # Calculate frames per second
    fps  = num_frames / seconds;
    print ("Estimated frames per second : {0}".format(fps))

    for i,img in enumerate(data):
        fileName = 'Frames\img_'+ str(i) + '.tif'
        cv2.imwrite(fileName,img)
    # Release video
    video.release()

My Questions are

  1. In which of the format (png, bmp, jpg, tif) I should save so that it should not lose the resolution and quality of video.
  2. Just putting extension like .tif in cv2.imwrite() saves it to that particular format?
  3. Which library is the best to save OpenCV, scikit-image or Pillow

Any help or suggestion will be highly appreciated.

Regards

1 Answers

well .jpg is a lossy format so you can loose some information. So it'd be better to use .png format to save the video frames. Because in this case you can also set the IMWRITE_PNG_COMPRESSION level. In cv2 its value is from 0 to 9. A higher value means higher compression level and longer time. This value defaults to 1 in cv2 so you can set it to 0 if you don't want to compress it at all. A example code is as follow,


# lets say you stored a single video frame in variable named 'frame' then
# save it as follows to avoid any compression or loss of information

png_compression_level = 0 # for no compression what so ever (means larger file size)
cv2.imwrite(dest_dir_path, frame,
           [int(cv2.IMWRITE_PNG_COMPRESSION), png_compression_level])

you can then also verify if the stored image lost any information or not by using a pip package called sewar

img = cv2.imread('read the frame you saved as iamge')
# then
from sewar.full_ref import rmse, psnr, uqi, ssim

print("RMSE: ", rmse(img, frame)) # 0 is ideal value
print("PSNR: ", psnr(img, frame)) # inf is ideal value
print("SSIM: ", ssim(img, frame)) # (1, 1) is ideal value
print("UQI: ", uqi(img, frame))   # 1 is the ideal value
Related