【问题标题】:Access elements of multidimentional cv::Mat array访问多维 cv::Mat 数组的元素
【发布时间】:2020-04-08 08:51:28
【问题描述】:

我无法弄清楚如何正确访问 3D cv::Mat 数组的元素。以下代码在 Vivado HLS 中运行并失败并出现非描述性错误。这是 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

错误:

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

【问题讨论】:

    标签: opencv opencv-mat vivado-hls


    【解决方案1】:

    在使用多维数据时,Mat::at&lt;T&gt; 确实存在一些问题。 看看:Post

    我建议不使用Mat::at&lt;T&gt; 直接访问像素:

    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 的一种简单方法,但如果您想提高效率,请使用数据指针。

    【讨论】:

    • 没有尝试这种方法,但答案是接受的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 2021-06-11
    相关资源
    最近更新 更多