数据增强操作
img= cv2.imread(r'XXX\q_1.JPG') #图像转置操作
img1 = cv2.transpose(img)
#cv2.flip 图像不会改变尺寸
img_flip = cv2.flip(img,0) #上下对称 x轴
img_flip1 = cv2.flip(img,1) #左右 y轴
img_flip2 = cv2.flip(img,-1) #中心对称
RGBA转RGB
import os
import glob
from PIL import Image
root_dir = R'XX\h_img'
dirs = os.listdir(root_dir)
for path in dirs:
jpeg = os.path.join(root_dir,path)
img = Image.open(jpeg)
if img.mode != 'RGB':
img = img.convert('RGB')
print(path)
os.remove(jpeg)
img.save(jpeg)
图像使用鼠标实现任意角度旋转
import PIL.Image as Image
import matplotlib.pyplot as plt
from pylab import mpl
# 设置显示中文字体
mpl.rcParams["font.sans-serif"] = ["SimHei"]
# 设置正常显示符号
mpl.rcParams["axes.unicode_minus"] = False
from torchvision import transforms as transforms
import torchvision.transforms.functional as TF
class PLOT_2(object):
def __init__(self,p1title='原图', p2title='旋转',wtitle='1'):
self.sub_title = [p1title, p2title] # 4个子图的标题
self.fig, self.ax = plt.subplots(1, 1,figsize=(400,200)) # 创建1X2子图
#self.fig.subplots_adjust(wspace=0.3, hspace=0.3) # 设置子图之间的间距
self.fig.canvas.manager.set_window_title(wtitle) # 设置窗口标题
self.fig.canvas.mpl_connect('button_press_event', self.onclick)
# 子图字典,key为子图的序号,value为子图句柄
self.angle = 0
self.img_or_path = r'E:\damage_data\q_img'
self.img_or_save = os.path.join(self.img_or_path,'q_jz')
if not os.path.exists(self.img_or_save):
os.makedirs(self.img_or_save)
self.img_or_lis = os.listdir(self.img_or_path)
self.img_index = 0
self.img = Image.open(os.path.join(self.img_or_path,self.img_or_lis[self.img_index]))
self.or_img =self.img
self.ax.imshow(self.img)
def showPlot(self):
""" 显示曲线 会将进程卡在这里"""
plt.show()
def onclick(self,event):
print('hell',self.angle)
mouse = str( event.button)
if mouse == 'MouseButton.LEFT':
self.angle += 1
self.img = TF.rotate(self.or_img, -(self.angle))
if mouse == 'MouseButton.RIGHT':
if self.angle != 0:
with open(r'E:\code\yolov5-6.1\angle.txt','a') as f:
f.write(str(self.img_or_lis[self.img_index])+':'+str(self.angle)+'\n')
self.img.save(os.path.join(self.img_or_save,self.img_or_lis[self.img_index]))
self.img_index += 1
self.img = Image.open(os.path.join(self.img_or_path,self.img_or_lis[self.img_index]))
self.or_img =self.img
self.angle = 0
if mouse == 'MouseButton.FORWARD':
self.angle +=5
self.img = TF.rotate(self.or_img, -(self.angle))
if mouse == 'MouseButton.BACK':
self.angle -= 5
self.img = TF.rotate(self.or_img, -(self.angle))
plt.cla()
self.ax.set_title(str(self.img_or_lis[self.img_index])+': '+str(self.angle), fontsize=12)
self.ax.imshow(self.img)
plt.draw()
if __name__ == "__main__":
img_fig = PLOT_2()
img_fig.showPlot()