diff --git a/samples/cpp/tutorial_code/gpu/gpu-basics-similarity/gpu-basics-similarity.cpp b/samples/cpp/tutorial_code/gpu/gpu-basics-similarity/gpu-basics-similarity.cpp index c80799828c..879fda86fc 100644 --- a/samples/cpp/tutorial_code/gpu/gpu-basics-similarity/gpu-basics-similarity.cpp +++ b/samples/cpp/tutorial_code/gpu/gpu-basics-similarity/gpu-basics-similarity.cpp @@ -181,14 +181,13 @@ double getPSNR(const Mat& I1, const Mat& I2) 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.0*log10((255*255)/mse); - return psnr; - } + double mse = sse /(double)(I1.channels() * I1.total()); + + // For very small SSE, add epsilon to approximate infinite PSNR (~361 dB) + if( sse <= 1e-10) mse+= DBL_EPSILON; + + double psnr = 10.0*log10((255*255)/mse); + return psnr; } //! [getpsnr] @@ -206,14 +205,13 @@ double getPSNR_CUDA_optimized(const Mat& I1, const Mat& I2, BufferPSNR& b) double sse = cuda::sum(b.gs, b.buf)[0]; - 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; - } + double mse = sse /(double)(I1.channels() * I1.total()); + + // For very small SSE, add epsilon to approximate infinite PSNR (~361 dB) + if (sse <= 1e-10) mse += DBL_EPSILON; + + double psnr = 10.0*log10((255*255)/mse); + return psnr; } //! [getpsnropt] @@ -234,14 +232,13 @@ double getPSNR_CUDA(const Mat& I1, const Mat& I2) Scalar s = cuda::sum(gs); double sse = s.val[0] + s.val[1] + s.val[2]; - if( sse <= 1e-10) // for small values return zero - return 0; - else - { - double mse =sse /(double)(gI1.channels() * I1.total()); - double psnr = 10.0*log10((255*255)/mse); - return psnr; - } + double mse = sse /(double)(gI1.channels() * I1.total()); + + // For very small SSE, add epsilon to approximate infinite PSNR (~361 dB) + if( sse <= 1e-10) mse+= DBL_EPSILON; + + double psnr = 10.0*log10((255*255)/mse); + return psnr; } //! [getpsnrcuda] diff --git a/samples/cpp/tutorial_code/videoio/video-input-psnr-ssim/video-input-psnr-ssim.cpp b/samples/cpp/tutorial_code/videoio/video-input-psnr-ssim/video-input-psnr-ssim.cpp index 8d567b2f5e..4d9082be65 100644 --- a/samples/cpp/tutorial_code/videoio/video-input-psnr-ssim/video-input-psnr-ssim.cpp +++ b/samples/cpp/tutorial_code/videoio/video-input-psnr-ssim/video-input-psnr-ssim.cpp @@ -144,8 +144,8 @@ double getPSNR(const Mat& I1, const Mat& I2) double sse = s.val[0] + s.val[1] + s.val[2]; // sum channels - if( sse <= 1e-10) // for small values return zero - return 0; + if( sse <= 1e-10) // For very small values, return 360 to cap PSNR (theoretical value is infinity) + return 360.0; else { double mse = sse / (double)(I1.channels() * I1.total()); diff --git a/samples/python/tutorial_code/videoio/video-input-psnr-ssim.py b/samples/python/tutorial_code/videoio/video-input-psnr-ssim.py index 973854feb6..17037b2433 100644 --- a/samples/python/tutorial_code/videoio/video-input-psnr-ssim.py +++ b/samples/python/tutorial_code/videoio/video-input-psnr-ssim.py @@ -16,7 +16,7 @@ def getPSNR(I1, I2): s1 = s1 * s1 # |I1 - I2|^2 sse = s1.sum() # sum elements per channel if sse <= 1e-10: # sum channels - return 0 # for small values return zero + return 360 # For very small SSE, return 360 to cap PSNR (theoretical value is infinity) else: shape = I1.shape mse = 1.0 * sse / (shape[0] * shape[1] * shape[2])