【无标题】

数据增强操作

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()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值