背景
from skimage.measure import compare_ssim, compare_psnr
をすると
compare_ssim
とcompare_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]