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()

 

相关推荐

  1. Opencv | 图像几何变换

    2024-03-16 17:46:02       26 阅读
  2. 图像处理中几何变换有哪些?

    2024-03-16 17:46:02       17 阅读
  3. OpencV图像几何形状绘制

    2024-03-16 17:46:02       21 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-16 17:46:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-16 17:46:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-16 17:46:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-16 17:46:02       20 阅读

热门阅读

  1. ubuntu图形界面卡住了,通过以下几种方法解决

    2024-03-16 17:46:02       23 阅读
  2. Ubuntu22.04挂载ntfs硬盘

    2024-03-16 17:46:02       21 阅读
  3. EasyExcel导入导出

    2024-03-16 17:46:02       30 阅读
  4. Lua使用三目运算符取值

    2024-03-16 17:46:02       20 阅读
  5. MT1069 圆切平面

    2024-03-16 17:46:02       18 阅读
  6. Redis 哨兵模式

    2024-03-16 17:46:02       19 阅读
  7. 大数据开发(Hive面试真题-卷二)

    2024-03-16 17:46:02       21 阅读
  8. Crossing River

    2024-03-16 17:46:02       20 阅读
  9. 蓝桥杯刷题(八)

    2024-03-16 17:46:02       17 阅读
  10. C++/CLI学习笔记4(快速打通c++与c#相互调用的桥梁)

    2024-03-16 17:46:02       17 阅读