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")
相关推荐
夕除几秒前
java--2
java·开发语言
ASS-ASH2 分钟前
霸王色霸气的本质概括分析
人工智能·python·机器学习·大脑·脑电波
ValidationExpression8 分钟前
学习:词嵌入(Word Embedding / Text Embedding)技术
python·学习·ai
星辰徐哥19 分钟前
Rust函数与流程控制——构建逻辑清晰的系统级程序
开发语言·后端·rust
liliangcsdn20 分钟前
如何使用lambda对python列表进行排序
开发语言·python
葱明撅腚31 分钟前
seaborn绘图(下)
python·matplotlib·可视化·seaborn·图表绘制
半路_出家ren35 分钟前
3.python模拟勒索病毒
python·网络安全·密码学·网络攻击模型·base64·病毒·勒索病毒
jhf20201 小时前
2026汽车4S店GEO优化高性价比公司选型指南:从效果、成本到适配
python·汽车
叫我:松哥1 小时前
基于scrapy的网易云音乐数据采集与分析设计实现
python·信息可视化·数据分析·beautifulsoup·numpy·pandas
极智-9961 小时前
GitHub 热榜项目-日榜精选(2026-01-24)| AI智能体工具、Python生态等 | remotion、VibeVoice、goose等
人工智能·python·github·ai智能体·大模型部署·语音ai