我想在Matrix中保存4个矩阵。但是我不确定我们是否可以这样做。所以我将我的4矩阵转换为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个向量复制到一个Matrix。如果有任何方法我可以做到这一点,而无需将矩阵转换为vector.Please让我知道。在matlab中我们可以直接将它存储到数组中并像这样返回
features[] = {hist_TL', hist_TR', hist_BL', hist_BR'};
那么我怎么能在opencv中实现这个呢?
答案 0 :(得分: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)));
答案 1 :(得分: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