0

我正在尝试将 4 个矩阵保存在一个 Matrix 中。但我不确定我们是否可以这样做。所以我将我的 4matrix 转换为 4 个向量。现在我想在矩阵中插入 4 个向量。我的代码是:

       Mat hist_TL = Mat::zeros(20,1, CV_32F);
       Mat hist_TR = Mat::zeros(20,1, CV_32F);
       Mat hist_BL = Mat::zeros(20,1, CV_32F);
       Mat hist_BR = Mat::zeros(20,1, CV_32F);
        for(int i=1;i<=21;i++)
        {
            for(int k=0;k<TL_k_stats.rows;k++)
            {
            if((angl_TL<=bins[i]) && (angl_TL>bins[i-1]))
            {
               hist_TL.at<float>(i-1,0)+=TL_k_stats.at<float>(k,4);
            }
            if((angl_TR<=bins[i]) && (angl_TR>bins[i-1]))
               {
                hist_TR.at<float>(i-1,0)+=TR_k_stats.at<float>(k,4);
               }

            if((angl_BL<=bins[i]) && (angl_BL>bins[i-1]))
               {
                hist_BL.at<float>(i-1,0)+=BL_k_stats.at<float>(k,4);
               }

            if((angl_BR<=bins[i]) && (angl_BR>bins[i-1]))
               {
                hist_BR.at<float>(i-1,0)+=BR_k_stats.at<float>(k,4);
               }
            }
        hist_TL=hist_TL.inv();
        hist_TR=hist_TR.inv();
        hist_BL=hist_BL.inv();
        hist_BR=hist_BR.inv();
        std::vector<float> vhist_TL;
        std::vector<float> vhist_TR;
        std::vector<float> vhist_BL;
        std::vector<float> vhist_BR;

        hist_TL.copyTo(vhist_TL);
        hist_TR.copyTo(vhist_TR);
        hist_BL.copyTo(vhist_BL);
        hist_BR.copyTo(vhist_BR);

所以我想将这 4 个向量复制到一个矩阵。如果有什么方法我可以在不将矩阵转换为向量的情况下做到这一点。请告诉我。在 matlab 中我们可以直接将它存储到一个数组中并像这样返回它

features[] = {hist_TL', hist_TR', hist_BL', hist_BR'};

那么我怎样才能在opencv中实现这一点?

4

2 回答 2

0

您可以像这样直接将 4 矩阵复制到更大的矩阵中:

    Mat TL(rows, cols, type);
    Mat TR(rows, cols, type);
    Mat BL(rows, cols, type);
    Mat BR(rows, cols, type);
    Mat dst(rows * 2, cols *2, type);
    TL.copyTo(dst(Rect(0, 0, cols, rows)));
    TR.copyTo(dst(Rect(cols, 0, cols, rows)));
    BL.copyTo(dst(Rect(0, rows, cols, rows)));
    BR.copyTo(dst(Rect(cols, rows, cols, rows)));

文件:

http://docs.opencv.org/modules/core/doc/basic_structures.html#rect http://docs.opencv.org/modules/core/doc/basic_structures.html?highlight=copyto#mat-copyto

于 2013-08-27T04:15:01.473 回答
0

您可以利用允许您单独为列分配值的“col”方法:

   Mat hist_TL = Mat::zeros(20,1, CV_32F)+1;
   Mat hist_TR = Mat::zeros(20,1, CV_32F)+2;
   Mat hist_BL = Mat::zeros(20,1, CV_32F)+3;
   Mat hist_BR = Mat::zeros(20,1, CV_32F)+4;

   Mat result = Mat::zeros(20,4,CV_32F);

   hist_TL.col(0).copyTo(result.col(0));
   hist_TR.col(0).copyTo(result.col(1));
   hist_BL.col(0).copyTo(result.col(2));
   hist_BR.col(0).copyTo(result.col(3));

更新:

如果您不想替换整个列,而只是替换其中的一部分,则可以使用“范围”方法:

   Mat result = Mat::zeros(20*4,1,CV_32F);

   hist_TL.copyTo(result.rowRange(Range(20*0,20*1)));
   hist_TR.copyTo(result.rowRange(Range(20*1,20*2)));
   hist_BL.copyTo(result.rowRange(Range(20*2,20*3)));
   hist_BR.copyTo(result.rowRange(Range(20*3,20*4)));
于 2013-08-27T03:04:10.970 回答