import cv2 import copy import os def mirror_imgs(imgs_path,save_path): for name in os.listdir(imgs_path): print(name) image = cv2.imread(os.path.join(imgs_path,name), 1); # image.imshow() print(os.path.join(imgs_path,name)) height = image.shape[0] width = image.shape[1] #channels = image.shape[2] iLR = copy.deepcopy(image) # 获得一个和原始图像相同的图像,注意这里要使用深度复制 for i in range(height): for j in range(width): iLR[height-1-i,j] = image[i, j] #cv2.imshow('image', image) #cv2.imshow('iLR', iLR) cv2.imwrite(os.path.join(save_path,name), iLR, [int(cv2.IMWRITE_JPEG_QUALITY), 100])#保存图片 #cv2.imwrite(os.path.join(save_path,name), IOU, # [int(cv2.IMWRITE_JPEG_QUALITY), 100])#保存图片 #cv2.waitKey(0) #cv2.destroyAllWindows() imgs_path='E:/leaf/' save_path="E:/leaf/savechuizhi/" mirror_imgs(imgs_path,save_path)
其中iLR[height-1-i,j] = image[i, j]表示垂直镜像,其他方向的镜像公式如下:
水平镜像可按公式
I = i
J = N - j + 1
垂直镜像可按公式
I = M - i + 1
J = j
对角镜像可按公式
I = M - i + 1
J = N - j + 1
iUD[h
-
1
-
i,j]
=
image[i,j] 垂直
iLR[i,w
-
1
-
j]
=
image[i,j] 水平
iAcross[h
-
1
-
i,w
-
1
-
j]
=
image[i,j] 垂直+水平
来源:https://www.cnblogs.com/lian-dong/p/12624995.html