OpenCV:如何标准化脸部(去除阴影)?

mrg*_*oom 2 python opencv computer-vision

是否可以使用计算机视觉算法对脸部进行标准化(去除阴影)?

这是示例图像: 在此输入图像描述

这是结果cv2.equalizeHist在此输入图像描述

这是结果cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))在此输入图像描述

cv2.createCLAHE这是和 的clipLimit=2.0图像网格tileGridSize=[1, 2, 4, 8, 16, 32]在此输入图像描述

cv2.createCLAHE这是和 的clipLimit=[1, 2, 4, 8, 16, 32]图像网格tileGridSize=(8, 8)在此输入图像描述

这是伽玛校正的结果gamma = 0.6在此输入图像描述

这是用于伽玛校正的图像网格gamma = [0.2, 0.4, 0.6, 0.8, 1.0]在此输入图像描述

这是重现的代码:

def method_v1(img):
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    res = cv2.equalizeHist(img)

    img = np.hstack([img, res])

    return img


def method_v2(img):
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Less 'clipLimit' value less effect
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
    res = clahe.apply(img)

    img = np.hstack([img, res])

    return img


def method_v3(img):
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    gamma = 0.6
    res = np.power((img / 255.0), gamma) * 255
    res = np.clip(res, 0, 255).astype(np.uint8)

    img = np.hstack([img, res])

    return img


def create_clahe_grid_v1(img):
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    grid_size = [1, 2, 4, 8, 16, 32]

    res_list = []
    res_list.append(img)
    for sz in grid_size:
        clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(sz, sz))
        res = clahe.apply(img)
        res_list.append(res)

    img = np.hstack(res_list)

    return img


def create_clahe_grid_v2(img):
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    clip_limit = [1, 2, 4, 8, 16, 32]

    res_list = []
    res_list.append(img)
    for cl in clip_limit:
        clahe = cv2.createCLAHE(clipLimit=cl, tileGridSize=(8, 8))
        res = clahe.apply(img)
        res_list.append(res)

    img = np.hstack(res_list)

    return img


def create_gamma_correction_grid_v1(img):
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    res_list = []
    res_list.append(img)
    gamma_list = [0.2, 0.4, 0.6, 0.8, 1.0] # lighter
    #gamma_list = [1.2, 1.4, 1.6, 1.8, 2.0] # darker
    for gamma in gamma_list:
        res = np.power((img / 255.0), gamma) * 255
        res = np.clip(res, 0, 255).astype(np.uint8)
        res_list.append(res)

    img = np.hstack(res_list)

    return img
Run Code Online (Sandbox Code Playgroud)

到目前为止,伽玛校正看起来最好,但它显然无法消除阴影,因为它只是像素级非线性滤波器。还有其他值得尝试的计算机视觉算法吗?

fmw*_*w42 5

您可以考虑在 Python/OpenCV 中进行除法标准化

  • 读取输入
  • 转换为灰度
  • 高斯模糊
  • 分配
  • 保存结果

输入:

在此输入图像描述

import cv2
import numpy as np

# read the image
img = cv2.imread('face_shaded.png')

# convert to gray
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# blur
smooth = cv2.GaussianBlur(gray, (95,95), 0)

# divide gray by morphology image
division = cv2.divide(gray, smooth, scale=192)

# save results
cv2.imwrite('face_shaded_division.jpg',division)

# show results
cv2.imshow('smooth', smooth)  
cv2.imshow('division', division)  
cv2.waitKey(0)
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

根据整体亮度的需要调整比例值 (192)。

根据您的应用程序,您可能还需要减去平均值并除以除法归一化结果的标准差。