繁体   English   中英

如何将第一帧与视频的其他帧进行比较 opencv python

[英]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()

我希望视频的第一帧与其他视频帧进行比较,并根据第一帧计算 ssim(相似度)。 如何做这个搜索很多但没有得到想要的结果。

@Ceopee 是对的,该教程中没有错误。

这个想法很简单:

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

代码:


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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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