[PYTHON] Data Augmentation with openCV

Introduction

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.

Who is it for

--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

program

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

At the end

――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

Data Augmentation with openCV
Binarize photo data with OpenCV
How to Data Augmentation with PyTorch
Data analysis with python 2
Detect stoop with OpenCV
Binarization with OpenCV / Python
Visualize data with Streamlit
Reading data with TensorFlow
Data visualization with pandas
Data manipulation with Pandas!
Rotate sprites with OpenCV
Shuffle data with pandas
Normarize data with Scipy
Data analysis with Python
LOAD DATA with PyMysql
Easy TopView with OpenCV
Stumble with homebrew opencv3
Display the image after Data Augmentation with Pytorch
Sample data created with python
Embed audio data with Jupyter
Graph Excel data with matplotlib (1)
Artificial data generation with numpy
Face recognition with Python's OpenCV
"Apple processing" with OpenCV3 + Python3
Try edge detection with OpenCV
Extract Twitter data with CSV
Image editing with python OpenCV
Camera capture with Python + OpenCV
Get Youtube data with python
[Python] Using OpenCV with Python (Basic)
Clustering ID-POS data with LDA
Learn new data with PaintsChainer
Loop video loading with opencv
Graph Excel data with matplotlib (2)
Real-time edge detection with OpenCV
Save tweet data with Django
Face detection with Python + OpenCV
[PyTorch] Data Augmentation for segmentation
Get image features with OpenCV
Face recognition / cutting with OpenCV
Try OpenCV with Google Colaboratory
Try Random Erasing Data Augmentation
New Data Augmentation? [Grid Mix]
Cascade classifier creation with opencv
Data processing tips with Pandas
Using OpenCV with Python @Mac
Interpolate 2D data with scipy.interpolate.griddata
Image recognition with Keras + OpenCV
Read json data with python
Anime face detection with OpenCV
Shining life with Python and OpenCV
Save & load data with joblib, pickle
Real-time image processing basics with opencv
[Python] Using OpenCV with Python (Image Filtering)
Neural network with OpenCV 3 and Python 3
I tried trimming efficiently with OpenCV
[Python] Using OpenCV with Python (Image transformation)
[Python] Using OpenCV with Python (Edge Detection)
Erase certain colors with OpenCV + PySimpleGUI
Introducing OpenCV on Mac with homebrew
How to deal with imbalanced data