模拟人脸光照,阴影,遮挡的数据增强

本文以人脸识别问题中的关键点检测问题为例,讲述不同的数据增强方式对结果的影响


提示:以下是本篇文章正文内容,下面案例可供参考

一、模拟强光照射到人脸上效果

示例:现实生活中,人脸收到光照的强度,对受到光照人脸的角度和人脸面部的起伏的影响。在本文中模拟了人收到电光源的干扰,如类似手电筒直接照到人脸区域。我们主要需要确定两个参数,一个是形成的光源分布的有效区域的半径,一个是受到光源照射的中心点坐标。然后通过计算在半径内点像素到中心像素的距离,通过对应的距离赋予不同权重,越靠近点光源中心,赋值强度最高。

def En(image):
    x, y,_ = image.shape  # 获取图片大小
    radius = np.random.randint(10, int(min(x, y)), 1)  #
    pos_x = np.random.randint(0, (min(x, y) - radius), 1)  # 获取人脸光照区域的中心点坐标
    pos_y = np.random.randint(0, (min(x, y) - radius), 1)  # 获取人脸光照区域的中心坐标
    pos_x = int(pos_x[0])
    pos_y = int(pos_y[0])
    radius = int(radius[0])
    strength = 100
    for j in range(pos_y - radius, pos_y + radius):
        for i in range(pos_x-radius, pos_x+radius):

            distance = math.pow((pos_x - i), 2) + math.pow((pos_y - j), 2)
            distance = np.sqrt(distance)
            if distance < radius:
                result = 1 - distance / radius
                result = result*strength
                # print(result)
                image[i, j, 0] = min((image[i, j, 0] + result),255)
                image[i, j, 1] = min((image[i, j, 1] + result),255)
                image[i, j, 2] = min((image[i, j, 2] + result),255)
    image       = image.astype(np.uint8)
    return image

二、模拟人脸的阴影干扰

显示中,当人脸区域出现阴影,说明人脸区域前方有物体遮挡了部分光源,使得被遮挡部分光源相对于其他部分显的更暗一些。而且正常情况下越垂直于和靠近于遮挡物的中心,人脸受到的阴影干扰越强。在本文中,我们将人脸光照散布的赋值方式转换成了减值的方式。
代码如下(示例):

def De(image):
    x, y,_ = image.shape  # 获取图片大小
    radius = np.random.randint(10, int(min(x, y)), 1)  #
    pos_x = np.random.randint(0, (min(x, y) - radius), 1)  # 获取人脸光照区域的中心点坐标
    pos_y = np.random.randint(0, (min(x, y) - radius), 1)  # 获取人脸光照区域的中心坐标
    pos_x = int(pos_x[0])
    pos_y = int(pos_y[0])
    radius = int(radius[0])
    strength = 100
    for j in range(pos_y - radius, pos_y + radius):
        for i in range(pos_x-radius, pos_x+radius):

            distance = math.pow((pos_x - i), 2) + math.pow((pos_y - j), 2)
            distance = np.sqrt(distance)
            if distance < radius:
                result = 1 - distance / radius
                result = result*strength
                # print(result)
                image[i, j, 0] = max((image[i, j, 0] - result),0)
                image[i, j, 1] = max((image[i, j, 1] - result),0)
                image[i, j, 2] = max((image[i, j, 2] - result),0)
    image       = image.astype(np.uint8)
    return image

三、模拟掩膜对人脸图像的遮挡效果

def Ma(image):
    x, y,_ = image.shape  # 获取图片大小
    mask_size = np.random.randint(10, 50, 1)
    pos_x = np.random.randint(10, (min(x, y) - 50), 1)  # 获取人脸光照区域的中心点坐标
    pos_y = np.random.randint(10, (min(x, y) - 50), 1)  # 获取人脸光照区域的中心坐标
    pos_x = int(pos_x[0])
    pos_y = int(pos_y[0])
    mask_size = int(mask_size[0])
    image[pos_x:pos_x + mask_size, pos_y:pos_y + mask_size] = 0
    return image

在这里插入图片描述

完整代码:

import cv2 as cv
import math
import numpy as np
from copy import deepcopy
#读取原始图像
def En(image):
    x, y,_ = image.shape  # 获取图片大小
    radius = np.random.randint(10, int(min(x, y)), 1)  #
    pos_x = np.random.randint(0, (min(x, y) - radius), 1)  # 获取人脸光照区域的中心点坐标
    pos_y = np.random.randint(0, (min(x, y) - radius), 1)  # 获取人脸光照区域的中心坐标
    pos_x = int(pos_x[0])
    pos_y = int(pos_y[0])
    radius = int(radius[0])
    strength = 100
    for j in range(pos_y - radius, pos_y + radius):
        for i in range(pos_x-radius, pos_x+radius):

            distance = math.pow((pos_x - i), 2) + math.pow((pos_y - j), 2)
            distance = np.sqrt(distance)
            if distance < radius:
                result = 1 - distance / radius
                result = result*strength
                # print(result)
                image[i, j, 0] = min((image[i, j, 0] + result),255)
                image[i, j, 1] = min((image[i, j, 1] + result),255)
                image[i, j, 2] = min((image[i, j, 2] + result),255)
    image       = image.astype(np.uint8)
    return image

def De(image):
    x, y,_ = image.shape  # 获取图片大小
    radius = np.random.randint(10, int(min(x, y)), 1)  #
    pos_x = np.random.randint(0, (min(x, y) - radius), 1)  # 获取人脸光照区域的中心点坐标
    pos_y = np.random.randint(0, (min(x, y) - radius), 1)  # 获取人脸光照区域的中心坐标
    pos_x = int(pos_x[0])
    pos_y = int(pos_y[0])
    radius = int(radius[0])
    strength = 100
    for j in range(pos_y - radius, pos_y + radius):
        for i in range(pos_x-radius, pos_x+radius):

            distance = math.pow((pos_x - i), 2) + math.pow((pos_y - j), 2)
            distance = np.sqrt(distance)
            if distance < radius:
                result = 1 - distance / radius
                result = result*strength
                # print(result)
                image[i, j, 0] = max((image[i, j, 0] - result),0)
                image[i, j, 1] = max((image[i, j, 1] - result),0)
                image[i, j, 2] = max((image[i, j, 2] - result),0)
    image       = image.astype(np.uint8)
    return image

def Ma(image):
    x, y,_ = image.shape  # 获取图片大小
    mask_size = np.random.randint(10, 50, 1)
    pos_x = np.random.randint(10, (min(x, y) - 50), 1)  # 获取人脸光照区域的中心点坐标
    pos_y = np.random.randint(10, (min(x, y) - 50), 1)  # 获取人脸光照区域的中心坐标
    pos_x = int(pos_x[0])
    pos_y = int(pos_y[0])
    mask_size = int(mask_size[0])
    image[pos_x:pos_x + mask_size, pos_y:pos_y + mask_size] = 0
    return image

img      = cv.imread('4.png')
img1     = deepcopy(img)
img1     = En(img1)
img2     = deepcopy(img)
img2     = De(img2)
img3     = deepcopy(img)
img3     = Ma(img3)

Light_En = np.hstack([img,img1,img2,img3])
cv.imshow('face_enhanced', Light_En)
cv.waitKey(0)
cv.destroyAllWindows()




结果

在每一张图像的增强过程中,我对每个增强操作的位置和增强的范围大小进行了随机化,这样使得每次增强后的结果不一样,达到数据增强的效果。喜欢就点个赞呗,best wishes!

  • 10
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值