【数据增强】——弹性变形(Elastic Distortion)

转载自:https://zhuanlan.zhihu.com/p/342274228
作者:ChenCver

可能应用场景

  • 线性场景的语义分割
  • 文字识别

随笔
弹性变形的第二个步骤,和现在语义分割中流行的可学习光流上采样SFnet,采用神经网络来学习随机变化的随机场。

1. 前言

弹性变形数据增强,本人在看Unet论文时第一次接触到,工作中,经常用到语义分割解决问题,发现在针对线分割的问题,经常都会出现中间断裂的情况。后期经过不断工程验证,发现弹性变形这种数据增强有助于解决该问题,弹性变形论文最早是由Patrice等人在2003年ICDAR上发表:《Best Practices for Convolutional Neural Networks Applied to Visual Document Analysis》,本文主要是对其中的弹性变形进行详细讲解。

2. 弹性变形

  • 仿射变换
  • 弹性变形

step1:对图像imageA进行仿射变换(三点法),得到imageB,关于仿射变换可以看: 马同学;

step2:对imageB图像中的每个像素点随机生成一个在x和y方向的位移,△x和△y。其位移范围在(-1, 1)之间,得到一个随机位移场(random displacement fields);

step3:用服从高斯分布的N(0, δ)对step2中生成的随机位移场进行卷积操作(和CNN中的卷积操作一样,说白了就是滤波操作)。我们知道δ越大,产生的图像越平滑。下图是论文中的不同δ值对随机位移场的影响,下图左上角为原图,右上角为δ较小的情况(可以发现,位移方向非常随机),左下角和右下角为较大的不同δ值。
在这里插入图片描述
step4:用一个控制因子α与随机位移场相乘,用以控制其变形强度;

step5:将随机位移场施加到原图上,具体是怎么施加的呢?首先,生成一个和imageB大小一样的meshgrid网格meshB,网格中的每个值就是像素的坐标,比如说meshgrid网格大小为512x512,则meshgrid中的值为(0, 0), (0, 1), …, (511, 0), (511, 511),然后将随机位移场和meshB网格相加,这就模拟了imageB中的每个像素点在经过随机位移场的作用后,被偏移的位置,meshB与随机位移场相加后的结果记做imageC。

step6:弹性变形最终输出的imageC中每个位置的灰度值大小,组成一副变形图像,现在imageC中每个像素点存储的是(x+△x, y+△y),如下图中的A’,那怎么转化成灰度值呢,依据论文,作者是根据imageB中的B位置的双线性插值灰度值作为A’点的像素灰度值大小,最终将imageC输出得到变形图像。

参考代码

# -*- coding:utf-8 -*-
import cv2
import numpy as np
from scipy.ndimage.filters import gaussian_filter
from scipy.ndimage.interpolation import map_coordinates


def elastic_transform(image, alpha, sigma,
                      alpha_affine, random_state=None):

    if random_state is None:
        random_state = np.random.RandomState(None)

    shape = image.shape
    shape_size = shape[:2]
    # Random affine
    center_square = np.float32(shape_size) // 2
    square_size = min(shape_size) // 3
    # pts1: 仿射变换前的点(3个点)
    pts1 = np.float32([center_square + square_size,
                       [center_square[0] + square_size,
                        center_square[1] - square_size],
                       center_square - square_size])
    # pts2: 仿射变换后的点
    pts2 = pts1 + random_state.uniform(-alpha_affine, alpha_affine,
                                       size=pts1.shape).astype(np.float32)
    # 仿射变换矩阵
    M = cv2.getAffineTransform(pts1, pts2)
    # 对image进行仿射变换.
    imageB = cv2.warpAffine(image, M, shape_size[::-1], borderMode=cv2.BORDER_REFLECT_101)

    # generate random displacement fields
    # random_state.rand(*shape)会产生一个和shape一样打的服从[0,1]均匀分布的矩阵
    # *2-1是为了将分布平移到[-1, 1]的区间, alpha是控制变形强度的变形因子
    dx = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma) * alpha
    dy = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma) * alpha
    # generate meshgrid
    x, y = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]))
    # x+dx,y+dy
    indices = np.reshape(y + dy, (-1, 1)), np.reshape(x + dx, (-1, 1))
    # bilinear interpolation
    imageC = map_coordinates(imageB, indices, order=1, mode='constant').reshape(shape)

    return imageC


if __name__ == '__main__':
    img_path = '/home/cxj/Desktop/img/8_5_5.png'
    imageA = cv2.imread(img_path)
    img_show = imageA.copy()
    imageA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
    # Apply elastic transform on image
    imageC = elastic_transform(imageA, imageA.shape[1] * 2,
                                   imageA.shape[1] * 0.08,
                                   imageA.shape[1] * 0.08)

    cv2.namedWindow("img_a", 0)
    cv2.imshow("img_a", img_show)
    cv2.namedWindow("img_c", 0)
    cv2.imshow("img_c", imageC)
    cv2.waitKey(0)

在这里插入图片描述

参考

  • 2
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值