0%

OpenCV(六)-图像变换

一、图像的放大与缩小

1
2
3
4
5
6
7
8
(function) def resize(
src: MatLike,
dsize: Size | None, # 这是你自己定义的大小,如果为None,按照fx fy缩放
dst: MatLike | None = ..., # 输出,python中不需要这个,它直接返回了
fx: float = ...,
fy: float = ...,
interpolation: int = ... # 缩放算法
) -> MatLike

image-20241106145510955

1
2
3
4
5
6
7
8
9
10
import cv2
import numpy as np

dog = cv2.imread('./dog.jpeg')
# resize (x, y)
new = cv2.resize(dog, None, fx=0.3, fy=0.3, interpolation=cv2.INTER_AREA)

cv2.imshow('dog', dog)
cv2.imshow('new', new)
cv2.waitKey(0)

image-20241106145106327

二、图像的翻转

image-20241106145613206

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import cv2
import numpy as np

# 加载图像
dog = cv2.imread('./dog.jpeg')

# 检查图像是否加载成功
if dog is None:
print("错误: 找不到图像文件。")
exit()

# 对图像进行翻转操作
new = cv2.flip(dog, 0) # 垂直翻转
new_2 = cv2.flip(dog, 1) # 水平翻转

# 调整图像大小
dog = cv2.resize(dog, None, fx=0.3, fy=0.3, interpolation=cv2.INTER_AREA)
new = cv2.resize(new, None, fx=0.3, fy=0.3, interpolation=cv2.INTER_AREA)
new_2 = cv2.resize(new_2, None, fx=0.3, fy=0.3, interpolation=cv2.INTER_AREA)

# 创建窗口并设置显示位置
cv2.namedWindow('dog')
cv2.moveWindow('dog', 0, 0) # 将 'dog' 窗口放置在屏幕的左上角

cv2.namedWindow('new')
cv2.moveWindow('new', 400, 0) # 将 'new' 窗口放置在屏幕上方,横向偏移400像素

cv2.namedWindow('new_2')
cv2.moveWindow('new_2', 800, 0) # 将 'new_2' 窗口放置在屏幕的右上方,横向偏移800像素

# 显示图像
cv2.imshow('dog', dog)
cv2.imshow('new', new)
cv2.imshow('new_2', new_2)

# 等待按键,并关闭所有窗口
cv2.waitKey(0)
cv2.destroyAllWindows()

image-20241107145706672

三、图像的旋转

1
2
3
4
5
6
7
8
9
(function) def rotate(
src: MatLike,
rotateCode: int,
dst: MatLike | None = ...
) -> MatLike

ROTATE_90_CLOCKWISE: int # 顺时针90度
ROTATE_180: int # 顺时针180度
ROTATE_90_COUNTERCLOCKWISE: int # 逆时针90度
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import cv2
import numpy as np

dog = cv2.imread('./dog.jpeg')
new = cv2.rotate(dog, cv2.ROTATE_90_CLOCKWISE)
new2 = cv2.rotate(dog, cv2.ROTATE_180)


# 调整图像大小
dog = cv2.resize(dog, None, fx=0.2, fy=0.2, interpolation=cv2.INTER_AREA)
new = cv2.resize(new, None, fx=0.2, fy=0.2, interpolation=cv2.INTER_AREA)
new2 = cv2.resize(new2, None, fx=0.2, fy=0.2, interpolation=cv2.INTER_AREA)

# 创建窗口并设置显示位置
cv2.namedWindow('dog')
cv2.moveWindow('dog', 0, 0) # 将 'dog' 窗口放置在屏幕的左上角

cv2.namedWindow('new')
cv2.moveWindow('new', 400, 0) # 将 'new' 窗口放置在屏幕上方,横向偏移400像素

cv2.namedWindow('new2')
cv2.moveWindow('new2', 800, 0) # 将 'new_2' 窗口放置在屏幕的右上方,横向偏移800像素


cv2.imshow('dog', dog)
cv2.imshow('new', new)
cv2.imshow('new2', new2)

cv2.waitKey(0)

image-20241107150230563

四、仿射变换之图像平移

仿射API

1
2
3
4
5
6
7
8
9
10
(function)
def warpAffine(
src: MatLike, # 图片
M: MatLike, # 变换矩阵
dsize: Size, # 输出尺寸大小
dst: MatLike | None = ...,
flags: int = ..., # 差值算法
borderMode: int = ..., # 边界外推法标志
borderValue: Scalar = ... # 填充边界的值
) -> MatLike: ...
  • 平移矩阵
    • 矩阵中的每个像素由(x, y)组成
    • 因此,其变换矩阵是2x2的矩阵
    • 平移向量为2x1的向量,所在平移矩阵为2x3矩阵
1
2
3
4
5
6
7
8
9
10
11
import cv2
import numpy as np

dog = cv2.imread('./dog.jpeg')
h, w, ch = dog.shape
M = np.float32([[1, 0, 500], [0, 1, 0]]) # 水平向右移动500
new = cv2.warpAffine(dog, M, (w, h))

cv2.imshow('dog', dog)
cv2.imshow('new', new)
cv2.waitKey(0)

image-20241107151138851

五、仿射变换之获取变换矩阵

1
2
3
4
5
(function) def getRotationMatrix2D(
center: Point2f, # 变换的中心点
angle: float, # 旋转角度,角度为逆时针
scale: float # 缩放比例,缩放图片,但是整体的大小不变
) -> MatLike
1
2
3
4
5
6
7
8
9
10
11
12
13
import cv2
import numpy as np

dog = cv2.imread('./dog.jpeg')
h, w, ch = dog.shape
# 中心点为x,y
M = cv2.getRotationMatrix2D((w/2, h/2), 15, 0.5)
# 如果想改变新图像的尺寸,需要修改dsize
new = cv2.warpAffine(dog, M, (int(w/2), int(h/2)))

# cv2.imshow('dog', dog)
cv2.imshow('new', new)
cv2.waitKey(0)

image-20241107152515026

六、仿射变换之变换矩阵之二

image-20241107153216513

1
2
3
4
5
(function)
def getAffineTransform(
src: MatLike, # 刚开始的3个点
dst: MatLike # 变换后的3个点
) -> MatLike: ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import cv2
import numpy as np

dog = cv2.imread('./dog.jpeg')
h, w, ch = dog.shape

src = np.float32([[400, 300], [800, 300], [400, 1000]])
dst = np.float32([[200, 400], [600, 500], [150, 1100]])
# 中心点为x,y
M = cv2.getAffineTransform(src, dst)
# 如果想改变新图像的尺寸,需要修改dsize
new = cv2.warpAffine(dog, M, (w, h))

# cv2.imshow('dog', dog)
cv2.imshow('new', new)
cv2.waitKey(0)

image-20241107153110329

七、OpenCV透视变换

将一种坐标系换为另一种坐标系

image-20241107153727167

找出页面的四个角,给出最后展出的四个角

1
2
3
4
5
6
(function)
def getPerspectiveTransform(
src: MatLike,
dst: MatLike,
solveMethod: int = ...
) -> MatLike: ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import cv2
import numpy as np

img = cv2.imread('123.png')

src = np.float32([[100, 1100], [2100, 1100], [0, 4000], [2500, 3900]])
dst = np.float32([[0, 0], [2300, 0], [0, 3000], [2300, 3000]])
M = cv2.getPerspectiveTransform(src, dst)

new = cv2.warpPerspective(img, M, (2300, 3000))

cv2.imshow('origin', img)
cv2.imshow('new', new)
cv2.waitKey(0)
-------------本文结束感谢您的阅读-------------