How are you all doing the hot days in August? I'm about to melt, but while listening to Kenshi Yonezu's new song </ strong> I'm coding in a room where I don't know the day and night when the air conditioner works. </ Strong>
What do you guys say summer?
No, summer is the summer of *** Data Augmentation! !! !! *** *** So I will write a method of data augmentation with openCV instead of a memo.
--People who want to data augment a large amount of data while maintaining the directory structure!
--People who get angry at someone if they don't create data quickly
--People who have trouble programming!
--Execute with the following command
python3 img_change.py input_directory output_directory
--Example
python3 img_change.py /data/imageset /data/imagser_change
img_change.py
# -*- coding: utf-8 -*-
import numpy as np
import os
import cv2
import sys
import pathlib
import glob
from scipy.signal import qspline1d, qspline1d_eval, qspline2d,cspline2d
if len(sys.argv) != 3:
print("python3 test.py inputdir outputdir")
sys.exit()
else:
input_path = sys.argv[1]
file_list = glob.glob(input_path + "/**/**.png ", recursive=True)
for item in file_list:
split_name = item.split('/')
output_name = sys.argv[2] + "/" + split_name[-3] + "/" + split_name[-2] + "/" + split_name[-1]
output_dir = sys.argv[2] + "/" + split_name[-3] + "/" + split_name[-2]
pathlib.Path(output_dir).mkdir(parents=True,exist_ok=True)
img = cv2.imread(item, cv2.IMREAD_UNCHANGED) # read image
#Image processing part
###Increase the brightness of the image
chg_img=img*1.2 #Brightness doubles
###Image resizing
height = img.shape[0]
#img.shape[0]*0.5 half the size of the original
width = img.shape[1]
chg_img = cv2.resize(img , (int(width), int(height)))
###CLAHE (redistribution so that the histogram is as evenly distributed as possible)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
chg_img = clahe.apply(img)
###noise
row,col,ch = img.shape
#White
pts_x = np.random.randint(0, col-1 , 1000) #From 0(col-1)Make a thousand random numbers up to
pts_y = np.random.randint(0, row-1 , 1000)
img[(pts_y,pts_x)] = (255,255,255) #y,Note that the order is x
#black
pts_x = np.random.randint(0, col-1 , 1000)
pts_y = np.random.randint(0, row-1 , 1000)
img[(pts_y,pts_x)] = (0,0,0)
###Distort the image
chg_img = cv2.flip(img, 1) #Flip horizontally
chg_img = cv2.flip(img, 0) #Invert vertically
cv2.imwrite(output_name,chg_img ) #write image
--In the above program, the program is created with the following directory structure.
$ tree
/data
└── val
├── A1
│ │
│ ├── 0.png
│ ├── 1.png
│ ├── 2.png
│ └── ....
├── B1
│ │
│ ├── 0.png
│ ├── 1.png
│ ├── 2.png
│ └── ....
│
train
├── A1
│ │
│ ├── 0.png
│ ├── 1.png
│ ├── 2.png
│ └── ....
├── B1
│ │
│ ├── 0.png
│ ├── 1.png
│ ├── 2.png
│ └── ....
│
test
├── A1
│ │
│ ├── 0.png
│ ├── 1.png
│ ├── 2.png
│ └── ....
├── B1
│ │
│ ├── 0.png
│ ├── 1.png
│ ├── 2.png
│ └── ....
│
└──
--Therefore, it feels like getting all .png in the directory two directories below the specified directory as a list
――There are various ways to change the image, so check it out with OpenCV image processing. --Reference site Image processing with Python (2) Data Augmentation (image padding)
Recommended Posts