This code only throws an exception in Debug mode. In Release, it gives the expected output of 0.
#include <opencv2\opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(){
Mat image;
image = Mat::zeros(5,5,CV_8UC1);
try{
cout<< image.at<unsigned int>(1,1)<<"\n";
}
catch(Exception ex){
cout<< ex.msg;
}
cin.get();
return 0;
}
The text of the exception thrown is
OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)i0 < (unsigned)si ze.p[0] && (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channel s()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3 ) - 1))*4) & 15) == elemSize1()) in unknown function, file c:\users\tim\document s\code\opencv\build\include\opencv2\core\mat.hpp, line 537
Version of OpenCV is 2.4.6, and the executable is dynamically linked to the debug library.
The exception happened because you defined image as array of unsigned char but used unsigned int inside at<> function. at<> must get same type as your matrix, i.e. unsigned char. Otherwise it throws an exception you see.
When you providing unsigned char to cout function, it assumes that you are trying to print a character (char) not a number. If you want to see its numeric value cast it to int:
cout << (int)image.at < unsigned char > (1,1) << "\n";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With