当前位置:网站首页>[OpenCV] Book view correction + advertising screen switching Perspective transformation image processing
[OpenCV] Book view correction + advertising screen switching Perspective transformation image processing
2022-08-03 13:15:00 【I am sixteen years old】
目录
一:Practical application scenarios of perspective transformation
二:Case to learn about perspective transformation--Book view correction
三:Case to learn about perspective transformation--Advertising screen switch
一:Practical application scenarios of perspective transformation
When we were out in the car,It is common to see drivers using a driving aid,This uses the related operations of perspective transformation,Assist the driver to drive safely.
可以看出,The normal display of the road conditions around the own car,It facilitates the driving operation of the driver
A real life application of perspective transformation like this,其实还有许多,接下来,我们就来学习一下OpenCV的透视变换
二:Case to learn about perspective transformation--Book view correction
原图:
目标:A perspective transformation is required Put the textbook on the desk View from a positive perspective
结果展示:
The complete code for book view correction is as follows:【Here is the mouse click to select clockwise from the upper left corner4point perspective transformation】
#include <iostream>
#include <opencv2/opencv.hpp>
#include<vector>
using namespace cv;
using namespace std;
//鼠标操作 Prepare the structure yourself
struct imagedata
{
Mat img;//目标图像 用于点击 确定坐标
vector<Point2f> points;//Store the coordinates of the original image Save by mouse click
};
//鼠标操作的回调函数:用于选择四个角的点(使用方法:Select four points clockwise starting from the upper left corner,Press Enter after selection)
void mouseHundle(int event,int x,int y,int flag,void *arg)
{
//强制转换
struct imagedata * d = (struct imagedata*)arg;
//If the left mouse button is pressed
if(event==EVENT_LBUTTONDOWN)
{
//Use a circle to mark where the left mouse button is pressed
circle(d->img,Point(x,y),3,Scalar(255,0,0),3,CV_AA);//在图上标记,圆心为点击的位置
imshow("image",d->img);//Marker points are displayed on the original window
//透视变换 The coordinates of the four points need to be used
if(d->points.size()<4)
{
d->points.push_back(Point2f(x,y));//Store the clicked coordinates
}
}
}
void example_1()
{
Mat image=imread("D:/00000000000003jieduanshipincailliao/book2.jpg");
Mat result=Mat::zeros(400,500,CV_8UC1);//最终结果显示 单通道
//准备坐标:存放四个转换以后的坐标
vector<Point2f>obj;
//The coordinates must be reversed mirror coordinates
obj.push_back(Point2f(0,0));
obj.push_back(Point2f(500,0));
obj.push_back(Point2f(500,400));
obj.push_back(Point2f(0,400));
imshow("image",image);
struct imagedata data;
data.img=image;
//鼠标操作 鼠标处理的回调函数
setMouseCallback("image",mouseHundle,&data);
//按任意键关闭当前显示的窗口,显示下一个窗口
waitKey(0);
//利用RANSAC算法 Calculate the transformation mapping matrix3*3
Mat res=findHomography(data.points,obj,CV_RANSAC);
//Check out the transformation matrix
//imshow("res",res);
//The original image is converted into the resulting image
warpPerspective(image,result,res,result.size());
//View the conversion result graph 透视变换
imshow("result",result);
waitKey(0);
}
int main(int argc, char *argv[])
{
example_1();
return 0;
}
The above is to use the mouse to click to determine the coordinates,当然,We can also write the selected coordinates in the code to perform perspective transformation related operations. Still select the upper left corner to start clockwise coordinate selection,Take the upper left corner as an example,The other three points can be analogized to the upper left coordinate selection to determine the lower coordinate selection
You can see the coordinates of the upper left corner x=282,y=43
Determine the four coordinates used by the perspective transformation yourself 直接回车 You can view the perspective transformation results
//鼠标操作 鼠标处理的回调函数
//setMouseCallback("image",mouseHundle,&data);
//Do not use mouse clicks,Set the four coordinates of the perspective transformation yourself
vector<Point2f>src;
src.push_back(Point2f(283,134));
src.push_back(Point2f(745,234));
src.push_back(Point2f(578,463));
src.push_back(Point2f(61,299));
//按任意键关闭当前显示的窗口,显示下一个窗口
waitKey(0);
//利用RANSAC算法 Calculate the transformation mapping matrix3*3
Mat res=findHomography(src,obj,CV_RANSAC);
//Check out the transformation matrix
//imshow("res",res);
程序运行 直接回车操作 查看
三:Case to learn about perspective transformation--Advertising screen switch
背景图片:
The ad image you want to toggle:
目标:Select Ads screen 切换 Advertising screen content
结果:on the advertising screen 内容推荐 Successfully switched to the ad image you want to switch
实现方法:The superposition of the two images after processing,It is to fill the black background image on the advertising screen that you want to replace first,Then add the ad image you want The superposition of two pictures can achieve the effect of switching the advertising screen
The complete code of ad screen switching is as follows:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
struct imagedata
{
Mat img;//目标图像
vector <Point2f> points;//3D点
};
//鼠标操作函数:用于选择四个角的点(使用方法有顺序的,从左上角顺时针选择,选完之后回车)
void mouseHundle(int event,int x,int y,int flag,void *ptr)
{
struct imagedata * d=(struct imagedata*)ptr;
if(event==EVENT_LBUTTONDOWN)
{
//确定按下的是鼠标左键
//用圆形标记一下鼠标按下左键标记的位置
circle(d->img,Point(x,y),3,Scalar(255,0,0),3,CV_AA);//在图上标记,圆心为点击的位置
imshow("dst",d->img);//在dstThe image selected by the mouse is displayed in the background image
if(d->points.size()<4)//只存下来,最先点的前四个点
{
d->points.push_back(Point2f(x,y));//把鼠标操作点击的点存起来
}
}
}
void example_2()
{
Mat image1=imread("D:/00000000000003jieduanshipincailliao/0802.jpg");//The ad content you want to switch to
Mat image2=imread("D:/00000000000003jieduanshipincailliao/city.jpg");//New York Times Square picture background
Mat dst=image2.clone();//背景图片 克隆
//Store the original image coordinates The picture of the advertising screen that you want to switch
vector <Point2f>obj;
obj.push_back(Point2f(0,0));
obj.push_back(Point2f(image1.cols,0));
obj.push_back(Point2f(image1.cols,image1.rows));
obj.push_back(Point2f(0,image1.rows));
//New York Times Square map background map display
imshow("dst",dst);
struct imagedata data;
data.img =dst;
//鼠标操作 鼠标处理的回调函数
setMouseCallback("dst",mouseHundle,&data);
//按任意键关闭当前显示的窗口,显示下一个窗口
waitKey(0);
//Calculate the transformation mapping matrix 3*3 The original image coordinates are converted to mouse selection
Mat res = findHomography(obj,data.points,CV_RANSAC);
//imshow("res",res);
//透视变换
warpPerspective(image1,dst,res,dst.size());
//The only image that has been changed is the advertisement image
//imshow("image1",dst);
Point pts[4];
for(int i=0;i<4;i++)
{
pts[i]=data.points[i];
}
//The area of the four coordinates clicked by the mouse is filled with black
fillConvexPoly(image2,pts,4,Scalar(0),CV_AA);
//imshow("image2",image2);
//Replace the only image of the advertisement image 和 The area of the four coordinates selected by the mouse is filled with a black background image 叠加
image2+=dst;
//Display the final advertisement screen change result picture
imshow("final",image2);
waitKey(0);
}
int main(int argc, char *argv[])
{
example_2();
return 0;
}
边栏推荐
猜你喜欢
An introduction to the skeleton tool
【实战技能】单片机bootloader的CANFD,I2C,SPI和串口方式更新APP视频教程(2022-08-01)
How to build an overseas purchasing system/purchasing website - source code analysis
基于php校园医院门诊管理系统获取(php毕业设计)
层次分析法
图像融合DDcGAN学习笔记
An工具介绍之3D工具
便携烙铁开源系统IronOS,支持多款便携DC, QC, PD供电烙铁,支持所有智能烙铁标准功能
Jmeter使用
An introduction to the camera
随机推荐
Graphic animation and button animation of an animation basic component
自律成就自己
Image fusion GAN-FM study notes
Jmeter使用
免费的网络传真平台_发传真不显示发送号码
An animation based button animation combined with basic code
滑动窗口的最大值
浅谈低代码平台远程组件加载方案
Image fusion SDDGAN article learning
类和对象(中上)
JS获得浏览器类型
一些测试相关知识
OpenCV 透视变换
利用pgsql插件PostGIS 实现地理坐标系数据转换
[数据仓库]分层概念,ODS,DM,DWD,DWS,DIM的概念「建议收藏」
【深度学习】高效轻量级语义分割综述
【OpenCV】 书本视图矫正 + 广告屏幕切换 透视变换图像处理
Notepad++ install jsonview plugin
An introduction to the width tool, deformation tool and lasso tool
An动画基础之元件的影片剪辑效果