SLAM 学习笔记
单应矩阵
以下题目来自计算机视觉life从零开始一起学习SLAM系列
题目: 利用OpenCV编程实现虚拟广告牌的效果。提供两张图,一张是“计算机视觉life”公众号的logo,另外一张是带广告牌的原图,请用单应矩阵实现将原图中广告牌替换为提供的logo的效果。要求通过鼠标点击来选择要替换的广告牌的四个顶点。
代码框架及图片见: 链接:https://pan.baidu.com/s/1uRsBJWahU7uQ7nOk1R49ow 密码:xn5l
参考答案:
#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");
Mat im_src = imread("cvlife.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");
Mat im_dst = imread("ad.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); // 暂停直到按下enter
// ---------- 开始你的代码 --------------
// 计算四个角点和目标图区域对应角点的 Homegraphy
Mat homography = findHomography(pts_src, data.points);
// 用H对原图做变换
warpPerspective(im_src,im_temp,homography, im_temp.size()); // 注意输出图片的尺寸,一定要和im_dst的尺寸一致。
//imshow("test",im_temp);
// 提取鼠标点击的四个角点
Point pts_dst[4];
for (int i = 0; i < 4; i++)
{
pts_dst[i] = data.points[i];
}
// 把目标中对应区域像素值设置为0
fillConvexPoly(im_dst,pts_dst,4,Scalar(0),CV_AA);
// 把原图叠加到目标图上
im_dst = im_dst + im_temp;
// ---------- 结束你的代码 --------------
// Display image.
imshow("Image", im_dst);
waitKey(0);
return 0;
}
效果:
附:CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(012_practice)
set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CXX_FLAGS "-std=c++11")
# 添加OpenCV
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(virtual-billboard virtual-billboard.cpp)
target_link_libraries(virtual-billboard ${OpenCV_LIBS})
总结:
单应矩阵:单应矩阵描述的就是同一个平面的点在不同图像之间的映射关系。参考
( u 1 v 1 1 ) = M 1 ( x w y w 1 ) , ( u 2 v 2 1 ) = M 2 ( x u y w 1 ) ( u 2 v 2 1 ) = M 2 M 1 − 1 ( u 1 v 1 1 ) = H ( u 1 v 1 1 ) \begin{array}{l} \left(\begin{array}{l} u_{1} \\ v_{1} \\ 1 \end{array}\right)=M_{1}\left(\begin{array}{c} x_{w} \\ y_{w} \\ 1 \end{array}\right),\left(\begin{array}{l} u_{2} \\ v_{2} \\ 1 \end{array}\right)=M_{2}\left(\begin{array}{c} x_{u} \\ y_{w} \\ 1 \end{array}\right) \\ \left(\begin{array}{l} u_{2} \\ v_{2} \\ 1 \end{array}\right)=M_{2} M_{1}^{-1}\left(\begin{array}{c} u_{1} \\ v_{1} \\ 1 \end{array}\right)=H\left(\begin{array}{c} u_{1} \\ v_{1} \\ 1 \end{array}\right) \end{array} ⎝⎛u1v11⎠⎞=M1⎝⎛xwyw1⎠⎞,⎝⎛u2v21⎠⎞=M2⎝⎛xuyw1⎠⎞⎝⎛u2v21⎠⎞=M2M1−1⎝⎛u1v11⎠⎞=H⎝⎛u1v11⎠⎞
H的自由度为8,所以4对点就能求出H。
遇到新函数要勤查OpenCV的文档,感觉OpenCV文档也是做的比较用心,函数解释的相当清楚。这里放几个OpenCV的函数:
- 透视变换函数
根据提供的四对点以及相应的单应矩阵H,进行透视变换。函数文档里面把计算公式都写出来了。
- 鼠标回调函数
参数 :要控制的窗口,onMouse是回调函数,格式如下:
多查文档!