图像增强

Task

Reference

Imgaug

构造数据增强器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from imgaug import augmenters as iaa

sometimes = lambda aug: iaa.Sometimes(0.5, aug) #建立lambda表达式,

seq = iaa.Sequential(
[
sometimes(iaa.Affine(
scale={"x": (0.8, 1.2), "y": (0.8, 1.2)},#图像缩放为80%到120%之间
rotate=(-3, 3), #旋转±3度之间
order=[0, 1], #使用最邻近差值或者双线性差值
)),

iaa.SomeOf((0, 5),
[
# 将部分图像进行超像素的表示。
sometimes(
iaa.Superpixels(
p_replace=(0, 1.0),
n_segments=(20, 200)
)
),

#用高斯模糊,均值模糊,中值模糊中的一种增强。注意OneOf的用法
iaa.OneOf([
iaa.GaussianBlur((0, 3.0)),
iaa.AverageBlur(k=(2, 7)), # 核大小2~7之间,k=((5, 7), (1, 3))时,核高度5~7,宽度1~3
iaa.MedianBlur(k=(3, 11)),
]),


# 加入高斯噪声
iaa.AdditiveGaussianNoise(
loc=0, scale=(0.0, 0.05*255), per_channel=0.5
),

# 每个像素随机加减-10到10之间的数
iaa.Add((-10, 10), per_channel=0.5),


# 将RGB变成灰度图然后乘alpha加在原图上
iaa.Grayscale(alpha=(0.0, 1.0)),

],

random_order=True # 随机的顺序把这些操作用在图像上
)
],
random_order=True # 随机的顺序把这些操作用在图像上
)

使用数据增强器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
file_list = os.listdir('1_aug/')
pre_path = ('1_aug/')

aug_gen_cnt = 2

for it in file_list:
for _ in range(aug_gen_cnt):
img1 = seq.augment_image(cv2.imread(pre_path + it))
#img2 = seq.augment_image(cv2.imread(pre_path + it))

name_pos = re.search('.jpg', file_list[0]).span()
name_pre = it[:name_pos[0]]

cv2.imwrite(pre_path + name_pre + '_aug_1.jpg', img1)
# cv2.imwrite(pre_path + name_pre + '_aug_2.jpg', img2)

Opencv

  • 灰度图 To 颜色3通道图

    1
    2
    img1 = cv2.imread(it1, cv2.IMREAD_GRAYSCALE)
    img1 = cv2.cvtColor(img1, cv2.COLOR_GRAY2BGR)
  • 模糊去噪与梯度滤波处理边缘检测——拉普拉斯算子+中值模糊+反转正则化

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    def edge_aug(img):
    img_process_1 = cv2.medianBlur(img, 7)
    img_process_1_gray = cv2.cvtColor(img_process_1, cv2.COLOR_BGR2GRAY)
    cv2.Laplacian(img_process_1_gray, cv2.CV_8U, img_process_1_gray, ksize=5)
    normalizedInverse = (1.0 / 255) * (255 - img_process_1_gray)
    channels = cv2.split(img)

    for channel in channels:
    channel[:] = channel * normalizedInverse

    dst = cv2.merge(channels)

    return dst
  • Canny边缘检测

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    pre_path = 'rec_res/'
    tar_path = 'edge_enforce/'

    for img_path in os.listdir(pre_path):
    img = cv2.imread(pre_path + img_path)

    # dst_temp = edge_aug(img)
    edges = cv2.Canny(img,100,200)

    cv2.imwrite(tar_path + img_path, edges)

    #plt.imshow(edges, cmap='gray')
    #plt.show()
  • 角点检测

    1
    2
    3
    4
    5
    6
    7
    img = cv2.imread('621669_7600000020025.jpeg')
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray = np.float32(gray)
    dst = cv2.cornerHarris(gray, 2, 23, 0.04)
    img[dst>0.01 * dst.max()] = [0, 0, 255]
    plt.imshow(img)
    plt.show()
  • Resize

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    def resize_im(im, scale, max_scale=None):
    f = float(scale) / min(im.shape[0], im.shape[1])
    if max_scale != None and f * max(im.shape[0], im.shape[1]) > max_scale:
    f = float(max_scale) / max(im.shape[0], im.shape[1])
    return cv2.resize(im, None, None, fx=f, fy=f, interpolation=cv2.INTER_LINEAR), f

    img = cv2.imread('621669_7600000020025.jpeg')
    SCALE=900 # 600
    MAX_SCALE=1500 # 1200
    TEXT_PROPOSALS_WIDTH=0 # 16
    MIN_NUM_PROPOSALS=0 # 2
    MIN_RATIO=0.01 # 0.5
    LINE_MIN_SCORE=0.6 # 0.9
    MAX_HORIZONTAL_GAP=30 # 50
    TEXT_PROPOSALS_MIN_SCORE=0.7 # 0.7
    TEXT_PROPOSALS_NMS_THRESH=0.3 # 0.2
    TEXT_LINE_NMS_THRESH=0.3
    MIN_V_OVERLAPS=0.6 # 0.7
    MIN_SIZE_SIM=0.6 # 0.7

    img, scale = resize_im(img, scale=SCALE,
    max_scale=MAX_SCALE)
    plt.imshow(img_temp)
    plt.show()

