# Passing multi dimensional array to opencv Mat

What is the correct way to pass a multi dimensional array into an opencv 3d mat vs a 3 channel matrix ? Is the following code below correct ?

int size[3] = {100, 100, 100};
cv::Mat a = cv::Mat(3, size, CV_64FC1, arr->data);
cv::Mat b = cv::Mat(cv::Size(image.cols, image.rows), CV_64FC(100), arr->data);

edit retag close merge delete

It is not possible like this : data order are not the same. Opencv likes 2d data but not 3d data

( 2017-04-18 01:59:47 -0500 )edit

thanks. i have also seen the way multi channel images are stored in http://docs.opencv.org/2.4/doc/tutori... also. Is there a simple solution so I dont have to write my own function to iterate through the 3D matrix ? I guess ill try writing my own function first to verify that it is working.

( 2017-04-18 02:56:04 -0500 )edit

If it is like your matlab post (4 plans must go into 4channels) you can simply divide your image in 4 mat and then use merge to make a 4 channel image

( 2017-04-18 03:04:40 -0500 )edit

Sort by ยป oldest newest most voted

I eventually decided to get my multichannel matrix by looping through the entire array. Below, m_textures is the 1D array. Not sure if this will be good enough in terms of speed.

//fill up m_textures array whose dimension is [512,512,100]

//convert back to opencv format
cv::Mat cv_textures = cv::Mat(cv::Size(image.cols, image.rows), CV_64FC(100));
for (int row = 0; row < 512; row++)
for (int col = 0; col < 512; col++)
for (int channel = 0; channel < 100; channel++)
{
cv_textures.at<cv::Vec<double, 100>>(row, col)[channel]
= m_textures->data[col + row * 512 + channel * 512 * 512];
}

more

2

try like this :

cv::Mat cv_textures ;
vector<Mat> plan(100);
for (int i=0;i<plan.size();i++)
{
plan[i]=Mat(512,512,CV_64FC,m_textures->data+i*512*512*sizeof(double))
}
merge(plan,cv_textures);

( 2017-04-18 06:58:06 -0500 )edit