Python Opencv实践 - 图像透射变换

复制代码
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt


img = cv.imread("../SampleImages/pomeranian.png", cv.IMREAD_COLOR)
rows,cols = img.shape[:2]
print(rows,cols)

#opencv中的透射变换,需要一个3x3透射变换矩阵
#这个矩阵可以通过cv.getPerspectiveTransform(src,dst)获得
#src是原图中的4个点的坐标,dst是目标图像中的4个点的坐标(任意三个点不共线)
#参考资料:https://blog.csdn.net/qq_50394133/article/details/123832496
srcPts = np.float32([[56,65],[368,52],[28,387],[389,390]])
dstPts = np.float32([[100,145],[300,100],[80,290],[310,300]])
M_perspective = cv.getPerspectiveTransform(srcPts,dstPts)


#图像透射变换
#cv.warpPerspective(src,M,dsize,dst,flags,borderMode,borderValue)
img_perspective = cv.warpPerspective(img,M_perspective,(cols,rows))

#显示图像
fig,axes = plt.subplots(nrows=1, ncols=2, figsize=(12,12), dpi=100)
axes[0].imshow(img[:,:,::-1])
axes[0].set_title("Original")
axes[1].imshow(img_perspective[:,:,::-1])
axes[1].set_title("Perspective Transform")
相关推荐
databook9 分钟前
Pandas 与 Polars:语法、性能和资源占用方面的对比
python·数据分析·pandas
威哥爱编程15 分钟前
Python 智能体开发实战 10:本地跑得好好的,一并发就崩?把它做成一个能上线的服务
python·agent
威哥爱编程20 分钟前
Python 智能体开发实战 09:改了提示词,到底是变好还是变坏?搭一套能打分的离线评测
python·agent
meiying88381 小时前
多快递公司快递单号批量查询接口怎么对接?Python 聚合查询实现
java·大数据·python
Hello_Pyhx1 小时前
Deep-Live-Cam 源码配置思路:环境、模型与执行后端分开排查
python
不积跬步无以至千里mmbb261 小时前
rust_analysis
开发语言·后端·rust
张张123y1 小时前
后端第十六课:Multi-Agent 多智能体(让多个 Agent 分工协作)
大数据·数据库·人工智能·python·deepseek·dsh·agent 框架
全栈练习生2 小时前
大规模智能体仿真
python·ai
原子延迟2 小时前
Smallpdf免费次数用完了(2026),4款工具避坑
图像处理·人工智能·计算机视觉
暴力求解2 小时前
C++ ---继承(二)
开发语言·c++·算法