Pillow

  • 翻转

    1
    2
    3
    4
    5
    from PIL import Image,ImageEnhance,ImageFilter,ImageOps

    img = Image.open(x)
    img_flip_left_right = img.transpose(Image.FLIP_LEFT_RIGHT)
    img_flip_top_bottom = img.transpose(Image.FLIP_TOP_BOTTOM)
  • 旋转

    1
    2
    3
    4
    5
    6
    7
    8
    from PIL import Image,ImageEnhance,ImageFilter,ImageOps

    img = Image.open(x)
    img_rotate_90 = img.transpose(Image.ROTATE_90)
    img_rotate_180 = img.transpose(Image.ROTATE_180)
    img_rotate_270 = img.transpose(Image.ROTATE_270)
    img_rotate_90_left = img_flip_left_right.transpose(Image.ROTATE_90)
    img_rotate_270_left = img_flip_left_right.transpose(Image.ROTATE_270)
  • 3.亮度

    1
    2
    3
    4
    5
    6
    from PIL import Image,ImageEnhance,ImageFilter,ImageOps

    img = Image.open(x)
    enh_bri = ImageEnhance.Brightness(img)
    brightness = 1.5
    image_brightened = enh_bri.enhance(brightness)
  • 色彩

    1
    2
    3
    4
    5
    6
    from PIL import Image,ImageEnhance,ImageFilter,ImageOps

    img = Image.open(x)
    enh_col = ImageEnhance.Color(img)
    color = 1.5
    image_colored = enh_col.enhance(color)
  • 对比度

    1
    2
    3
    4
    5
    6
    from PIL import Image,ImageEnhance,ImageFilter,ImageOps

    img = Image.open(x)
    enh_con = ImageEnhance.Contrast(img)
    contrast = 1.5
    image_contrasted = enh_con.enhance(contrast)
  • 锐度

    1
    2
    3
    4
    5
    6
    from PIL import Image,ImageEnhance,ImageFilter,ImageOps

    img = Image.open(x)
    enh_sha = ImageEnhance.Sharpness(img)
    sharpness = 3.0
    image_sharped = enh_sha.enhance(sharpness)

Skimage

  • 加高斯噪声

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    from skimage.util import random_noise
    import cv2
    # 加高斯噪声
    def addNoise(img):
    '''
    注意:输出的像素是[0,1]之间,所以乘以5得到[0,255]之间
    '''
    return random_noise(img, mode='gaussian', seed=13, clip=True)*255

    cv_image = cv2.imread(x)
    # 高斯噪声
    gau_image = addNoise(cv_image)
  • 随机改变亮度

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    import cv2
    import random
    from skimage import exposure

    def changeLight(img):
    rate = random.uniform(0.5, 1.5)
    # print(rate)
    img = exposure.adjust_gamma(img, rate) #大于1为调暗,小于1为调亮;1.05
    return img

    cv_image = cv2.imread(x)
    # 随机改变
    light = changeLight(cv_image)

Other Skill

  • 搜寻图片名字

    1
    2
    3
    4
    5
    6
    re.search和re.match区别:match只匹配开头
    - match
    #re.match('row/0/', 'row/0/_001a_0.png').span()
    #(0, 6)
    - search
    #shutil.copyfile(it, tar_pre_path + it[re.search(ori_pre_path, it).span()[1]:])
  • shutil复制图片

    1
    2
    3
    4
    5
    ori_pre_path = './0_aug/'
    tar_pre_path = './images/'

    for it in glob.glob(ori_pre_path + '*.jpg'):
    shutil.copyfile(it, tar_pre_path + it[re.search(ori_pre_path, it).span()[1]:])
  • 图像拼接&裁剪

    1
    2
    3
    4
    5
    6

    for it in test_list[1:4]:
    img_res = np.hstack([img_res, it])


    img_cut = test_list[4][:, :test_list[4].shape[1] // 4 * 3, :]
  • Keras—data_generater的写法

    【Github】刘扬农作物病虫害检测

TODO

  • pydicom (医学,dcm)
  • SimpleITK (医学,dcm)