OpenCV图像的几何变换处理方法详解
作者:一二爱上蜜桃猫
这篇文章主要给大家介绍了关于OpenCV图像的几何变换处理的相关资料,图像的几何变换是指将一幅图像映射到另一幅图像内,有缩放、翻转、仿射变换、透视、重映射等操作,需要的朋友可以参考下
一、图像缩放
1.API
cv2.resize(src, dsize, fx=0,fy=0,interpolation = cv2.INTER_LINEAR)
参数:
①src :输入图像
②dsize:绝对尺寸
③fx,fy:相对尺寸
④interpolation:插值方法
2.代码演示
import cv2 as cv import numpy as np import matplotlib.pyplot as plt img = cv.imread('Genshin.jpeg', -1) cv.imshow('image', img) [rows, cols] = img.shape[:2] res_1 = cv.resize(img, (2*cols, 2*rows), interpolation=cv.INTER_CUBIC) cv.imshow('image', res_1) cv.waitKey() res_2 = cv.resize(img, None, fx=0.5, fy=0.5) cv.imshow('image', res_1) cv.waitKey()
二、图像平移
1.API
cv2.warpAffine(img, M, dsize)
参数:
①img:输入图像
②M:2×3移动矩阵,为np.float32类型
③dsize:输出图像的大小
2.代码演示
import cv2 as cv import numpy as np import matplotlib.pyplot as plt img = cv.imread('Genshin.jpeg', -1) cv.imshow('image', img) [rows, cols] = img.shape[:2] M = np.float32([[1, 0, 100], [0, 1, 50]]) dst = cv.warpAffine(img, M, (cols, rows)) cv.imshow('image', dst) cv.waitKey()
三、图像旋转
1.API
cv2.getRotationMatrix2D(center, angle, scale) cv.warpAffine()
参数:
①center:旋转中心
②angle:旋转角度
③scale:缩放比例
返回值:
M:旋转矩阵
2.代码演示
import cv2 as cv import numpy as np import matplotlib.pyplot as plt img = cv.imread('Genshin.jpeg', -1) cv.imshow('image', img) [rows, cols] = img.shape[:2] M = cv.getRotationMatrix2D((cols/2, rows/2), 120, 1) dst = cv.warpAffine(img, M, (cols, rows)) cv.imshow('image', dst) cv.waitKey()
四、仿射变换
1.API
cv2.getAffineTransform() cv2.warpAffine()
2.代码演示
import cv2 as cv import numpy as np import matplotlib.pyplot as plt img = cv.imread('Genshin.jpeg', -1) cv.imshow('image', img) [rows, cols] = img.shape[:2] pts1 = np.float32([[50, 50], [200, 50], [50, 200]]) pts2 = np.float32([[100, 100], [200, 50], [100, 250]]) M = cv.getAffineTransform(pts1, pts2) dst = cv.warpAffine(img, M, (cols, rows)) cv.imshow('image', dst) cv.waitKey()
五、透射变换
1.API
cv2.getPerspectiveTransform() cv2.warpPerspective()
2.代码演示
import cv2 as cv import numpy as np import matplotlib.pyplot as plt img = cv.imread('Genshin.jpeg', -1) cv.imshow('image', img) img = cv.resize(img, None, fx=0.5, fy=0.5) [rows, cols] = img.shape[:2] pts1 = np.float32([[56, 65], [368, 52], [28, 138], [389, 390]]) pts2 = np.float32([[100, 145], [300, 100], [80, 290], [310, 300]]) T = cv.getPerspectiveTransform(pts1, pts2) dst = cv.warpPerspective(img, T, (cols, rows)) cv.imshow('image', dst) cv.waitKey()
六、图像金字塔
1.API
cv2.pyrUp(img) #对图像进行上采样 cv2.pyrDown(img) #对图像进行下采样
2.代码演示
import cv2 as cv import numpy as np import matplotlib.pyplot as plt img = cv.imread('Genshin.jpeg', -1) cv.imshow('image', img) img = cv.pyrDown(img) img = cv.pyrDown(img) img = cv.pyrDown(img) cv.imshow('image', img) cv.waitKey()
总结
到此这篇关于OpenCV图像的几何变换处理的文章就介绍到这了,更多相关OpenCV图像几何变换内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!