我们可以利用仿射变换来翻转图片仿射变换参考链接 :
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 cvimport numpy as npimport matplotlib.pyplot as pltfrom PIL import Imageimg0 = 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
import cv2 as cvimport numpy as npimport matplotlib.pyplot as pltfrom PIL import Imageimg0 = 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 cvimport numpy as npimport matplotlib.pyplot as pltfrom PIL import Imageimg0 = 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] while True : cv.imshow('new_img' , new_img) if (cv.waitKey(1 ) == 27 ): break
输出:
近期评论