How to find PSNR and SSIM of two video files in python using openCV and other libraries?

混江龙づ霸主 提交于 2019-12-18 09:29:23

问题


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

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)

回答1:


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


来源:https://stackoverflow.com/questions/48657765/how-to-find-psnr-and-ssim-of-two-video-files-in-python-using-opencv-and-other-li

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!