up42 / image-similarity-measures

:chart_with_upwards_trend: Implementation of eight evaluation metrics to access the similarity between two images. The eight metrics are as follows: RMSE, PSNR, SSIM, ISSM, FSIM, SRE, SAM, and UIQ.
MIT License
550 stars 68 forks source link

psnr #41

Closed kerwinxu closed 2 years ago

kerwinxu commented 2 years ago

1.https://docs.opencv.org/4.x/dd/d3d/tutorial_gpu_basics_similarity.html `

double getPSNR(const Mat& I1, const Mat& I2) { Mat s1; absdiff(I1, I2, s1); // |I1 - I2| s1.convertTo(s1, CV_32F); // cannot make a square on 8 bits s1 = s1.mul(s1); // |I1 - I2|^2 Scalar s = sum(s1); // sum elements per channel double sse = s.val[0] + s.val[1] + s.val[2]; // sum channels if( sse <= 1e-10) // for small values return zero return 0; else { double mse =sse /(double)(I1.channels() I1.total()); double psnr = 10.0log10((255*255)/mse); return psnr; } } `

  1. image-similarity-measures `def psnr(org_img: np.ndarray, pred_img: np.ndarray, max_p=4095) -> float: """ Peek Signal to Noise Ratio, implemented as mean squared error converted to dB.

    It can be calculated as PSNR = 20 log10(MAXp) - 10 log10(MSE)

    When using 12-bit imagery MaxP is 4095, for 8-bit imagery 255. For floating point imagery using values between 0 and 1 (e.g. unscaled reflectance) the first logarithmic term can be dropped as it becomes 0 """ _assert_image_shapes_equal(org_img, pred_img, "PSNR")

    org_img = org_img.astype(np.float32)

    mse_bands = [] for i in range(org_img.shape[2]): mse_bands.append(np.mean(np.square(org_img[:, :, i] - pred_img[:, :, i])))

    return 20 np.log10(max_p) - 10. np.log10(np.mean(mse_bands))`

the same equation (10.0log10((255 255)/mse) = 20 log10(MAXp) - 10 log10(MSE)), but get different result . i don't know why ?

a0 b0

kerwinxu commented 2 years ago

I may have found out what the problem was.