OpenCV应用技术

OpenCV: 单应性矩阵变换相关函数

2020-04-18  本文已影响0人  AI秘籍

step1.获取投影变换矩阵

(1)使用getPerspectiveTransform()函数

// 平面1
 std::vector<cv::Point2f> image1_pts{
    cv::Point2f(483, 432),
    cv::Point2f(960, 432),
    cv::Point2f(27, 960),
    cv::Point2f(1172, 960)
  };

    // 平面2
  std::vector<cv::Point2f> image2_pts{
    cv::Point2f(50, 100),
    cv::Point2f(400, 100),
    cv::Point2f(50, 500),
    cv::Point2f(400, 500)
  };

 cv::Mat image1_to_image2 = cv::getPerspectiveTransform(image1_pts, image2_pts);
 cv::Mat image2_to_image1 = cv::getPerspectiveTransform(image2_pts, image1_pts);
 std::cout<<"image1_to_image2=="<<image1_to_image2<<std::endl;
 std::cout<<"image2_to_image1=="<<image2_to_image1<<std::endl;

(2)使用findHomography()函数

cv::Mat image1_to_image2_ = findHomography(image1_pts,image2_pts);
cv::Mat image2_to_image1_ = findHomography(image2_pts,image1_pts);
std::cout<<"image1_to_image2_=="<<image1_to_image2_<<std::endl;
std::cout<<"image2_to_image1_=="<<image2_to_image1_<<std::endl;
image.png
image.png

可见,这两个矩阵结果近乎相等.

step2.图像变换

warpPerspective()函数

        // 图像变换
    cv::Mat src_im = cv::imread("src.jpg",1);
    cv::Mat dst_im;
    warpPerspective(src_im, dst_im, image1_to_image2, src_im.size());

image.png

step3.图像点变换

采用perspectiveTransform()函数

  std::vector<cv::Point2f> pts_t,result_pts;
  pts_t = image1_pts;
  cv::perspectiveTransform(pts_t,result_pts,image1_to_image2);
  std::cout<<"result pt:="<<result_pts<<std::endl;
上一篇 下一篇

猜你喜欢

热点阅读