微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

图像拼接战争透视尺寸问题

如何解决图像拼接战争透视尺寸问题

我正在尝试拼接两张图片。技术栈是 vs 2017 上的 opecv c++。

我考虑过的图像是:

代码图像1:

代码图像 2:

我使用此代码找到了单应矩阵。我已经考虑了上面给出的 image1 和 image2。

    int minHessian = 400;
    Ptr<SURF> detector = SURF::create(minHessian);
    vector< KeyPoint > keypoints_object,keypoints_scene;
    detector->detect(gray_image1,keypoints_object);
    detector->detect(gray_image2,keypoints_scene);

    
    Mat img_keypoints;
    drawKeypoints(gray_image1,keypoints_object,img_keypoints);
    imshow("SURF Keypoints",img_keypoints);

    Mat img_keypoints1;
    drawKeypoints(gray_image2,keypoints_scene,img_keypoints1);
    imshow("SURF Keypoints1",img_keypoints1);
    //-- Step 2: Calculate descriptors (feature vectors)
    Mat descriptors_object,descriptors_scene;
    detector->compute(gray_image1,descriptors_object);
    detector->compute(gray_image2,descriptors_scene);

    //-- Step 3: Matching descriptor vectors using FLANN matcher

    Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create(DescriptorMatcher::FLANNBASED);
    vector< DMatch > matches;
    matcher->match(descriptors_object,descriptors_scene,matches);


    double max_dist = 0; double min_dist = 100;

    //-- Quick calculation of max and min distances between keypoints 
    for (int i = 0; i < descriptors_object.rows; i++)
    {
        double dist = matches[i].distance;
        if (dist < min_dist) min_dist = dist;
        if (dist > max_dist) max_dist = dist;
    }

    printf("-- Max dist: %f \n",max_dist);
    printf("-- Min dist: %f \n",min_dist);


    //-- Use only "good" matches (i.e. whose distance is less than 3*min_dist )
    vector< DMatch > good_matches;
    Mat result,H;
    for (int i = 0; i < descriptors_object.rows; i++)
    {
        if (matches[i].distance < 3 * min_dist)
        {
            good_matches.push_back(matches[i]);
        }
    }
    Mat img_matches;
    drawMatches(gray_image1,gray_image2,good_matches,img_matches,Scalar::all(-1),vector<char>(),DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
    imshow("Good Matches",img_matches);
    std::vector< Point2f > obj;
    std::vector< Point2f > scene;
    cout << "Good Matches detected" << good_matches.size() << endl;
    for (int i = 0; i < good_matches.size(); i++)
    {
        //-- Get the keypoints from the good matches
        obj.push_back(keypoints_object[good_matches[i].queryIdx].pt);
        scene.push_back(keypoints_scene[good_matches[i].trainIdx].pt);
    }


    // Find the Homography Matrix for img 1 and img2
    H = findHomography(obj,scene,RANSAC);

下一步是扭曲这些。我使用透视变换函数在拼接图像上找到 image1 的角。我认为这是在 Mat result 中使用的列数。这是我写的代码 ->

    vector<Point2f> imageCorners(4);
    imageCorners[0] = Point(0,0);
    imageCorners[1] = Point(image1.cols,0);
    imageCorners[2] = Point(image1.cols,image1.rows);
    imageCorners[3] = Point(0,image1.rows);
    vector<Point2f> projectedCorners(4);
    perspectiveTransform(imageCorners,projectedCorners,H);
    Mat result;
    warpPerspective(image1,result,H,Size(projectedCorners[2].x,image1.rows));
    Mat half(result,Rect(0,image2.cols,image2.rows));
    image2.copyTo(half);
    imshow("result",result);

我得到了这些图像的拼接输出。但问题在于图像的大小。我通过手动将两个原始图像与上述代码的结果进行比较来进行比较。代码结果的大小更大。我应该怎么做才能使其尺寸完美?理想的尺寸应该是 image1.cols + image2.cols - overlapping length

解决方法

warpPerspective(image1,result,H,Size(projectedCorners[2].x,image1.rows));

这条线似乎有问题。 您应该选择大小的极值点。

Rect rec = boundingRect(projectedCorners);
warpPerspective(image1,rec.size());

但是如果 rec.tl() 落在负轴上,您将丢失部分,因此您应该将单应矩阵移动到第一象限。 请参阅我对 Fast and Robust Image Stitching Algorithm for many images in Python 的回答的透视变形部分。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。