search
LoginSignup
12

More than 3 years have passed since last update.

posted at

updated at

画像品質評価のPSNR, SSIMの実装と速度比較

背景

from skimage.measure import compare_ssim, compare_psnr

をすると
compare_ssimcompare_psnrでSSIMとPSNRの評価ができる。
速度比較がしたい。

速度比較

from skimage.measure import compare_ssim, compare_psnr
import cv2
import time

def measurement(func, **kwargs):
    start = time.time()
    val = func(kwargs["img1"], kwargs["img2"])
    end = time.time()
    return val, end-start

img1 = cv2.imread("sample1.png", cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread("sample2.png", cv2.IMREAD_GRAYSCALE)
print("ssim: %f, time: %lf[sec]" % measurement(compare_ssim, img1=img1, img2=img2))
print("psnr: %f, time: %lf[sec]" % measurement(compare_psnr, img1=img1, img2=img2))

# => ssim: 0.974289, time: 0.806180[sec]
# => psnr: 21.250949, time: 0.053643[sec]

結論

わかってはいたけどPSNRの方がめっちゃ早い。(15倍ぐらい)

おまけ

画像間の同じ座標のピクセルと値との差の合計pixel_diffとの比較をしてみる。

from skimage.measure import compare_ssim, compare_psnr
import cv2
import time
import numpy as np

def pixel_diff(img1, img2):
    return np.sum(np.absolute(img1 - img2))

def measurement(func, **kwargs):
    start = time.time()
    val = func(kwargs["img1"], kwargs["img2"])
    end = time.time()
    return val, end-start

img1 = cv2.imread("sample1.png", cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread("sample2.png", cv2.IMREAD_GRAYSCALE)
print("ssim: %f, time: %lf[sec]" % measurement(compare_ssim, img1=img1, img2=img2))
print("psnr: %f, time: %lf[sec]" % measurement(compare_psnr, img1=img1, img2=img2))
print("pixel_diff: %f, time: %lf[sec]" % measurement(compare_psnr, img1=img1, img2=img2))

# => ssim: 0.996097, time: 0.734509[sec]
# => psnr: 29.721666, time: 0.046622[sec]
# => pixel_diff: 29.721666, time: 0.045121[sec]

参考

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
12