如何使用openCV和其他库在python中找到两个视频文件的PSNR和SSIM? [英] How to find PSNR and SSIM of two video files in python using openCV and other libraries?

查看:389
本文介绍了如何使用openCV和其他库在python中找到两个视频文件的PSNR和SSIM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用openCv和numpy在python中找出两个视频文件的PSNR和SSIM. 如何在python中找到PSNR

I want to find out PSNR and SSIM of two video files in python using openCv and numpy. How to find PSNR in python

我尝试了以下用于SSIM的代码

I tried below code for SSIM

# compute the Structural Similarity Index (SSIM) between the two
# images, ensuring that the difference image is returned
(score, diff) = compare_ssim(grayA, grayB, full=True)
diff = (diff * 255).astype("uint8")
print("SSIM: {}".format(score))

# threshold the difference image, followed by finding contours to
# obtain the regions of the two input images that differ
thresh = cv2.threshold(diff, 0, 255,
        cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
        cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]

# loop over the contours

for c in cnts:
        # compute the bounding box of the contour and then draw the
        # bounding box on both input images to represent where the two
        # images differ
        (x, y, w, h) = cv2.boundingRect(c)
        cv2.rectangle(imageA, (x, y), (x + w, y + h), (0, 0, 255), 2)
        cv2.rectangle(imageB, (x, y), (x + w, y + h), (0, 0, 255), 2)

推荐答案

您可以逐帧读取视频帧,并使用此函数计算帧之间的相似度并找到均值.

You can read the video frames by frames and use this function to compute similarity between frames and find mean.

确保提供图像的完整路径.

Make sure you provide full path of the image.

def compare(ImageAPath, ImageBPath):
    img1 = cv2.imread(ImageAPath)          # queryImage
    img2 = cv2.imread(ImageBPath)
    image1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
    image2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)          # trainImage


    score, diff = compare_ssim(image1, image2, full=True,  multichannel=False)
    print("SSIM: {}".format(score))

如果您的图像是彩色的,并且您不想使用灰色图像,请通过

If you Image is colourful and you don't wish to use gray image, pass

multichannel=True

这篇关于如何使用openCV和其他库在python中找到两个视频文件的PSNR和SSIM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