Ask Your Question
0

Passing multi dimensional array to opencv Mat

asked 2017-04-18 01:22:24 -0600

Nbb gravatar image

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 flag offensive close merge delete

Comments

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

LBerger gravatar imageLBerger ( 2017-04-18 01:59:47 -0600 )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.

Nbb gravatar imageNbb ( 2017-04-18 02:56:04 -0600 )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

LBerger gravatar imageLBerger ( 2017-04-18 03:04:40 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-04-18 04:29:41 -0600

Nbb gravatar image

updated 2017-04-18 04:33:33 -0600

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];
    }
edit flag offensive delete link more

Comments

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);
LBerger gravatar imageLBerger ( 2017-04-18 06:58:06 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-04-18 01:22:24 -0600

Seen: 3,799 times

Last updated: Apr 18 '17