图像数据增广

import os
import numpy as np
import cv2
import random
import csv

def save_to(name,data):
    f = open(name,"a+",newline='',encoding='utf-8')
    csv_writer = csv.writer(f)
    csv_writer.writerow(data)
    f.close()

k=0
path=r"E:\qichacha\img\\beijing\\" #生成的营业执照的路径
for i in os.listdir(path):
    back = os.path.join(path, i)
    img = cv2.imread(back)
#平移
    # 构造移动矩阵H
    # 在x轴方向移动多少距离,在y轴方向移动多少距离
    tx = int(random.randint(0, 150))
    ty = int(random.randint(0, 360))
    # tx=50;ty=25
    H = np.float32([[1, 0, tx], [0, 1, ty]])
    height, width = img.shape[0:2]
    res = cv2.warpAffine(img, H, (width, height))
    cv2.imencode('.jpg', res)[1].tofile("E:\qichacha\img\zengguang\pingyi\{}".format(i))
    a = [tx,ty]
    save_to("E:\qichacha\data\zuobiao\\csv1\{}.csv".format(i[:-4]), a) #保存平移距离,为后面检测框做准备,保存名要方便读取对应的图片
    # print("成功--{}".format(i))

#旋转
    height, width = img.shape[0:2]
    # 第一个参数是旋转中心,第二个参数是旋转角度,第三个参数是缩放比例
    dx = int(random.randint(-10, 10))
    M1 = cv2.getRotationMatrix2D((width / 2, height / 2), dx, 1)
    M2 = cv2.getRotationMatrix2D((width / 2, height / 2), dx, 0.9)
    res1 = cv2.warpAffine(img, M1, (width, height))
    res2 = cv2.warpAffine(img, M2, (width, height))
    a = [dx]
    save_to("E:\qichacha\data\zuobiao\\csv2\{}.csv".format(i[:-4]), a)
    cv2.imencode('.jpg', res1)[1].tofile("E:\\qichacha\\img\\zengguang\\xuanzhuan\\{}".format(i))#保存旋转角度
    cv2.imencode('.jpg', res2)[1].tofile("E:\\qichacha\\img\\zengguang\\xuanzhuan1\\{}".format(i))

# 仿射变换
    height, width = img.shape[0:2]
    #放射变换  str->drc变换坐标
    matsrc=np.float32([[0,0],[0,height-1],[width-1,0]])
    matdrc=np.float32([[10,10],[10,height-50],[width-50,10]]) #(左上角,左下角,右上角)
    mataffine=cv2.getAffineTransform(matsrc,matdrc) #形成组合矩阵
    dstl=cv2.warpAffine(img,mataffine,(width,height))
    cv2.imencode('.jpg', dstl)[1].tofile("E:\qichacha\img\zengguang\\fangshe\{}".format(i))

#亮度
    img2=np.uint8(np.clip((1.5 * img + 125*(1-1.5)), 0, 255))#大于1变亮
    img3 = np.uint8(np.clip((0.5 * img + 125 * (1 - 0.5)), 0, 255))#小于1变暗
    cv2.imencode('.jpg', img2)[1].tofile("E:\qichacha\img\zengguang\liangdu\{}".format(i))
    cv2.imencode('.jpg', img3)[1].tofile("E:\qichacha\img\zengguang\liangdu1\{}".format(i))
    # print("成功--{}".format(m))
    # m+=1

#滤波2选一:模糊 一个高斯模糊,一个平均值模糊
    dImg2 = cv2.GaussianBlur(img, (7, 7), 0)
    dImg1=cv2.blur(img,(5,5))  #封装函数
    cv2.imencode('.jpg', dImg2)[1].tofile("E:\qichacha\img\zengguang\mohu\{}".format(i))
    cv2.imencode('.jpg', dImg1)[1].tofile("E:\qichacha\img\zengguang\mohu1\{}".format(i))
    # print("成功--{}".format(m))
    # m += 1

#裁剪
    sp = img.shape  # 获取图像形状:返回【行数值,列数值】列表
    sz1 = sp[0]  # 图像的高度(行 范围)
    sz2 = sp[1]  # 图像的宽度(列 范围)
    # sz3 = sp[2]                #像素值由【RGB】三原色组成
    # 你想对文件的操作
    a = int(100)  # y start
    b = int(sz1 - 100)  # y end
    c = int(40)  # x start
    d = int(sz2 - 100)  # x end
    caijian = img[a:b, c:d]  # 裁剪图像
    cv2.imencode('.jpg', caijian)[1].tofile("E:\qichacha\img\zengguang\caijian\{}".format(i))
#椒盐噪声
    h, w = img.shape[:2]
    jiaoyan=img
    nums = 10000
    rows = np.random.randint(0, h, nums, dtype=np.int64)
    cols = np.random.randint(0, w, nums, dtype=np.int64)
    for n in range(nums):
        if n % 2 == 1:
            jiaoyan[rows[n], cols[n]] = (255, 255, 255)  # 奇数则产生白色
        else:
            jiaoyan[rows[n], cols[n]] = (0, 0, 0)  # 偶数则产生黑椒盐色
    cv2.imencode('.jpg', jiaoyan)[1].tofile("E:\qichacha\img\zengguang\jiaoyan\{}".format(i))
