Opencv中Homography
转自 https://blog.csdn.net/datase/article/details/78420251
- 什么是Homography
在图1中有两张书的平面图,两张图分别有四个相对位置相同的点,Homography就是一个变换(3*3矩阵),将一张图中的点映射到另一张图中对应的点
因为Homography是一个3*3矩阵,所以可以写成
两张图间的H映射关系就可以表示成
Homography应用:图像对齐
上面公式得出的H ,对于图一中的所有点都是正确的,换句话说,可以用H将第一个图中的点映射到第二张图。
如何得到一个Homography
要得到两张图片的H,就必须至少知道4个相同对应位置的点,opencv中可以利用findHomography正确得到
- // 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);
OpenCV C++ Homography的一个简单例子:
- #include "opencv2/opencv.hpp"
- using namespace cv;
- using namespace std;
- int main( int argc, char** argv)
- {
- // Read source image.
- Mat im_src = imread("book2.jpg");
- // Four corners of the book in source image
- vector<Point2f> pts_src;
- pts_src.push_back(Point2f(141, 131));
- pts_src.push_back(Point2f(480, 159));
- pts_src.push_back(Point2f(493, 630));
- pts_src.push_back(Point2f(64, 601));
- // Read destination image.
- Mat im_dst = imread("book1.jpg");
- // Four corners of the book in destination image.
- vector<Point2f> pts_dst;
- pts_dst.push_back(Point2f(318, 256));
- pts_dst.push_back(Point2f(534, 372));
- pts_dst.push_back(Point2f(316, 670));
- pts_dst.push_back(Point2f(73, 473));
- // Calculate Homography
- Mat h = findHomography(pts_src, pts_dst);
- // Output image
- Mat im_out;
- // Warp source image to destination based on homography
- warpPerspective(im_src, im_out, h, im_dst.size());
- // Display images
- imshow("Source Image", im_src);
- imshow("Destination Image", im_dst);
- imshow("Warped Source Image", im_out);
- waitKey(0);
- }
- 41
Homography应用:图像矫正
假设你有一张如下所示的图片
你想点击图中书的四个顶点,然后得到正放的书:
该如何做?
利用Homography可以做到这点。
1.首先获取书本四个顶点的坐标 pts_src
2.然后我们需要知道书本的宽高比,此书的宽高比是3/4,所以可使输出图像的size 为300*400,就可设其四个点的坐标为(0,0),(299,0),(299,399),(0,399)保存在pts_dst中
3.通过pts_src和pts_dst 获取homography
4.对原图应用homography 得到输出- #include <opencv2/opencv.hpp>
- using namespace cv;
- using namespace std;
- struct userdata{
- Mat im;
- vector<Point2f> points;
- };
- void mouseHandler(int event, int x, int y, int flags, void* data_ptr)
- {
- if ( event == EVENT_LBUTTONDOWN )
- {
- userdata *data = ((userdata *) data_ptr);
- circle(data->im, Point(x,y),3,Scalar(0,0,255), 5, CV_AA);
- imshow("Image", data->im);
- if (data->points.size() < 4)
- {
- data->points.push_back(Point2f(x,y));
- }
- }
- }
- void main()
- {
- // Read source image.
- Mat im_src = imread("book1.jpg");
- // Destination image. The aspect ratio of the book is 3/4
- Size size(300,400);
- Mat im_dst = Mat::zeros(size,CV_8UC3);
- // Create a vector of destination points.
- vector<Point2f> pts_dst;
- pts_dst.push_back(Point2f(0,0));
- pts_dst.push_back(Point2f(size.width - 1, 0));
- pts_dst.push_back(Point2f(size.width - 1, size.height -1));
- pts_dst.push_back(Point2f(0, size.height - 1 ));
- // Set data for mouse event
- Mat im_temp = im_src.clone();
- userdata data;
- data.im = im_temp;
- cout << "Click on the four corners of the book -- top left first and" << endl
- << "bottom left last -- and then hit ENTER" << endl;
- // Show image and wait for 4 clicks.
- imshow("Image", im_temp);
- // Set the callback function for any mouse event
- setMouseCallback("Image", mouseHandler, &data);
- waitKey(0);
- // Calculate the homography
- Mat h = findHomography(data.points, pts_dst);
- // Warp source image to destination
- warpPerspective(im_src, im_dst, h, size);
- // Show image
- imshow("Image", im_dst);
- waitKey(0);
- }
Homography应用:虚拟广告牌
在足球或者棒球体育直播中,经常可以看到球场旁边有虚拟广告,并且还会根据地区,国家的不同播放不同的广告,这是如何做到的?
看完此篇博客,你应该就能知道如何实现了。原理跟前一个差不多,这里直接上代码- #include <opencv2/opencv.hpp>
- using namespace cv;
- using namespace std;
- struct userdata{
- Mat im;
- vector<Point2f> points;
- };
- void mouseHandler(int event, int x, int y, int flags, void* data_ptr)
- {
- if ( event == EVENT_LBUTTONDOWN )
- {
- userdata *data = ((userdata *) data_ptr);
- circle(data->im, Point(x,y),3,Scalar(0,255,255), 5, CV_AA);
- imshow("Image", data->im);
- if (data->points.size() < 4)
- {
- data->points.push_back(Point2f(x,y));
- }
- }
- }
- int main( int argc, char** argv)
- {
- // Read in the image.
- Mat im_src = imread("first-image.jpg");
- Size size = im_src.size();
- // Create a vector of points.
- vector<Point2f> pts_src;
- pts_src.push_back(Point2f(0,0));
- pts_src.push_back(Point2f(size.width - 1, 0));
- pts_src.push_back(Point2f(size.width - 1, size.height -1));
- pts_src.push_back(Point2f(0, size.height - 1 ));
- // Destination image
- Mat im_dst = imread("times-square.jpg");
- // Set data for mouse handler
- Mat im_temp = im_dst.clone();
- userdata data;
- data.im = im_temp;
- //show the image
- imshow("Image", im_temp);
- cout << "Click on four corners of a billboard and then press ENTER" << endl;
- //set the callback function for any mouse event
- setMouseCallback("Image", mouseHandler, &data);
- waitKey(0);
- // Calculate Homography between source and destination points
- Mat h = findHomography(pts_src, data.points);
- // Warp source image
- warpPerspective(im_src, im_temp, h, im_temp.size());
- // Extract four points from mouse data
- Point pts_dst[4];
- for( int i = 0; i < 4; i++)
- {
- pts_dst[i] = data.points[i];
- }
- // Black out polygonal area in destination image.
- fillConvexPoly(im_dst, pts_dst, 4, Scalar(0), CV_AA);
- // Add warped source image to destination image.
- im_dst = im_dst + im_temp;
- // Display image.
- imshow("Image", im_dst);
- waitKey(0);
- return 0;
- }
结果:
Opencv中Homography相关推荐
- 详解OpenCV中的Lucas Kanade稀疏光流单应追踪器
详解OpenCV中的Lucas Kanade稀疏光流单应追踪器 1. 效果图 2. 源码 参考 这篇博客将详细介绍OpenCV中的Lucas Kanade稀疏光流单应追踪器. 光流是由物体或相机的运动 ...
- OpenCV中的特征匹配+单应性以查找对象
OpenCV中的特征匹配+单应性以查找对象 1. 效果图 2. 源码 参考 这篇博客将混合calib3d模块中的特征匹配和单应性,在复杂图像中查找已知对象. 1. 效果图 特征匹配 & 单应性 ...
- 在OpenCV中使用单应性进行姿势估计
点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 什么是单应性 单应性是一种平面关系,可将点从一个平面转换为另一个平 ...
- OpenCV中的「透视变换 / 投影变换 / 单应性」—cv.warpPerspective、cv.findHomography
文章目录 引言 透视变换(projective transform) 单应性(Homography) opencv代码 仿射变换相关函数 投影变换相关的函数 鸟瞰图代码示例 小结 引言 图像的几何变换 ...
- [OpenCV实战]39 在OpenCV中使用ArUco标记的增强现实
文章目录 1 什么是ArUco标记? 2 在OpenCV中生成ArUco标记 3 检测Aruco标记 4 增强现实应用 5 总结和代码 5.1 生成aruco标记 5.2 使用aruco增强现实 6 ...
- OpenCV进阶(10)在 OpenCV 中使用 ArUco 标记的增强现实
在这篇文章中,我们将解释什么是 ArUco 标记,以及如何使用 OpenCV 将它们用于简单的增强现实任务. ArUco 标记已经在增强现实.相机姿态估计和相机校准中使用了一段时间.让我们更多地了解它 ...
- OpenCV学习笔记(12)——OpenCV中的轮廓
什么是轮廓 找轮廓.绘制轮廓等 1.什么是轮廓 轮廓可看做将连续的点(连着边界)连在一起的曲线,具有相同的颜色和灰度.轮廓在形态分析和物体的检测和识别中很有用. 为了更加准确,要使用二值化图像.在寻找 ...
- OpenCV中的光流及视频特征点追踪
OpenCV中的光流及视频特征点追踪 1. 效果图 2. 原理 2.1 什么是光流?光流追踪的前提.原理 2.2 光流的应用 2.3 光流的2种方法 3. 源码 3.2 稀疏光流追踪 3.2 优化版稀 ...
- Python,OpenCV中的图像修复——cv2.inpaint()
Python,OpenCV中的图像修复--cv2.inpaint 1. 效果图 2. 原理 3. 源码 参考 image inpainting 图像修改 这篇博客将介绍如何通过OpenCV中图像修复的 ...
最新文章
- Python、Unicode和中文
- Wireshark运算符!=无法正常工作
- mysql数据库入门教程(15):流程控制结构
- 第六章:Java_异常处理
- Flask Sessions会话
- Android IDA 动态调试最完善攻略,跨过各种坑
- 使用Vitamio打造自己的Android万能播放器(6)——在线播放(播放列表)
- java基础学习——14、代码格式
- SK海力士推出首款DDR5 DRAM 适用于大数据、人工智能等领域
- 修改 VC 生成的 EXE 文件的图标
- 开个坑,不定期更新OI段子
- 【codevs3290】华容道
- 如何在FL Studio中对整首歌曲音量进行调整
- win10不用密码登录及不显示“要使用本计算机,用户必须输入用户名和密码”的解决办法
- 【算法】硬币找钱问题(贪心算法)
- 基于JAVAweb唐院寻人表白系统计算机毕业设计源码+数据库+lw文档+系统+部署
- 科目一常考知识点速记技巧汇总(2022全新版)
- python实验六到十二作业(待完善)
- 安装Tomcat服务器
- Transformer 实现 Kaggle 房价预测竞赛
热门文章
- SpringMVC---web.xml的配置
- SpringBoot在Tomcat下面启动,访问路径
- HTML中的link的简单介绍和用法
- 使用dex2jar反编译APK时出现的问题
- unix mysql命令大全_mySql的一些常用命令
- python怎么判断是不是列表_python判断变量是否为int、字符串、列表、元组、字典等方法...
- Conda 为什么越来越慢?
- R语言绘制waffle chart
- 中国农业大学生物学院徐凌组博后招聘-植物微生物组的多样性和功能研究
- MPB:邓晔、王尚等-​环境样本中的细菌总量测定—流式细胞法