Difference in output of same c++ and Java function in OpenCV

asked 2020-01-12 05:34:37 -0500

Gourav gravatar image

updated 2020-01-12 07:04:25 -0500

I was implementing this tutorial in Java and wrote the "getPsnr" function but the results I get are not same
C++ version from the tutorial

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
cout<<sse<<"\n";
if( sse <= 1e-10) // for small values return zero
    return 0;
else
{
double mse  = sse / (double)(I1.channels() * I1.total());
double psnr = 10.0 * log10((255 * 255) / mse);
return psnr;
}

}

Here is the Java version that I have written

double getPSNR(Mat I1, Mat I2){
        Mat s1 = new Mat();
        Core.absdiff(I1, I2, s1);
        s1.convertTo(s1,CvType.CV_32F);


   s1 = s1.mul(s1);
    Scalar s = Core.sumElems(s1);
    double sse = s.val[0] + s.val[1] + s.val[2];
    System.out.println(sse);
if(sse<=1e-10){
    return 0;
}
else {
    double mse = sse / (double)(I1.channels()*I1.total());
    double psnr = 10.0 * Math.log10(255*255/mse);
    return psnr;
}

}

Even the sse values differ in C++ version for the first frame it is 5523678.000 whereas for the java version it comes out to be 4056725.0

edit retag flag offensive close merge delete