神经网络训练中图像数据预处理的一些方式
1. crop
1. 1 对于有黑色背景的图片,将数据crop在有效区域内
对于一些有黑色背景的图片,如下图:
这样的图片在做数据预处理的时候,黑色背景的无效区域,带来了很大的运算量开销。面对这种图片,我们一般先将区域限制到它的有效区域。
代码如下:
import numpy as np
from skimage.filters import threshold_otsu
from skimage import measure, exposure
import skimage
def tight_crop(img, size=None):
img_gray = np.mean(img, 2)
img_bw = img_gray > threshold_otsu(img_gray)
img_label = measure.label(img_bw, background=0)
largest_label = np.argmax(np.bincount(img_label.flatten())[1:])+1
img_circ = (img_label == largest_label)
img_xs = np.sum(img_circ, 0)
img_ys = np.sum(img_circ, 1)
xs = np.where(img_xs>0)
ys = np.where(img_ys>0)
x_lo = np.min(xs)
x_hi = np.max(xs)
y_lo = np.min(ys)
y_hi = np.max(ys)
img_crop = img[y_lo:y_hi, x_lo:x_hi, :]
return img_crop
import scipy.misc
from PIL import Image
img = scipy.misc.imread('./raw1.jpg')
img = img.astype(np.float32)
img /= 255
img_crop = tight_crop(img)
pilImage = Image.fromarray(skimage.util.img_as_ubyte(img_crop))
pilImage.show()
结果如下:
2. 对比度
接1,有一些图像可能比较模糊,对比度增强可能有很好的效果。对比度增强的方式很多。
2.1 adaptive histogram equalization
代码
def channelwise_ahe(img):
img_ahe = img.copy()
for i in range(img.shape[2]):
img_ahe[:,:,i] = exposure.equalize_adapthist(img[:,:,i], clip_limit=0.03)
return img_ahe
img_ahe = channelwise_ahe(img_crop)
pilImage = Image.fromarray(skimage.util.img_as_ubyte(img_ahe))
pilImage.show()
效果如下:
待续。。。