当前位置:网站首页>[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;
}
边栏推荐
- Graphic animation and button animation of an animation basic component
- 为冲销量下探中低端市场,蔚来新品牌产品定价低至10万?
- 安全自定义 Web 应用程序登录
- Secure Custom Web Application Login
- IDEA的模板(Templates)
- JS获得浏览器类型
- Image fusion GAN-FM study notes
- shell编程条件语句
- 技术分享 | 接口自动化测试如何搞定 json 响应断言?
- 论文理解:“Gradient-enhanced physics-informed neural networks for forwardand inverse PDE problems“
猜你喜欢
PyTorch框架训练线性回归模型(CPU与GPU环境)
An introduction to 3D tools
The common problems in the futures account summary
An animation based button animation combined with basic code
leetcode16最接近的三数之和 (排序+ 双指针)
An animation optimization of traditional guide layer animation
类和对象(中上)
基于php校园医院门诊管理系统获取(php毕业设计)
有趣的opencv-记录图片二值化和相似度实现
PyTorch构建分类网络模型(Mnist数据集,全连接神经网络)
随机推荐
ECCV 2022|通往数据高效的Transformer目标检测器
图像融合DDcGAN学习笔记
d作者:d的新特性
Random forest project combat - temperature prediction
YOLOv5 training data prompts No labels found, with_suffix is used, WARNING: Ignoring corrupted image and/or label appears during yolov5 training
Golang 结构体&方法
Golang Mutex
Sogou news - dataset
Using the Work Queue Manager (4)
Nodejs 安装依赖cpnm时,install 出现Error: Cannot find module ‘fs/promises‘
Basic principle of the bulk of the animation and shape the An animation tip point
[Blue Bridge Cup Trial Question 48] Scratch Dance Machine Game Children's Programming Scratch Blue Bridge Cup Trial Question Explanation
期货公司开户关注的关键点
Unsupervised learning KMeans notes and examples
基于php校园医院门诊管理系统获取(php毕业设计)
Golang sync.WaitGroup
2022 年 CISO 最关心的是什么?
使用工作队列管理器(四)
[R] Use grafify for statistical plotting, ANOVA, intervention comparisons, and more!
基于php家具销售管理系统获取(php毕业设计)