当前位置:网站首页>[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;
}
边栏推荐
- Notepad++ 安装jsonview插件
- 类和对象(中下)
- JS get browser type
- leetcode 11. The container that holds the most water
- 安全自定义 Web 应用程序登录
- An introduction to basic tools for selecting line tools (package church)
- Golang structs & methods
- 为冲销量下探中低端市场,蔚来新品牌产品定价低至10万?
- YOLOv5 training data prompts No labels found, with_suffix is used, WARNING: Ignoring corrupted image and/or label appears during yolov5 training
- An introduction to the camera
猜你喜欢

An动画基础之元件的影片剪辑效果

Yahoo! Answers-数据集

How does Filebeat maintain file state?

An introduction to the width tool, deformation tool and lasso tool

An introduction to 3D tools

Sogou news-数据集

基于php校园医院门诊管理系统获取(php毕业设计)

An animation optimization of shape tween and optimization of traditional tweening

An动画优化之传统引导层动画

The common problems in the futures account summary
随机推荐
An animation basic element movie clip effect
PolarFormer: Multi-camera 3D Object Detection with Polar Transformers 论文笔记
An工具介绍之宽度工具、变形工具与套索工具
Notepad++ install jsonview plugin
shell编程之条件语句
Blog records life
In order to counteract the drop in sales and explore the low-end market, Weilai's new brand products are priced as low as 100,000?
Secure Custom Web Application Login
安全自定义 Web 应用程序登录
Byte's favorite puzzle questions, how many do you know?
漫画:怎么证明sleep不释放锁,而wait释放锁?
基于php家具销售管理系统获取(php毕业设计)
Classes and objects (upper)
Database basics one (MySQL) [easy to understand]
【OpenCV】 级联分类器训练模型
PyTorch构建分类网络模型(Mnist数据集,全连接神经网络)
Notepad++ 安装jsonview插件
Image fusion GAN-FM study notes
VLAN 实验
Comics: how do you prove that sleep does not release the lock, and wait to release lock?