首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在OpenCV中使用单形变换图片?

问如何在OpenCV中使用单形变换图片?
EN

Stack Overflow用户
提问于 2012-11-26 17:36:21
回答 2查看 23.6K关注 0票数 13

我有两幅图片(A和B),其中一幅略有扭曲,另一幅有平移、旋转和缩放差异(例如,这些图片:)

​

​

我需要的是在pic B中应用一种变换,这样它就可以补偿存在的失真/平移/旋转,使两幅图片都具有相同的大小、方向和没有平移。

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

代码语言:javascript
复制
//-- 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);

干杯,

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-26 17:55:49

您需要warpPerspective函数。该过程类似于这教程中提供的过程(用于仿射变换和翘曲)。

票数 6
EN

Stack Overflow用户

发布于 2016-01-13 18:59:48

对于这个问题,您不需要进行同形处理。你可以用仿射变换代替。但是,如果您确实希望将“同形”用于其他目的,则可以查看下面的代码。它是从这复制而来的,在同调上有很多详细的文章。

C++示例

代码语言:javascript
复制
// 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示例

代码语言:javascript
复制
'''
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)
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13570140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档