Skip to content
Advertisement

How to compare first frame to other frame of video opencv python

import cv2
cap = cv2.VideoCapture(input_path)

count = 0
n=0
while True:
    ret, frame = cap.read()
    
    if ret:
        # You can do processing on this frame variabqle
        roi = frame[343:489, 572:759]
#         frame = cv2.resize(roi, None, fx=0.9, fy=0.9)
          
        cv2.imshow("roi", roi)
    

    
        cv2.imshow("image", frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
    

    
cv2.destroyAllWindows()

I want the first frame of video to compare with other frames of video and calculate ssim (similarity) based on the first frame. How to do this search a lot but not getting the desired result.

Advertisement

Answer

@Ceopee is right and there is no error in that tutorial.

The idea is simple:

if count is 0
    get the first frame
else
    calculate ssim of frame1 and the next-frame
    display

Code:


import cv2
import numpy as np


def ssim(i1, i2):
    c1 = 6.5025
    c2 = 58.5225
    # INITS
    I1 = np.float32(i1) # cannot calculate on one byte large values
    I2 = np.float32(i2)
    I2_2 = I2 * I2 # I2^2
    I1_2 = I1 * I1 # I1^2
    I1_I2 = I1 * I2 # I1 * I2
    # END INITS
    # PRELIMINARY COMPUTING
    mu1 = cv2.GaussianBlur(I1, (11, 11), 1.5)
    mu2 = cv2.GaussianBlur(I2, (11, 11), 1.5)
    mu1_2 = mu1 * mu1
    mu2_2 = mu2 * mu2
    mu1_mu2 = mu1 * mu2
    sigma1_2 = cv2.GaussianBlur(I1_2, (11, 11), 1.5)
    sigma1_2 -= mu1_2
    sigma2_2 = cv2.GaussianBlur(I2_2, (11, 11), 1.5)
    sigma2_2 -= mu2_2
    sigma12 = cv2.GaussianBlur(I1_I2, (11, 11), 1.5)
    sigma12 -= mu1_mu2
    t1 = 2 * mu1_mu2 + c1
    t2 = 2 * sigma12 + c2
    t3 = t1 * t2                    # t3 = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))
    t1 = mu1_2 + mu2_2 + c1
    t2 = sigma1_2 + sigma2_2 + c2
    t1 = t1 * t2                    # t1 =((mu1_2 + mu2_2 + C1).*(sigma1_2 + sigma2_2 + C2))
    ssim_map = cv2.divide(t3, t1)    # ssim_map =  t3./t1;
    mssim = cv2.mean(ssim_map)       # mssim = average of ssim map
    return mssim


cap = cv2.VideoCapture("b/ex.mp4")

count = 0
frm1 = -1

cv2.namedWindow("frame-1")
cv2.moveWindow("frame-1", 800, 0)

while cap.isOpened():
    ret, frm = cap.read()

    if ret:
        if count == 0:
            frm1 = frm
        else:
            mssimv = ssim(frm1, frm)
            print("nMSSISM: R {}% G {}% B {}%".format(round(mssimv[2] * 100, 2), round(mssimv[1] * 100, 2),
                                                       round(mssimv[0] * 100, 2)), end=" ")
            cv2.imshow("frame-1", frm1)
            cv2.imshow("frame-{}".format(count), frm)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        count += 1
    else:
        break

cap.release()
cv2.destroyAllWindows()
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement