多维cv :: Mat数组的访问元素 [英] Access elements of multidimentional cv::Mat array

查看:347
本文介绍了多维cv :: Mat数组的访问元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何正确访问3D cv :: Mat数组的元素.以下代码在Vivado HLS中运行,但由于非描述性错误而失败. Vivado HLS是否存在问题,还是我无法正确读取值?

I cannot figure out how to properly access elements of a 3D cv::Mat array. The following code runs in Vivado HLS and fails with a non-descriptive error. Is this a problem with the Vivado HLS, or I am not properly reading values?

cv::Mat img = cv::Mat(cv::Size(100,100),CV_MAKETYPE(CV_8U,5));   // should create a 100x100x5 array

uchar x;
x = img.at<uchar>(0,0,0);    // works fine when reading from third dimension at 0
x = img.at<uchar>(0,0,1);    // fails when reading from third dimension at 1

错误:

@E Simulation failed: SIGSEGV.
ERROR: [SIM 211-100] CSim failed with errors.

推荐答案

实际上,使用多维数据时Mat::at<T>存在一些问题. 看看:发布

Indeed there are some issues with Mat::at<T> when using multidimensional data. take a look at : Post

我建议不使用Mat::at<T>直接访问像素:

i recommend accessing pixels directly without using Mat::at<T> :

int main(int argc, char** argv)
{

   cv::Mat img = cv::Mat(cv::Size(5, 5), CV_MAKETYPE(CV_8U, 5));  

   std::cout << "Matrix = " << " " << std::endl << img <<std::endl;

   for (unsigned int band = 0; band < img.channels(); band++) {
        for (unsigned int row = 0; row < img.rows; row++) {
             for (unsigned int col = 0; col < img.cols; col++) {


            int  PixelVal = static_cast<int>(img.data[img.channels()*(img.cols*col + row) + band]);
            std::cout << PixelVal << std::endl;

        }
    }
}



return 0;
}

*注意:这是访问Mat的简便方法,但如果要提高效率,请使用数据指针.

*Note : this is an easy way to access Mat but if you want efficiency use data pointer.

这篇关于多维cv :: Mat数组的访问元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