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")
相关推荐
小鸡吃米…5 分钟前
TensorFlow——Keras 框架
人工智能·python·tensorflow·keras
Never_Satisfied5 分钟前
在c#中,Jint的AsString()和ToString()的区别
服务器·开发语言·c#
Never_Satisfied5 分钟前
在c#中,获取文件的大小
java·开发语言·c#
懒惰的bit12 分钟前
Python入门学习记录
python·学习
Never_Satisfied14 分钟前
在JavaScript / HTML中,触发某个对象的click事件
开发语言·javascript·html
lly20240615 分钟前
ionic 下拉刷新:实现与优化指南
开发语言
米羊12115 分钟前
Spring 框架漏洞
开发语言·python
键盘鼓手苏苏16 分钟前
Flutter for OpenHarmony:cider 自动化版本管理与变更日志生成器(发布流程标准化的瑞士军刀) 深度解析与鸿蒙适配指南
运维·开发语言·flutter·华为·rust·自动化·harmonyos
二十雨辰19 分钟前
[python]-闭包和装饰器
python
IT 行者19 分钟前
ZeroClaw:Rust 驱动的下一代 AI Agent 基础设施
开发语言·人工智能·rust