OpenCV C ++关键点和描述符转换为扭曲图像的单应性

时间:2017-07-04 21:07:46

标签: c++ opencv transform

我创建了一个拼接程序,可以将任意数量的图像拼接成一个马赛克。该计划的效率需要我努力改进的工作。我的目标是创建已经找到的关键点和描述符的列表,这样程序就不需要重新完成它已经完成的工作,这样如果图像太多,内存就不会被重载。要做到这一点,我需要将关键点和描述符与实际图像本身一起转换。这是我准备的代码(我想我与warpPerspective()perspectiveTransform()混淆了。

std::tuple<Mat, std::vector<KeyPoint>, Mat>  stitchMatches(Mat image1,Mat image2, Mat homography, std::vector<KeyPoint> kp1, std::vector<KeyPoint> kp2 , Mat desc1, Mat desc2){
    Mat result, destination, descriptors_updated;
    vector<Point2f> fourPoint;
    std::vector<KeyPoint> kp1New, kp2New, keypoints_updated;

    //-Get the four corners of the first image (master)
    fourPoint.push_back(Point2f (0,0));
    fourPoint.push_back(Point2f (image1.size().width,0));
    fourPoint.push_back(Point2f (0, image1.size().height));
    fourPoint.push_back(Point2f (image1.size().width, image1.size().height));
    perspectiveTransform(Mat(fourPoint), destination, homography);


    //- Get points used to determine Htr
    double min_x, min_y, tam_x, tam_y;
    float min_x1, min_x2, min_y1, min_y2, max_x1, max_x2, max_y1, max_y2;
    min_x1 = min(fourPoint.at(0).x, fourPoint.at(1).x);
    min_x2 = min(fourPoint.at(2).x, fourPoint.at(3).x);
    min_y1 = min(fourPoint.at(0).y, fourPoint.at(1).y);
    min_y2 = min(fourPoint.at(2).y, fourPoint.at(3).y);
    max_x1 = max(fourPoint.at(0).x, fourPoint.at(1).x);
    max_x2 = max(fourPoint.at(2).x, fourPoint.at(3).x);
    max_y1 = max(fourPoint.at(0).y, fourPoint.at(1).y);
    max_y2 = max(fourPoint.at(2).y, fourPoint.at(3).y);
    min_x = min(min_x1, min_x2);
    min_y = min(min_y1, min_y2);
    tam_x = max(max_x1, max_x2);
    tam_y = max(max_y1, max_y2);

    //- Htr use to map image one to result in line with the alredy warped image 1
    Mat Htr = Mat::eye(3,3,CV_64F);
    if (min_x < 0){
        tam_x = image2.size().width - min_x;
        Htr.at<double>(0,2)= -min_x;
    }
    if (min_y < 0){
        tam_y = image2.size().height - min_y;
        Htr.at<double>(1,2)= -min_y;
    }

    result = Mat(Size(tam_x*2,tam_y*2), CV_8UC3,cv::Scalar(0,0,0));
    warpPerspective(image2, result, Htr, result.size(), INTER_LINEAR, BORDER_TRANSPARENT,   0);
    warpPerspective(image1, result, (Htr*homography), result.size(), INTER_LINEAR, BORDER_TRANSPARENT,0);

    // transform perspective of keypoints and descriptros of kp1 and desc1
    perspectiveTransform(kp2, kp2New, (Htr));
    perspectiveTransform(kp1, kp1New, (Htr*homography));
    perspectiveTransform(desc2, desc2, (Htr));
    perspectiveTransform(desc1, desc1, (Htr*homography));

    //create new vector of keypoints containing the thranformed kp1 and kp2
    keypoints_updated.reserve(kp1New.size() + kp2New.size());
    keypoints_updated.insert(keypoints_updated.end(), kp1New.begin(), kp1New.end());
    keypoints_updated.insert(keypoints_updated.end(), kp2New.begin(), kp2New.end());

    //create a new Mat including the descriports from desc1 and desc2
    descriptors_updated.push_back(desc1);
    descriptors_updated.push_back(desc2);


    //------------TEST TO see if keypoints have moved

    Mat img_keypoints;
    drawKeypoints( result, keypoints_updated, img_keypoints, Scalar::all(-1), DrawMatchesFlags::DEFAULT );

    imshow("Keypoints 1", img_keypoints );
    waitKey();
    destroyAllWindows();



    return {result, keypoints_updated, descriptors_updated};
}

0 个答案:

没有答案