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;
    }
好的,以下是一个简单的图像拼接算法的 C++ 代码,需要使用 OpenCV 库: ``` #include <iostream> #include <opencv2/opencv.hpp> using namespace cv; using namespace std; int main(int argc, char** argv) { if (argc < 3) { cout << "Usage: " << argv[0] << " <image1> <image2>" << endl; return -1; } Mat img1 = imread(argv[1], IMREAD_COLOR); Mat img2 = imread(argv[2], IMREAD_COLOR); if (img1.empty() || img2.empty()) { cout << "Could not read the image(s)." << endl; return -1; } Mat img1_gray, img2_gray; cvtColor(img1, img1_gray, COLOR_BGR2GRAY); cvtColor(img2, img2_gray, COLOR_BGR2GRAY); vector<KeyPoint> keypoints1, keypoints2; Mat descriptors1, descriptors2; Ptr<ORB> orb = ORB::create(); orb->detectAndCompute(img1_gray, Mat(), keypoints1, descriptors1); orb->detectAndCompute(img2_gray, Mat(), keypoints2, descriptors2); BFMatcher matcher(NORM_HAMMING); vector<DMatch> matches; matcher.match(descriptors1, descriptors2, matches); double max_dist = 0; double min_dist = 100; for (int i = 0; i < descriptors1.rows; i++) { double dist = matches[i].distance; if (dist < min_dist) min_dist = dist; if (dist > max_dist) max_dist = dist; } vector<DMatch> good_matches; for (int i = 0; i < descriptors1.rows; i++) { if (matches[i].distance <= max(2 * min_dist, 0.02)) { good_matches.push_back(matches[i]); } } vector<Point2f> points1, points2; for (size_t i = 0; i < good_matches.size(); i++) { points1.push_back(keypoints1[good_matches[i].queryIdx].pt); points2.push_back(keypoints2[good_matches[i].trainIdx].pt); } Mat H = findHomography(points2, points1, RANSAC); Mat result; warpPerspective(img2, result, H, Size(img1.cols + img2.cols, img1.rows)); Mat half(result, Rect(0, 0, img1.cols, img1.rows)); img1.copyTo(half); namedWindow("Result", WINDOW_NORMAL); imshow("Result", result); waitKey(0); return 0; } ``` 这个算法使用了 ORB 特征检测器和描述符来检测和匹配两张图片中的特征点,然后使用 RANSAC 算法找到这些特征点之间的单应性矩阵,最终将图像拼接在一起。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值