深度學習中對圖片預處理的幾個方法

最近在做 object_detection 時需要用到一些對圖片的預處理腳本。
比如要批量剪切圖片、圖片灰度、二值化、縮放、豐富數(shù)據(jù)等操作,寫了如下幾個功能的腳本

images 文件加內(nèi)為圖片處理

  • 將某目錄內(nèi)圖片全部轉(zhuǎn)化成 500*500
python resize.py ./test/ 500
  • 將某目錄內(nèi)圖片隨機切成 300*300
python randomCrop.py ./test/ 300
  • 將某目錄內(nèi)圖片類型轉(zhuǎn)為RGB
python convertToRGB.py ./test/
  • 將某目錄內(nèi)圖片轉(zhuǎn)為灰度圖
python convertToL.py ./test/
  • 將某目錄內(nèi)圖片做二值化處理,其中閾值為 120。二值化圖像每個像素用 8 個bit表示,0 表示黑,255 表示白。閾值的作用就是大于閾值為白,小于閾值為黑
    在圖像檢測中較常用,可以去掉過多的干擾和噪點
python convertTo1.py ./test/ 120
  • 將某張圖片按照閾值 0~255 生成 256 張圖片,用于選擇合理的閾值
python getAllBinarizationImg.py test/github.JPG
  • 將某目錄內(nèi)圖片翻轉(zhuǎn) 45 度,共翻轉(zhuǎn) 7 次,每翻轉(zhuǎn)一次生成一張圖。適合增加樣本數(shù)量。還在在翻轉(zhuǎn)中隨機增加剪裁、灰度等豐富樣本數(shù)據(jù)
python rotate.py test 45 7

可以參考 github

核心代碼如下

import os
import sys
import math
import random
import functools
import numpy as np
from PIL import Image, ImageEnhance

def rotate(img_path, degree, num):
    img = Image.open(img_path)
    
    save_path = './rotate/'
    mkdir(save_path)

    i = 1
    while i <= num:
        img = img.rotate(degree)
        img_arr = os.path.basename(img_path).split('.')
        img_name = save_path + img_arr[0] + '_' + bytes(degree * i) + '.' + img_arr[1]
        img.save(img_name, quality=95)
        i += 1
        print 'save to ' + img_name



def randomCrop(img_path, size, scale=[0.08, 1.0], ratio=[3. / 4., 4. / 3.]):
    img = Image.open(img_path)
    aspect_ratio = math.sqrt(np.random.uniform(*ratio))
    w = 1. * aspect_ratio
    h = 1. / aspect_ratio

    bound = min((float(img.size[0]) / img.size[1]) / (w**2),
                (float(img.size[1]) / img.size[0]) / (h**2))
    scale_max = min(scale[1], bound)
    scale_min = min(scale[0], bound)

    target_area = img.size[0] * img.size[1] * np.random.uniform(scale_min,
                                                             scale_max)
    target_size = math.sqrt(target_area)
    w = int(target_size * w)
    h = int(target_size * h)

    i = np.random.randint(0, img.size[0] - w + 1)
    j = np.random.randint(0, img.size[1] - h + 1)

    img = img.crop((i, j, i + w, j + h))
    img = img.resize((size, size), Image.LANCZOS)

    if img.mode != 'RGB':
        img = img.convert('RGB')

    save_path = './random_crop/'
    mkdir(save_path)
    img_name = save_path + os.path.basename(img_path)
    img.save(img_name, quality=95)
    print 'save to ' + img_name
    return img

def resize(img_path, size):
    img = Image.open(img_path)

    img = img.resize((size, size), Image.ANTIALIAS)

    if img.mode != 'RGB':
        img = img.convert('RGB')

    save_path = './resize_' + bytes(size) + '/'
    mkdir(save_path)
    img_name = save_path + os.path.basename(img_path)
    img.save(img_name, quality=95)
    print 'save to ' + img_name
    return img

def convertToRGB(img_path):
    img = Image.open(img_path)

    if img.mode != 'RGB':
        img = img.convert('RGB')

    save_path = './convert/'
    mkdir(save_path)
    img_name = save_path + os.path.basename(img_path)
    img.save(img_name, quality=95)
    print 'save to ' + img_name
    return img

def convertToL(img_path):
    #a = np.array(Image.open(img_path).convert('L')).astype('float')
    # 
    #depth = 10.
    #grad = np.gradient(a)
    #grad_x, grad_y = grad
    # 
    #grad_x = grad_x*depth/100.
    #grad_y = grad_y*depth/100.
    #A = np.sqrt(grad_y**2+grad_y**2+1)
    #uni_x = grad_x/A
    #uni_y = grad_y/A
    #uni_z = 1./A
    # 
    #vec_el = np.pi/2.2
    #vec_az = np.pi/4
    #dx = np.cos(vec_el)*np.cos(vec_az)
    #dy = np.cos(vec_el)*np.sin(vec_az)
    #dz = np.sin(vec_el)
    # 
    #b = 255*(dx*uni_x+dy*uni_y+dz*uni_z)
    #b = b.clip(0, 225)
    # 
    #im = Image.fromarray(b.astype('uint8'))
    # 

    im = Image.open(img_path)
    im = im.convert('L')
    save_path = './toL/'
    mkdir(save_path)
    img_name = save_path + os.path.basename(img_path)
    print('success save to ' + img_name)
    im.save(img_name, quality=95)

def convertTo1(img_path, threshold):
    im = Image.open(img_path)
    Lim = im.convert('L' )

    table = []
    for i in range(256):
        if i < threshold:
            table.append(0)
        else:
            table.append(1)
    
    bim = Lim.point(table, '1' )

    save_path = './to_binarization/'
    mkdir(save_path)
    img_arr = os.path.basename(img_path).split('.')
    img_name = save_path + img_arr[0] + '_' + bytes(threshold) + '.' + img_arr[1]

    print('success save to ' + img_name)

    bim.save(img_name, quality=95)

def imgList(dir, suffix = '.JPG'):
    
    assert os.path.isdir(dir)

    list = []
    for file in os.listdir(dir):
        img_path = os.path.join(dir, file)
        if os.path.splitext(img_path)[1] == ".JPG":
            list.append(img_path)
    return list

def mkdir(dir):
    if not os.path.isdir(dir):
        os.makedirs(dir)

if __name__ == '__main__':
    print 'test'
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容