图像翻转

我们可以利用仿射变换来翻转图片
仿射变换参考链接:

https://blog.csdn.net/qq_43309286/article/details/101595206

输入:
在这里插入图片描述

图像翻转90度

可以直接利用矩阵转置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21


import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

img0 = cv.imread('t3.jpg', 0)
img0 = cv.resize(img0, (0, 0), fx=0.2, fy=0.2)

height, width = img0.shape

img_arr = np.array(img0)

new_img = np.zeros((height, width), dtype='uint8')

new_img = img_arr.T

cv.imshow('new_img', new_img)
cv.waitKey(0)
cv.destroyAllWindows()

输出:
在这里插入图片描述

图片翻转180度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 翻转180度

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

img0 = cv.imread('t3.jpg', 0)

height, width = img0.shape

img_arr = np.array(img0)

new_img = np.zeros((height, width), dtype='uint8')

for i in range(height):
for j in range(width):
new_img[height-1-i][width-1-j] = img0[i][j]

cv.imshow('new_img', new_img)
cv.waitKey(0)
cv.destroyAllWindows()

输出:在这里插入图片描述

图片倾斜

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 图片倾斜

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

img0 = cv.imread('t3.jpg', 0)

height, width = img0.shape

img_arr = np.array(img0)

new_img = np.zeros((height+100, width+100), dtype='uint8')

for i in range(height):
for j in range(width):
new_img[int(i*np.cos(np.pi/4)-j*np.sin(np.pi/4))][int(i*np.sin(np.pi/4)+j*np.cos(np.pi/4))] = img0[i][j]
# new_img[i][2*i+j] = img0[i][j]
while True:
cv.imshow('new_img', new_img)
if (cv.waitKey(1) == 27):
break

输出:
在这里插入图片描述