当前位置:网站首页>3h精通OpenCV(五)-透视变换
3h精通OpenCV(五)-透视变换
2022-06-29 17:46:00 【华为云】
| 0、准备工作 |
|---|
右击新建的项目,选择Python File,新建一个Python文件,然后在开头import cv2导入cv2库,import numpy并且重命名为np。
import cv2import numpy as np
我们还要知道在OpenCV中,坐标轴的方向是x轴向右,y轴向下,坐标原点在左上角,比如下面这张长为640像素,宽为480像素的图片。OK,下面开始本节的学习吧。
| 1、场景描述 |
|---|
给定任意一张图像,从中分割出你需要的区域,并且以指定的大小显示出来。而且分割出的图像在线性空间中还是与显示的窗口是平行的。就比如:我想要获取下面的K,原图中K是斜躺着的,分割后显示出来应该是竖直站立的。那么该如何实现这样的效果呢?
在实现上述效果前,我们需要获取些重要信息,比如说上图中扑克牌K的四个坐标点。我们可以使用画图工具打开这张图片,然后将鼠标放置在分割区域的四个点上,即可获取到坐标信息。
因为画图中图像的宽高坐标轴和OpenCV是一样的,当我将鼠标放到K左上角时,画图的左下角就显示出来(734像素,185像素),这就是左上角这个点的坐标。
| 2、透视变换 |
|---|
上面的要求实现看起来比较困难,但是用cv2库,实现起来其实只有9行代码。
- 首先我们设置下输出图片的宽width为250,高height为350。
- 然后读取我们的图片,就是上面这张图。
- 定义目标区域的四个点坐标,分别为:左上、右上、左下、右下。我们在上一步已经获取到了。
- pst2是输出图片在图片框中的位置,选择平铺即可,固定写法。
- 使用库函数
warpPerspective实现透视变换,感兴趣的可以深入了解下原理。 - 最后输出并且显示两张图像。
width,height=250,350img=cv2.imread("Resources/poker.jpg")pst1=np.float32([[734,185],[1101,266],[621,679],[1030,789]])pst2=np.float32([[0,0],[width,0],[0,height],[width,height]])matrix=cv2.getPerspectiveTransform(pst1,pst2)imgOutput=cv2.warpPerspective(img,matrix,(width,height))cv2.imshow("Image",img)cv2.imshow("Output",imgOutput)cv2.waitKey(0)我们来看下效果,左图是我们透视变换的图像,右图是原图像:
第五节的内容,其实很有趣,在生活中有很多应用。眼过千遍不如手过一遍,快去动手敲一遍吧~
边栏推荐
- 上班可以做副业
- 两种Controller层接口鉴权方式
- [webdriver] upload files using AutoIT
- 人脸识别4-百度商用方案调研
- Visio annotation, annotation location
- Multi mode concurrent implementation of tortoise and rabbit race in go language
- Force deduction daily question 06.29 add two numbers
- Redux源码分析之createStore
- 面试中问最常问的海量数据处理你拿捏了没?
- R语言ggplot2可视化:使用patchwork包(直接使用加号+)将两个ggplot2可视化结果横向组合、接着再和第三个图像横向组合起来(三幅图各占比例为50%、25%、25%)
猜你喜欢

Opencv+yolo-v3 for target tracking

mysql在linux中2003错误如何解决

关于日期相加减问题

小程序容器是什么技术?能助力物联网企业红海突围?

Openfeign use step polling strategy and weight log4j configuration of openfeign interceptor

Issue 42: is it necessary for MySQL to have multiple column partitions

On adding and subtracting dates

分布式 | 几步快速拥有读写分离

力扣每日一题 06.29 两数相加
![[webdriver] upload files using AutoIT](/img/69/8c27626d515976b47f1df4831d09c8.png)
[webdriver] upload files using AutoIT
随机推荐
MaxCompute Studio
Opencv+yolo-v3 for target tracking
双亲委派机制
力扣每日一题 06.29 两数相加
Uploading files using AutoIT
Redux源码分析之createStore
【Try to Hack】Cookie和Session
Open source warehouse contribution - submit pr
力扣今日题-535. TinyURL 的加密与解密
mysql查询视图命令是哪个
What is a SCM system? What are the advantages of a supply chain management system?
Teach you how to install the latest version of mysql8.0 database on windows, nanny level teaching
Bags of Binary Words for Fast Place Recognition in Image Sequenc
How to create and delete MySQL triggers
Inherit Chinese virtues, pay attention to the health of the middle-aged and the elderly, and Yurun milk powder has strong respect for the elderly
MaxCompute字符串替换函数-replace
[webdriver] upload files using AutoIT
Graduation season | Huawei experts teach interview tips: how to get a high salary offer from a large factory?
The aggregate function in the epidisplay package of R language divides numerical variables into different subsets based on factor variables, and calculates the summary statistics and aggregate data. W
Sword finger offer 13 Robot range of motion (BFS)