简体   繁体   English

访问多维 cv::Mat 数组的元素

[英]Access elements of multidimentional cv::Mat array

I cannot figure out how to properly access elements of a 3D cv::Mat array.我无法弄清楚如何正确访问 3D cv::Mat 数组的元素。 The following code runs in Vivado HLS and fails with a non-descriptive error.以下代码在 Vivado HLS 中运行,但因非描述性错误而失败。 Is this a problem with the Vivado HLS, or I am not properly reading values?这是 Vivado HLS 的问题,还是我没有正确读取值?

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

Error:错误:

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

Indeed there are some issues with Mat::at<T> when using multidimensional data.实际上,使用多维数据时Mat::at<T>存在一些问题。 take a look at : Post看一看:帖子

i recommend accessing pixels directly without using Mat::at<T> :我建议不使用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;
}

*Note : this is an easy way to access Mat but if you want efficiency use data pointer. *注意:这是访问 Mat 的简单方法,但如果您想提高效率,请使用数据指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM