opencv图像拼接,即把两个矩阵合并为一个(vconcat和hconcat)、Mat成员函数size()

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;
    }
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值