0

I have a very simple code to calculate PSNR, when I run it I am getting an exception.

void psnr()
{
    try
    {
        Mat img = imread("TestImage.png");
        Mat imgGray, imgCanny;

        Canny(img, imgCanny, 15, 150);

        cout << endl << "PSNR " << cv::PSNR(img, imgCanny);
    }
    catch (cv::Exception& ex)
    {
        cout << "Error " << ex.msg;
    }

    waitKey(0);    
}

I am getting expection:

Error OpenCV(4.5.2) C:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\norm.cpp:1279: error: (-215:Assertion failed) _src1.type() == _src2.type() in function 'cv::PSNR'

What could be the reason for the exception?

Pranit Kothari
  • 9,721
  • 10
  • 61
  • 137

1 Answers1

0

It's most likely that img format is 3 channels BGR format and imgCanny is single channel (Grayscale format).

The assertion message says:

(-215:Assertion failed) _src1.type() == _src2.type()

The meaning is that the type of _src1 and the type of _src2 are not the same.
When cv::PSNR expects the type of the input images to be the same.

For getting the Mat type read the following post.


A simple solution is converting img to Grayscale format:

cv::Mat grayImg;
cv::cvtColor(img, grayImg, cv::COLOR_BGR2GRAY);
cout << endl << "PSNR " << cv::PSNR(grayImg, imgCanny);
Rotem
  • 30,366
  • 4
  • 32
  • 65