13

我有两张图片(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);

干杯,

4

2 回答 2

10

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

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)
于 2016-01-13T18:59:48.787 回答
6

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

于 2012-11-26T17:55:49.647 回答