首页 文章

无法访问从OpenCV函数返回的Mat值

提问于
浏览
0

我正在使用cv :: imread加载图像并对该图像执行一些处理,但我不知道为什么我无法从imread函数读取返回的Mat的值 . 我用Mat.at方法:

Mat iplimage = imread("Photo.jpg",1); //input
    for(int i=0;i<iplimage.rows;i++){
        for(int j=0;j<iplimage.cols;j++){
            cout<<(int)iplimage.at<int>(i,j)<<" ";
        }
        cout<<endl;
    }

但它似乎是一个错误:

OpenCV错误:断言失败(dims <= 2 && data &&(unsigned)i0 <(unsigned)size.p [0] &&(unsigned)(i1 * DataType <_Tp> :: channels)<(unsigned)(size . p [1] * channels())&&((((Sizeof(size_t)<< 28)| 0x8442211)>>((DataType <_Tp> :: depth)&((1 << 3)-1))* 4)&15)== elemSize1())是未知函数,文件:“c:\ opencv2.2 \ include \ opencv2 \ core \ mat.hpp”,第517行

但是如果我使用直接访问方式就可以了:

Mat iplimage = imread("Photo.jpg",1); //input
     for(int i=0;i<iplimage.rows;i++){
         for(int j=0;j<iplimage.cols;j++){
        cout<<(int)iplimage.data[i*iplimage.cols + j]<<" ";                 
         }
    cout<<endl;
     }

谁能告诉我如何使用Mat.at方法访问上面的Mat?谢谢你的帮助!

3 回答

  • 0

    answer . 在你的情况下,返回的Mat是3维的,因此 iplimage.at<int> 无法断言,你只需要像所提到的答案所解释的那样访问每个通道的强度 .

  • 0

    你试图加载3通道的图像,如果你改成这个Mat iplimage = imread(“Photo.jpg”,0)将会很好; //输入

  • 0

    我找到了解决方案 . 这是因为我使用了: inputImage.at<int>(i,j)inputImage.at<float>(1,2) 而不是 (int)inputImage.at<uchar>(1,2)(float)inputImage.at<uchar>(1,2) 对不起我的粗心大意!

相关问题