最近在做 object_detection 時(shí)需要用到一些對(duì)圖片的預(yù)處理腳本。
比如要批量剪切圖片侨核、圖片灰度颂跨、二值化、縮放荧飞、豐富數(shù)據(jù)等操作凡人,寫了如下幾個(gè)功能的腳本
images 文件加內(nèi)為圖片處理
- 將某目錄內(nèi)圖片全部轉(zhuǎn)化成 500*500
python resize.py ./test/ 500
- 將某目錄內(nèi)圖片隨機(jī)切成 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叹阔。二值化圖像每個(gè)像素用 8 個(gè)bit表示挠轴,0 表示黑,255 表示白耳幢。閾值的作用就是大于閾值為白岸晦,小于閾值為黑
在圖像檢測中較常用,可以去掉過多的干擾和噪點(diǎn)
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)中隨機(jī)增加剪裁、灰度等豐富樣本數(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'