简体   繁体   中英

Have exception in OpenCV when converting an image to grayscale

Have strange error.
I try to convert Image to grayscale and,after that print result matrix to file, but get exception:

"Unhandled exception at 0x00007FF965E31F28 in ConsoleApplication1.exe: Microsoft C++ exception: cv::Exception at memory location 0x00000041305AF2A0.

Following code is below.
Can somebody say me when I made mistake?

int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    string fname;
    cin >> fname;

    cv::Mat img = readImage(fname);

    cv::Mat grayImg(img.size(), CV_64FC1);
    if (img.channels() == 3)
    {
        cvtColor(img, grayImg, CV_BGR2GRAY);
    }
    else
    {
        img.copyTo(grayImg);
    }
    printImg(grayImg);

    cv::waitKey();
    return 0;
}
void printImg(cv::Mat &img)
{
    cout << "---------//------\n";
    if (img.empty())
    {
        cout << "Empty Image\n";
        return;
    }

    for (int i = 0; i < img.size().height; i++)
    {
        for (int j = 0; j < img.size().width; j++)
        {
            cout << img.at<double>(i, j) << " ";
        }
        cout << endl;
    }
    cout << "---------//------\n";
}

error in string

cout << img.at<double>(i, j) << " ";

OpenCV functions are throwing exceptions if something happens. You can see them if you will put your code inside try-catch block:

int main() try
{
    // your code here
}
catch(const std::exception& e)
{
    std::cout << e.what() << std::endl;
}

When something bad happens - just look into terminal output and you will undertsand the reason.

UPDATE: After you got the error message - its easy to solve it. You are expecting to have 64-bit double values, but your greyscale Mat it 8-bit unsigned char

I sugeest this changes in your code that should help:

cv::Mat grayImg;
if (img.channels() == 3)
    cvtColor(img, grayImg, CV_BGR2GRAY);
else if (img.channels() == 4)
    cvtColor(img, grayImg, CV_BGRA2GRAY);
else grayImg = img;
// here grayImg is 8-bit unsigned char
// change it to doubles:
cv::Mat gray64bit;
grayImg.convertTo(gray64bit, CV_64FC1);
printImg(gray64bit);

I don't know why you have to read an image then convert it to grayscale while OpenCV support convert image to grayscale when reading it by enum CV_LOAD_IMAGE_GRAYSCALE.

http://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imread#imread

Next, the default imread you use will read image as BGR as CV_8U channel. You don't have to allocate grayImg, cvtColor will do it for you.

http://docs.opencv.org/2.4/modules/imgproc/doc/miscellaneous_transformations.html#cvtcolor

the grayImg will have same depth and size as original. So your

cout << img.at<double>(i, j) << " ";

produce the error. It should be

cout << img.at<uchar>(i, j) << " ";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM