[英]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: 我尝试将Image转换为灰度,然后将该打印结果矩阵转换为文件,但出现异常:
"Unhandled exception at 0x00007FF965E31F28 in ConsoleApplication1.exe: Microsoft C++ exception: cv::Exception at memory location 0x00000041305AF2A0.
“ ConsoleApplication1.exe中0x00007FF965E31F28处未处理的异常:Microsoft C ++异常:内存位置0x00000041305AF2A0处的cv :: Exception。
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. 如果发生某些情况,OpenCV函数将引发异常。 You can see them if you will put your code inside try-catch block:
如果将代码放入try-catch块中,则可以看到它们:
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
您期望具有64位double值,但是您的灰度Mat it 8位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. 我不知道为什么您必须读取图像然后将其转换为灰度,而OpenCV支持在通过枚举CV_LOAD_IMAGE_GRAYSCALE读取图像时将图像转换为灰度。
http://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imread#imread 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. 接下来,您使用的默认读取将以BGR格式将图像读取为CV_8U通道。 You don't have to allocate grayImg, cvtColor will do it for you.
您不必分配grayImg,cvtColor会为您完成。
http://docs.opencv.org/2.4/modules/imgproc/doc/miscellaneous_transformations.html#cvtcolor 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) << " ";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.