OpenCV mat :: at throw exception

Tim*_*Tim 5 c++ windows opencv visual-c++

此代码仅在调试模式下引发异常.在Release中,它给出了预期的输出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;
}
Run Code Online (Sandbox Code Playgroud)

抛出的异常文本是

OpenCV错误:断言失败(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())在未知函数中,文件c:\ users\tim\document\code\opencv\build\include\opencv2\core\mat.hpp,第537行

OpenCV的版本是2.4.6,可执行文件动态链接到调试库.

Mic*_*nov 5

  1. 发生异常是因为您将image定义为unsigned char数组,但在<> function中使用了unsigned int.at <>必须与矩阵相同,即unsigned char.否则它会抛出你看到的异常.

  2. 当您向cout函数提供unsigned char时,它假定您尝试打印字符(char)而不是数字.如果要查看其数值,请将其强制转换为int:

    cout <<(int)image.at <unsigned char>(1,1)<<"\n";