The peak signal-to-noise ratio (PSNR) is an engineering term that represents the maximum possible power and the ratio of destructive noise power that affects its representation accuracy. Because many signals have a very wide dynamic range, the peak signal-to-noise ratio is commonly expressed in logarithmic decibel units.
In image processing, it is necessary to evaluate the image objectively, and Psnr is often needed. Psnr is an objective criterion for measuring image distortion or noise level. The larger the Psnr value between 2 images, the more similar. The general benchmark is 30db,30db below the image deterioration is more obvious.
Psnr Definition:
WhereMax represents the maximum value of the image color, the 8bit image has a maximum value of 255. The MSE is the mean variance, defined as:
whereI and K resolution is the original image and the processed image, them*n is the size of the volume image.
/********************************************
* Input format is Cv::mat type, I1,I2 represents two images of input
*
/Double Getpsnr ( Const mat& I1, const mat& I2)
{
Mat s1;
Absdiff (I1, I2, S1); // | i1-i2| The Absdiff function is a function
S1.convertto (S1, cv_32f) that calculates the absolute value of two array differences in OpenCV. Here we use the cv_32f to calculate because 8-bit unsigned char is not possible to perform the square calculation
S1 = S1.mul (S1); // | I1-i2|^2
Scalar s = SUM (S1); For each channel plus
double SSE = s.val[0] + s.val[1] + s.val[2];//SUM channels
if (SSE <= 1e-10) //For very small values we will be approximately equal to 0
return 0;
else {
Double MSE =sse/(Double) (I1.channels () * i1.total ()); MSE
Double Psnr = 10.0*log10 ((255*255)/mse);
return Psnr;
}
}