简体   繁体   中英

OpenCV error for accessing cv::Mat data

For any Mat type that I use with allowable types, I get an an error like below when I try to do some assignment or write to stream etc. It happens on MSVC++ 2010 express compiler, it does not happen with gnu g++ compiler.

example flawed usage:

Mat M = Mat::zeros( image.size(),  DataType<int>::type  );

std::cout << M.at<int>( 0,0 ) << std::endl; // error

// OR

int x = M.at<int>( 0,0 ); // error

the two errors fired together:

on popup window

Unhandled exception at <some hex adress> in test.exe:Microsoft C++ exception: cv:xception at memory location <some hex adress>

and on console window

OpenCV Error: Assertion failed ... \mat.hpp, line 537

Any suggestions?

Make the matrix data type CV_16U .

The .at accessor functions are very meticulous, requiring very exact data types. Some compilers ignore these issues, while others catch it early.

Rather than referencing the elements with matrix.at<int>(row, col) , CV_16U makes reference to the unsigned short data type. Therefore, the elements can be accessed with matrix.at<unsigned short>(row, col) .

I don't believe openCV has an int data type image.
The integer type is generally CV_16s, ie 16bit short, so use

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