【问题标题】:HOW TO use Homography to transform pictures in OpenCV?如何使用 Homography 在 OpenCV 中转换图片?
【发布时间】:2012-11-14 05:48:42
【问题描述】:

我有两张图片(A 和 B),一张与另一张略有失真,它们之间存在平移、旋转和比例差异(例如,这些图片:)


Ssoooooooo 我需要在图片 B 中应用一种转换,以补偿存在的失真/平移/旋转,以使两张图片具有相同的大小、方向且没有平移

我已经提取了点并找到了 Homography,如下所示。但是我不知道如何使用 Homography 来转换 Mat img_B 所以它看起来像 Mat img_A。有什么想法吗?

//-- Localize the object from img_1 in img_2
std::vector<Point2f> obj;
std::vector<Point2f> scene;

for (unsigned 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);
}

Mat H = findHomography(obj, scene, CV_RANSAC);

干杯,

【问题讨论】:

    标签: opencv transformation distortion homography


    【解决方案1】:

    您需要warpPerspective 函数。该过程类似于this 教程中介绍的过程(用于仿射变换和扭曲)

    【讨论】:

      【解决方案2】:

      这个问题不需要单应性。您可以改为计算仿射变换。但是,如果您确实想将单应性用于其他目的,您可以查看下面的代码。它是从this 复制的homography 上的详细文章。

      C++ 示例

      // pts_src and pts_dst are vectors of points in source 
      // and destination images. They are of type vector<Point2f>. 
      // We need at least 4 corresponding points. 
      
      Mat h = findHomography(pts_src, pts_dst);
      
      // The calculated homography can be used to warp 
      // the source image to destination. im_src and im_dst are
      // of type Mat. Size is the size (width,height) of im_dst. 
      
      warpPerspective(im_src, im_dst, h, size);
      

      Python 示例

      '''
      pts_src and pts_dst are numpy arrays of points
      in source and destination images. We need at least 
      4 corresponding points. 
      ''' 
      h, status = cv2.findHomography(pts_src, pts_dst)
      
      ''' 
      The calculated homography can be used to warp 
      the source image to destination. Size is the 
      size (width,height) of im_dst
      '''
      
      im_dst = cv2.warpPerspective(im_src, h, size)
      

      【讨论】:

        猜你喜欢
        • 2020-08-12
        • 2022-01-15
        • 2018-11-04
        • 1970-01-01
        • 2014-07-17
        • 2017-02-28
        • 2023-02-25
        • 2011-05-31
        • 1970-01-01
        相关资源
        最近更新 更多