gpt4 book ai didi

opencv - 如何使用 Homography 在 OpenCV 中转换图片?

转载 作者:太空宇宙 更新时间:2023-11-03 20:39:40 24 4
gpt4 key购买 nike

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

ORIGINAL LENA DISTORTED 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);

干杯,

最佳答案

这个问题不需要单应性。您可以改为计算仿射变换。但是,如果您确实想将单应性用于其他目的,您可以查看下面的代码。它是从 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)

关于opencv - 如何使用 Homography 在 OpenCV 中转换图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13570140/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com