代码之家  ›  专栏  ›  技术社区  ›  IVlad

OpenCV中的图像变换与点变换不匹配

  •  2
  • IVlad  · 技术社区  · 6 年前

    我正在使用 ThinPlateSplineShapeTransformer 来自OpenCV3.4.2条在C++中。另外,使用相同的对象,我想从目标图像的初始图像中扭曲3个点。要可视化转换,我使用3个三角形:

    1. 绿色:在原图上画的三角形。这个会是
    2. 蓝色:参考三角形(初始坐标)

    original image

    原始图像用三角形增强以供参考: original image

    distorted image

    代码:

    void transform()
    {
        Mat img = imread("test.jpg"); // the posted original image
        auto tps = cv::createThinPlateSplineShapeTransformer();
        std::vector<cv::Point2f> sourcePoints, targetPoints, myPoints;
        sourcePoints.push_back(cv::Point2f(0, 0));
        targetPoints.push_back(cv::Point2f(100, 0));
        sourcePoints.push_back(cv::Point2f(650, 40));
        targetPoints.push_back(cv::Point2f(500, 0));
        sourcePoints.push_back(cv::Point2f(0, 599));
        targetPoints.push_back(cv::Point2f(0, 450));
        sourcePoints.push_back(cv::Point2f(799, 599));
        targetPoints.push_back(cv::Point2f(600, 599));
    
        std::vector<cv::DMatch> matches;
        for (unsigned int i = 0; i < sourcePoints.size(); i++)
            matches.push_back(cv::DMatch(i, i, 0));
    
        tps->estimateTransformation(sourcePoints, targetPoints, matches);
        std::vector<cv::Point2f> transPoints, transPoints2;
    
        //======== draw test points
        myPoints.push_back(Point2f(100, 100));
        myPoints.push_back(Point2f(200, 200));
        myPoints.push_back(Point2f(100, 400));
        line(img, myPoints[0], myPoints[1], Scalar(0, 255, 0), 3);
        line(img, myPoints[1], myPoints[2], Scalar(0, 255, 0), 3);
        line(img, myPoints[2], myPoints[0], Scalar(0, 255, 0), 3);
    
        //========= warp image
        Mat img2 = img.clone();
        tps->warpImage(img, img2);
    
        //========= warp points
        tps->applyTransformation(myPoints, transPoints);
        //tps->applyTransformation(transPoints2, transPoints);
        line(img2, transPoints[0], transPoints[1], Scalar(0, 0, 255), 3);
        line(img2, transPoints[1], transPoints[2], Scalar(0, 0, 255), 3);
        line(img2, transPoints[2], transPoints[0], Scalar(0, 0, 255), 3);
    
        //========== draw reference points
        line(img2, myPoints[0], myPoints[1], Scalar(255, 0, 0), 3);
        line(img2, myPoints[1], myPoints[2], Scalar(255, 0, 0), 3);
        line(img2, myPoints[2], myPoints[0], Scalar(255, 0, 0), 3);
    
        imshow("img", img);
        imshow("img2", img2);
    
        get_test_contur();
        waitKey(0);
    }
    

    0 回复  |  直到 6 年前