您的位置:首页 > 运维架构

opencv图像映射

2016-05-07 22:31 337 查看
测试环境:

win8 64

vs2008

opencv2.4.2

=================================================

参考http://www.learnopencv.com/homography-examples-using-opencv-python-c/

#include "stdafx.h"
#include "opencv2/opencv.hpp"

#pragma comment(lib,"opencv_core242d.lib")
#pragma comment(lib,"opencv_imgproc242d.lib")
#pragma comment(lib,"opencv_highgui242d.lib")
#pragma comment(lib,"opencv_calib3d242d.lib")

using namespace cv;
using namespace std;

int main( int argc, char** argv)
{
//投影图
Mat im_src = imread("D:\\SDK\\opencv2.4.2\\opencv\\samples\\c\\baboon200_rotated.jpg");
vector<Point2f> pts_src;
pts_src.push_back(Point2f(66 , 58));
pts_src.push_back(Point2f(118 , 40));
pts_src.push_back(Point2f(96 , 174));
pts_src.push_back(Point2f(135 , 165));

//目标图像
Mat im_dst = imread("D:\\SDK\\opencv2.4.2\\opencv\\samples\\c\\baboon.jpg");
vector<Point2f> pts_dst;
pts_dst.push_back(Point2f(178 , 59));
pts_dst.push_back(Point2f(335 , 56));
pts_dst.push_back(Point2f(186 , 371));
pts_dst.push_back(Point2f(297 , 376));

//对比点连线图
Mat im_all = Mat::zeros(im_dst.rows,im_src.cols+im_dst.cols,CV_8UC3);
im_src.copyTo(im_all(Rect(0,0,im_src.cols,im_src.rows)));
im_dst.copyTo(im_all(Rect(im_src.cols,0,im_dst.cols,im_dst.rows)));
for (int i = 0;i<4;i++)
{
line(im_all,pts_src[i],Point2f(pts_dst[i].x+im_src.cols,pts_dst[i].y),Scalar(0,255,0));
}

//寻找映射矩阵
Mat h = findHomography(pts_src, pts_dst);
Mat im_out;

//投影
warpPerspective(im_src, im_out, h, im_dst.size());

imshow("Source Image", im_all);
imshow("Warped Source Image", im_out);

waitKey(0);
}


程序运行效果图:





----------------------------------------------------------------------------------------------------------

opencv函数调用很简单,需要人工选着映射图上和对应图像的四个点。也可以使用opencv提供的sift特征点来匹配查找。

将两张图像看成两个矩阵M1,M2,我们要寻找M2=M1*H中的H,这是两张图像的映射关系。

用findHomography找到H

用warpPerspective完成投影输出结果。

(明天母亲节,祝所有妈妈身体健康!)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: