Mat类的成员函数 size()
当我们用size()获取一个矩阵的行数和列数时,要注意这个函数返回的结果行和列是相反的。
如
Mat ab(30,15,Cv_32fC1);
ab.rows;//返回的结果将是(15,30);
opencv如何将两个矩阵按行或者按列合并
在Matlab中将两个矩阵合并非常方便,按行合并,如A=[B C]
。按列合并如A=[B ;C]
以前用自己写的函数,今天逛外国人论坛发现了vconcat和hconcat函数,用于矩阵的合并与图像的拼接。
vconcat(B,C,A); // 等同于A=[B ;C]
hconcat(B,C,A); // 等同于A=[B C]
以下方法是2016 05 16日前的方法,自己写的函数,现在找到了opencv的函数vconcat和hconcat,可以放弃下面的方法了。
在opencv中貌似并不存在这样的函数,因此,写了两个函数,分别用于合并矩阵。
Mat comMatR(Mat Matrix1,Mat Matrix2,Mat &);//函数声明
Mat comMatC(Mat Matrix1,Mat Matrix2,Mat &);//函数声明
//comMatR(conbine matrix as row):combine Matrix1 and Matrix2 to MatrixCom as row ,just as the matlab expression :MatrixCom=[Matrix1 Matrix1]
Mat comMatR(Mat Matrix1,Mat Matrix2,Mat &MatrixCom)
{
CV_Assert(Matrix1.rows==Matrix2.rows);//行数不相等,出现错误中断
MatrixCom.create(Matrix1.rows,Matrix1.cols+Matrix2.cols,Matrix1.type());
Mat temp=MatrixCom.colRange(0,Matrix1.cols);
Matrix1.copyTo(temp);
Mat temp1=MatrixCom.colRange(Matrix1.cols,Matrix1.cols+Matrix2.cols);
Matrix2.copyTo(temp1);
return MatrixCom;
}
//comMatR(conbine matrix as col):combine Matrix1 and Matrix2 to MatrixCom as rows ,just as the matlab expression :MatrixCom=[Matrix1;Matrix1]
Mat comMatC(Mat Matrix1,Mat Matrix2,Mat &MatrixCom)
{
CV_Assert(Matrix1.cols==Matrix2.cols);//列数不相等,出现错误中断
MatrixCom.create(Matrix1.rows+Matrix2.rows,Matrix1.cols,Matrix1.type());
Mat temp=MatrixCom.rowRange(0,Matrix1.rows);
Matrix1.copyTo(temp);
Mat temp1=MatrixCom.rowRange(Matrix1.rows,Matrix1.rows+Matrix2.rows);
Matrix2.copyTo(temp1);
return MatrixCom;
}