神经网络训练中图像数据预处理的一些方式(一)

神经网络训练中图像数据预处理的一些方式

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

效果如下:
这里写图片描述

待续。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值