rehshape图像到3列矩阵matlab与openCV

时间:2015-09-24 12:35:19

标签: c++ matlab opencv

这是matlab代码:

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

这是我的openCV代码

    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;

这是我能够获得相同准确结果的唯一方法。我无法重塑openCV来执行与matlab相同的功能。

当我使用reshape时,matlab首先执行列,openCV首先执行行 所以我需要将我的3D矩阵分割成平面 - >转置它们 - >重塑它们 - >加入他们...这有点复杂.. 我在这里错过了什么吗?这可以用更简单的方式完成吗?

附加的输入数据是1920x1088x3矩阵2文件:accum1,accum2 :http://www.filetolink.com/b2a20a1f73

n2_point = [137, 77]
n_nframes = 3

1 个答案:

答案 0 :(得分:1)

此代码:

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

可以改写为:

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

然后,您可以使用以下内容获取newRows x 3 x 1矩阵timeMeanUnsorted,如Matlab:

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

如果您想要一个newRows x 1 x 3(3个频道)矩阵,您可以简单地:

cv::Mat timeMeanUnsorted = accum1r / numberOfFrames;