opencv中矩阵按行或者按列合并

11 篇文章 0 订阅

原文:http://blog.csdn.net/mikedadong/article/details/51305640

           http://www.cnblogs.com/cgjdemo/p/4173866.html

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;
    }



接下来我给出OpenCV的两个矩阵的合并代码2

Mat mergeRows(Mat A, Mat B)
 2 {
 3     CV_ASSERT(A.cols == B.cols&&A.type() == B.type());
 4     int totalRows = A.rows + B.rows;
 5 
 6     Mat mergedDescriptors(totalRows, A.cols, A.type());
 7     Mat submat = mergedDescriptors.rowRange(0, A.rows);
 8     A.copyTo(submat);
 9     submat = mergedDescriptors.rowRange(A.rows, totalRows);
10     B.copyTo(submat);
11     return mergedDescriptors;
12 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值