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")
相关推荐
ZhengEnCi1 小时前
08c. 检索算法与策略-混合检索
后端·python·算法
明月_清风7 小时前
Python 内存手术刀:sys.getrefcount 与引用计数的生死时速
后端·python
明月_清风7 小时前
Python 消失的内存:为什么 list=[] 是新手最容易踩的“毒苹果”?
后端·python
Flittly1 天前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(3)TodoWrite (待办写入)
python·agent
千寻girling1 天前
一份不可多得的 《 Django 》 零基础入门教程
后端·python·面试
databook1 天前
探索视觉的边界:用 Manim 重现有趣的知觉错觉
python·动效
明月_清风1 天前
Python 性能微观世界:列表推导式 vs for 循环
后端·python
明月_清风1 天前
Python 性能翻身仗:从 O(n) 到 O(1) 的工程实践
后端·python
helloweilei2 天前
python 抽象基类
python
用户8356290780512 天前
Python 实现 PPT 转 HTML
后端·python