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

时间:2018-08-17 13:04:59

标签: algorithm opencv image-processing

我正在使用C ++中OpenCV 3.4.2中的ThinPlateSplineShapeTransformer扭曲图像。另外,使用同一对象,我想使目标图像中的初始图像扭曲3个点。为了可视化转换,我使用了三个三角形:

  1. 绿色:在原始图像上绘制的三角形。这将是 图像失真。
  2. 蓝色:参考三角形(初始坐标)
  3. 红色:点变形后的三角形

原始图片: 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 个答案:

没有答案