#高斯噪声
    img = np.array(img / 255, dtype=float)
    noise = np.random.normal(0, (0.002) ** 0.5, img.shape)
    out = img + noise
    if out.min() < 0:
        low_clip = -1.
    else:
        low_clip = 0.
    out = np.clip(out, low_clip, 1.0)
    out = np.uint8(out * 255)
    cv2.imencode('.jpg', out)[1].tofile("E:\qichacha\img\zengguang\gaozao\{}".format(i))
    print("成功--{}".format(k))
    # m+=1
    k+=1

# 平移
# import cv2 as cv
# import numpy as np
# import math
# import os
#
# #读取文件
# def read_images(n):
#     img1 = cv.imread('./imgs/yinliao/{}.jpg'.format(n))
#     return img1
#
# #图片平移
# def save_pingyi(tx,ty,n,img1):
#     affine_arr=np.float32([[1,0,tx],[0,1,ty]])    #函数平移,
#     pingyi=cv.warpAffine(img1,affine_arr,(width,height))  #参数:【原图,平移的矩阵,(宽,高)】
#     cv.namedWindow("pingyi", cv.WINDOW_NORMAL)
#     # cv.imwrite('./imgs/pingyi/{}.jpg'.format(n),pingyi)
#     cv.imshow("pingyi",pingyi)
#     cv.waitKey(0)
#
#
# #图片旋转+平移
# def save_xuanzhuan(tx,ty,angle,n,img1):
#     affine_arr=np.float32([[1,0,tx],[0,1,ty]])    #函数平移,
#     pingyi=cv.warpAffine(img1,affine_arr,(width,height))  #参数:【原图,平移的矩阵,(宽,高)】
#     matrix = np.float32([[math.cos(angle), math.sin(angle), 0], [-math.sin(angle), math.cos(angle), 0]])  # 旋转的矩阵
#     xuanzhuan = cv.warpAffine(pingyi, matrix, (width, height))  # 参数:【原图,旋转矩阵,(宽,高)】
#     # cv.imwrite('./imgsanzhuan/{}.jpg'.format(n),xuanzhuan)
#     cv.imshow("xuanzhuan",xuanzhuan)
#     cv.waitKey(0)
#
# #翻转
# def save_fanzhuan(n,img1):
#     fanzhuan = cv.flip(img1, -1)
#     # cv.imwrite('./imgszhuan/{}.jpg'.format(n),fanzhuan)
#     cv.imshow("fanzhuan",fanzhuan)
#     cv.waitKey(0)
# #亮度
# def save_liangdu(light,n,img1):
#     liangdu = np.uint8(np.clip((light * img1 + 125 * (1 - light)), 0, 255))
#     # cv.imwrite('./imgsangdu/{}.jpg'.format(n), liangdu)
#     cv.imshow("liangdu",liangdu)
#     cv.waitKey(0)
# #裁剪
# def save_caijian(n,img1):
#     sp = img1.shape  # 获取图像形状:返回【行数值,列数值】列表
#     sz1 = sp[0]  # 图像的高度(行 范围)
#     sz2 = sp[1]  # 图像的宽度(列 范围)
#     # sz3 = sp[2]                #像素值由【RGB】三原色组成
#     # 你想对文件的操作
#     a = int(540)  # y start
#     b = int(sz1 - 100)  # y end
#     c = int(140)  # x start
#     d = int(sz2 - 100)  # x end
#     caijian = img1[a:b, c:d]  # 裁剪图像
#     # cv.imwrite('./imgs/caijian/{}.jpg'.format(n), caijian)
#     cv.imshow("caijian",caijian)
#     cv.waitKey(0)
#
# if __name__ == '__main__':
#     file_dir='./imgs/yinliao'         #营业执照待处理图片
#     for root, dirs, files in os.walk(file_dir):
#         #分别获取跟目录,当前目录,子文件
#         print(files) #当前路径下所有非目录子文件
#         print(len(files))
#
#     for n in range(1, len(files)):
#         img1=read_images(n)
#         height = img1.shape[0]  # 高
#         width = img1.shape[1]  # 宽
#
#         #平移
#         tx = 25;ty = 40  # 平移的像素
#         save_pingyi(tx,ty,n,img1)
#
#         # 旋转+平移
#         tx = -250;ty = -400  # 平移的像素
#         angle = -50  # 旋转度数
#         save_xuanzhuan(tx,ty,angle,n,img1)
#
#         # xy都翻转
#         save_fanzhuan(n,img1)
#
#         # 亮度调节
#         light = 0.3  # 亮度为原始的0.3倍
#         save_liangdu(light,n,img1)
#
#         #裁剪
#         save_caijian(n,img1)




检测框:https://blog.csdn.net/yangzheng_520/article/details/120215948

关注一下,谢谢!

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值