Gilad
Gilad

Reputation: 6575

rehshape image to 3 columns matrix matlab vs openCV

Here is the matlab code:

accum1r = imresize(accum1, n2_points, 'bilinear','AntiAliasing',false);    
time_mean_unsorted = reshape(accum1r, [], 3) ./ n_frames;

here is my openCV code

    cv::Mat accum1r(x_n2_points, y_n2_points,CV_64FC3);
    cv::resize(_accum1,accum1r,accum1r.size(),0.0,0.0,cv::INTER_LINEAR);

    int newRows = accum1r.rows * accum1r.cols;
    int dims[2] = {newRows,3};    
    cv::Mat planes[] = {cv::Mat::zeros(newRows,1, CV_32F), cv::Mat::zeros(newRows,1, CV_32F), cv::Mat::zeros(newRows,1, CV_32F)};
    cv::split(accum1r,planes);
    planes[0] = planes[0].t();    
    planes[0] = planes[0].reshape(1,2,dims);
    planes[1] = planes[1].t();    
    planes[1] = planes[1].reshape(1,2,dims);
    planes[2] = planes[2].t();    
    planes[2] = planes[2].reshape(1,2,dims);    
    cv::merge(planes,3,accum1r);
    cv::Mat timeMeanUnsorted = accum1r / (double)numberOfFrames;

this is the only way I was able to get the same accurate results. I can't get reshape of openCV to do the same functionality as matlab.

when I use reshape matlab does it columns first and openCV does it rows first so I need to split my 3D matrix into planes -> transpose them -> reshape them -> join them... this is a little complex.. am I missing something here? can this be done in a simpler way?

Attached input data is 1920x1088x3 matrix 2 files: accum1,accum2 :http://www.filetolink.com/b2a20a1f73

n2_point = [137, 77]
n_nframes = 3

Upvotes: 0

Views: 458

Answers (1)

Miki
Miki

Reputation: 41765

This code:

int newRows = accum1r.rows * accum1r.cols;
int dims[2] = {newRows,3};    
cv::Mat planes[] = {cv::Mat::zeros(newRows,1, CV_32F), cv::Mat::zeros(newRows,1, CV_32F), cv::Mat::zeros(newRows,1, CV_32F)};
cv::split(accum1r,planes);
planes[0] = planes[0].t();    
planes[0] = planes[0].reshape(1,2,dims);
planes[1] = planes[1].t();    
planes[1] = planes[1].reshape(1,2,dims);
planes[2] = planes[2].t();    
planes[2] = planes[2].reshape(1,2,dims);    
cv::merge(planes,3,accum1r);

can be rewritten as:

accum1r = accum1r.reshape(3, 1).t();

You can then get a newRows x 3 x 1 matrix timeMeanUnsorted as in Matlab using:

cv::Mat timeMeanUnsorted = accum1r.reshape(1) / numberOfFrames;

If you want instead a newRows x 1 x 3 (3 channel) matrix you can simply:

cv::Mat timeMeanUnsorted = accum1r / numberOfFrames;

Upvotes: 1

Related Questions