【问题标题】:rehshape image to 3 columns matrix matlab vs openCVrehshape 图像到 3 列矩阵 matlab vs openCV
【发布时间】:2015-12-22 01:32:51
【问题描述】:

这是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

【问题讨论】:

  • MATLAB 是列优先语言,C++ 是行优先语言。
  • @IKavanagh 我知道,这就是我解决问题的原因...
  • 什么是“准确的结果”?您没有提供样本输入或输出。据我所知,您根本不需要重塑 OpenCV Mat。
  • 为什么我不需要重塑? matlab 代码将矩阵重塑为 3 列矩阵。我可以提供输入和输出我不明白这与我的问题有什么关系?关于如何在c++中创建相同的matlab代码?
  • 我们如何确定我们的代码是否得到相同的结果?至于创建相同的代码,您是尝试重现从 Matlab 获得的结果,还是尝试逐行重现 Matlab 代码,即使它使获得相同的结果变得更加复杂?

标签: c++ matlab opencv


【解决方案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;

【讨论】:

    猜你喜欢
    • 2015-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    • 2014-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多