栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

文章目录
  • 一、模拟强光照射到人脸上效果
  • 二、模拟人脸的阴影干扰
  • 三、模拟掩膜对人脸图像的遮挡效果
  • 结果


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

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

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

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!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/655921.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号