Multi-Dimensional data in a Matrix in OpenCV with C++

前端 未结 2 833
一向
一向 2020-12-05 16:52

I want to declare, populate, access a Multi-Dimensional Matrix in OpenCV (C++) which is compatible with namespace cv. I found no quick and easy to learn exa

2条回答
  •  青春惊慌失措
    2020-12-05 17:03

    To create a multi-dimensional matrix that is of size 100x100x3, using floats, one channel, and with all elements initialized to 10 you write like this:

    int size[3] = { 100, 100, 3 };
    cv::Mat M(3, size, CV_32FC1, cv::Scalar(10));
    

    To loop over and output the elements in the matrix you can do:

    for (int i = 0; i < 100; i++)
      for (int j = 0; j < 100; j++)
        for (int k = 0; k < 3; k++) 
          std::cout << M.at(i,j)[k] << ", ";
    

    However, beware of the troubles with using multi-dimensional matrices as documented here: How do i get the size of a multi-dimensional cv::Mat? (Mat, or MatND)

提交回复
热议问题