目录
方案A
图片和标签统一命名
需要提前建好文件夹
# -*- coding:utf-8 -*-
import sys
# sys.path.append('E:\\Anaconda\\libs')
import os # os:操作系统相关的信息模块
import shutil
from os.path import join
import cv2
import numpy as np
import random # 导入随机函数
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
# 存放原始图片地址
data_base_dir = r'E:\VINS\train'
file_dir = r'E:\VINS\train'
dest_dir1 = r"E:\VINS\train01"
dest_dir2 = r"E:\VINS\train02"
dest_dir3 = r"E:\VINS\train03"
dest_dir4 = r"E:\VINS\train04"
dest_dir5 = r"E:\VINS\train05"
dest_dir6 = r"E:\VINS\train06"
dest_dir7 = r"E:\VINS\train07"
dest_dir8 = r"E:\VINS\train08"
dest_dir9 = r"E:\VINS\train09"
dest_dir10 = r"E:\VINS\train10"
dest_dir11 = r"E:\VINS\train11"
dest_dir12 = r"E:\VINS\train12"
file_list = [] # 建立列表,用于保存图片信息
# 读取图片文件,并将图片地址、图片名和标签写到txt文件中
def imgBrightness(img1, c, b):
rows, cols, channels = img1.shape
blank = np.zeros([rows, cols, channels], img1.dtype)
rst = cv2.addWeighted(img1, c, blank, 1-c, b)
return rst
def noise(img,snr):
h=img.shape[0]
w=img.shape[1]
img1=img.copy()
sp=h*w # 计算图像像素点个数
NP=int(sp*(1-snr)) # 计算图像椒盐噪声点个数
for i in range (NP):
randx=np.random.randint(1,h-1) # 生成一个 1 至 h-1 之间的随机整数
randy=np.random.randint(1,w-1) # 生成一个 1 至 w-1 之间的随机整数
if np.random.random()<=0.5: # np.random.random()生成一个 0 至 1 之间的浮点数
img1[randx,randy]=0
else:
img1[randx,randy]=255
return img1
for file in os.listdir(data_base_dir): # file为current_dir当前目录下图片名
if file.endswith(".jpg"): # 如果file以jpg结尾
for item in range(13):
# print(file)
imageName = "VIN" + str(item) + file[:-4] + '.jpg'
if item == 0:
# 原图不复制了
scr_dir = join(file_dir, file)
# shutil.copyfile(scr_dir, det)
# 增强图片
if item == 1:
img = cv2.imread(scr_dir, cv2.IMREAD_GRAYSCALE)
gaussianBlur = cv2.GaussianBlur(img, (3,3),3)
blur = cv2.blur(gaussianBlur, (5, 5))
# cv2.imshow("img1", blur)
# cv2.waitKey()
det = join(dest_dir1, imageName)
cv2.imwrite(det, blur)
if item == 2:
img = cv2.imread(scr_dir)
boxblur = cv2.boxFilter(img, -1, (9,9))
# cv2.imshow("img2", boxblur)
# cv2.waitKey()
det = join(dest_dir2, imageName)
cv2.imwrite(det, boxblur)
# 加椒盐噪声小
if item == 3:
img = cv2.imread(scr_dir, cv2.IMREAD_GRAYSCALE)
noiseimage = noise(img, 0.98) # 将信噪比设定为0.6
# cv2.imshow("img3", noiseimage)
# cv2.waitKey()
det = join(dest_dir3, imageName)
cv2.imwrite(det, noiseimage)
# 加椒盐噪声中
if item == 4:
img = cv2.imread(scr_dir)
noiseimage4 = noise(img, 0.90) # 将信噪比设定为0.6
# cv2.imshow("img4", noiseimage4)
# cv2.waitKey()
det = join(dest_dir4, imageName)
cv2.imwrite(det, noiseimage4)
# 加椒盐噪声多
if item == 5:
img = cv2.imread(scr_dir, cv2.IMREAD_GRAYSCALE)
noiseimage5 = noise(img, 0.86) # 将信噪比设定为0.6
# cv2.imshow("img5", noiseimage5)
# cv2.waitKey()
det = join(dest_dir5, imageName)
cv2.imwrite(det, noiseimage5)
# 加椒盐噪声加高斯模糊
if item == 6:
img = cv2.imread(scr_dir)
noiseimage = noise(img, 0.98) # 将信噪比设定为0.6
gaussianBlur6 = cv2.GaussianBlur(noiseimage, (3, 3), 3)
# cv2.imshow("img3", gaussianBlur6)
# cv2.waitKey()
det = join(dest_dir6, imageName)
cv2.imwrite(det, gaussianBlur6)
# 加椒盐噪声加高斯模糊
if item == 7:
img = cv2.imread(scr_dir, cv2.IMREAD_GRAYSCALE)
noiseimage = noise(img, 0.98) # 将信噪比设定为0.6
boxblur7 = cv2.boxFilter(noiseimage, -1, (9,9))
# cv2.imshow("img7", boxblur7)
# cv2.waitKey()
det = join(dest_dir7, imageName)
cv2.imwrite(det, boxblur7)
# 高斯模糊加椒盐噪声
if item == 8:
img = cv2.imread(scr_dir)
gaussianBlur = cv2.GaussianBlur(img, (5,5),3)
noiseimage8 = noise(gaussianBlur, 0.98) # 将信噪比设定为0.6
# cv2.imshow("img8", noiseimage8)
# cv2.waitKey()
det = join(dest_dir8, imageName)
cv2.imwrite(det, noiseimage8)
# 双边模糊加椒盐噪声
if item == 9:
img = cv2.imread(scr_dir, cv2.IMREAD_GRAYSCALE)
bilBlur = cv2.bilateralFilter(img, 1, 25, 10)
noiseimage9 = noise(bilBlur, 0.92) # 将信噪比设定为0.6
# cv2.imshow("img9", noiseimage9)
# cv2.waitKey()
det = join(dest_dir9, imageName)
cv2.imwrite(det, noiseimage9)
# 调亮
if item == 10:
img = cv2.imread(scr_dir)
# 第二个参数调节亮度,越大越亮,越小越暗
rst = imgBrightness(img, 1.6, 1)
# cv2.imshow("img10", rst)
# cv2.waitKey()
det = join(dest_dir10, imageName)
cv2.imwrite(det, rst)
# 调亮并模糊
if item == 11:
img = cv2.imread(scr_dir)
# 第二个参数调节亮度,越大越亮,越小越暗
rst = imgBrightness(img, 1.4, 4)
boxblur11 = cv2.boxFilter(rst, -1, (3,3))
# cv2.imshow("img11", boxblur11)
# cv2.waitKey()
det = join(dest_dir11, imageName)
cv2.imwrite(det, boxblur11)
# 调亮加椒盐噪声
if item == 12:
img = cv2.imread(scr_dir)
# 第二个参数调节亮度,越大越亮,越小越暗
rst = imgBrightness(img, 1.28, 4)
noiseimage12 = noise(rst, 0.92) # 将信噪比设定为0.6
# cv2.imshow("img12", noiseimage12)
# cv2.waitKey()
det = join(dest_dir12, imageName)
cv2.imwrite(det, noiseimage12)
print("all down")
方案B
图片和标签一起变化
import cv2
import os
if __name__ == "__main__":
Labelsfilepath = "DataSet/Labels/"
Imagesfilepath = "DataSet/Images/"
saveNewLabelsPath = "DataSet/NewLabels/"
saveNewImagesPath = "DataSet/NewImages/"
files = os.listdir(Labelsfilepath)
for i, name in enumerate(files):
txtFile = open(Labelsfilepath + name) #读取文件名
txtList = txtFile.readlines()
img = cv2.imread(Imagesfilepath + name[0:-4] + ".jpg")
if img is None:
break
Pheight, Pwidth, Pdepth = img.shape
img1 = cv2.flip(img,1) #水平翻转
cv2.imwrite(saveNewImagesPath + name[0:-4]+"_"+ "1" +".jpg", img1)
lines_1 = []
img2 = cv2.flip(img,0) #垂直翻转
cv2.imwrite(saveNewImagesPath + name[0:-4]+"_"+ "2" +".jpg", img2)
lines_2 = []
img3 = cv2.flip(img,0)
img3 = cv2.flip(img3,1) #对角翻转
cv2.imwrite(saveNewImagesPath + name[0:-4]+"_"+ "3" +".jpg", img3)
lines_3 = []
for j in txtList:
oneline = j.strip().split(" ") #以空格分隔第一行
label = oneline[0] #标注类别
cx = float(oneline[1])
cy = float(oneline[2])
w = float(oneline[3])
h = float(oneline[4])
new_cx = (Pwidth - cx*Pwidth -1)/Pwidth
new_cy = (Pheight- cy*Pheight -1)/Pheight
line_1 = "%s %.6f %.6f %.6f %.6f\n" % (label, new_cx, cy, w, h)
line_2 = "%s %.6f %.6f %.6f %.6f\n" % (label, cx, new_cy, w, h)
line_3 = "%s %.6f %.6f %.6f %.6f\n" % (label, new_cx, new_cy, w, h)
lines_1.append(line_1)
lines_2.append(line_2)
lines_3.append(line_3)
txt_name_1 = saveNewLabelsPath + name[0:-4]+"_"+ "1" +".txt"
txt_name_2 = saveNewLabelsPath + name[0:-4]+"_"+ "2" +".txt"
txt_name_3 = saveNewLabelsPath + name[0:-4]+"_"+ "3" +".txt"
with open(txt_name_1, "w") as f:
f.writelines(lines_1)
with open(txt_name_2, "w") as f:
f.writelines(lines_2)
with open(txt_name_3, "w") as f:
f.writelines(lines_3)
print("all finish")
方案C 每张图片旋转45度,然后标注
import cv2
import os
import numpy as np
def rotate(ps,m):
pts = np.float32(ps).reshape([-1, 2]) # 要映射的点
pts = np.hstack([pts, np.ones([len(pts), 1])]).T
target_point = np.dot(m, pts)
target_point = [[target_point[0][x],target_point[1][x]] for x in range(len(target_point[0]))]
return target_point
if __name__ == "__main__":
Imagesfilepath = "DataSet/Images/"
saveNewImagesPath = "DataSet/NewImages/"
files = os.listdir(Imagesfilepath)
for i, name in enumerate(files):
img = cv2.imread(Imagesfilepath + name[0:-4] + ".jpg")
Pheight, Pwidth, Pdepth = img.shape
#中心旋转45度
for rot in range(1,8):
angle = rot*45
matRotate = cv2.getRotationMatrix2D((Pwidth*0.5, Pheight*0.5), angle, 1)
dst_img = cv2.warpAffine(img, matRotate, (Pwidth,Pheight))
cv2.imwrite(saveNewImagesPath + name[0:-4] + "_" + str(angle) +".jpg", dst_img)
最后标签相关的,可以通过labelimg查看是否正确。
两个都要选择,前一个是图片的目录,后一个是标签的目录 ,如果标好的话这两个选了就可以打开了。