Access elements of multidimentional cv::Mat array

陌路散爱 提交于 2020-01-25 08:07:52

问题


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

Error:

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

回答1:


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

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;
}

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



来源:https://stackoverflow.com/questions/59347349/access-elements-of-multidimentional-cvmat-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!