Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV mat::at throwing exception

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.

like image 538
Tim Avatar asked Dec 08 '13 06:12

Tim


1 Answers

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

  2. 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";

like image 128
Michael Burdinov Avatar answered Oct 18 '22 07:10

Michael Burdinov