Reputation: 45
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) << " ";
Upvotes: 0
Views: 2599
Reputation: 2511
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);
Upvotes: 3
Reputation: 628
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.
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) << " ";
Upvotes: 1