Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV error for accessing cv::Mat data

Tags:

c++

opencv

mat

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?

like image 497
Zoran Avatar asked Nov 18 '25 12:11

Zoran


1 Answers

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).

like image 67
Chris Avatar answered Nov 20 '25 04:11

Chris