如何使用Homography转换OpenCV中的图片?

mar*_*oum 12 opencv transformation homography distortion

我有两张图片(A和B)从另一张图片中稍微扭曲了一下,它们之间存在平移,旋转和刻度差异(例如,这些图片:)

原始的LENA LISTORTED LENA


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);
Run Code Online (Sandbox Code Playgroud)

干杯,

Sat*_*ick 9

这个问题你不需要单应性.您可以改为计算仿射变换.但是,如果您确实希望将单应性用于其他目的,可以查看下面的代码.这是从复制在很多详细的文章单应.

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);
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)


Ham*_*mer 6

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