|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import numpy |
|
|
|
|
|
class SkinColorFilter(): |
|
""" |
|
This class implements a number of functions to perform skin color filtering. |
|
It is based on the work published in "Adaptive skin segmentation via feature-based face detection", |
|
M.J. Taylor and T. Morris, Proc SPIE Photonics Europe, 2014 [taylor-spie-2014]_ |
|
Attributes |
|
---------- |
|
mean: numpy.ndarray | dim: 2 |
|
the mean skin color |
|
covariance: numpy.ndarray | dim: 2x2 |
|
the covariance matrix of the skin color |
|
covariance_inverse: numpy.ndarray | dim: 2x2 |
|
the inverse covariance matrix of the skin color |
|
circular_mask: numpy.ndarray |
|
mask of the size of the image, defining a circular region in the center |
|
luma_mask: numpy.ndarray |
|
mask of the size of the image, defining valid luma values |
|
""" |
|
|
|
def __init__(self): |
|
self.mean = numpy.array([0.0, 0.0]) |
|
self.covariance = numpy.zeros((2, 2), 'float64') |
|
self.covariance_inverse = numpy.zeros((2, 2), 'float64') |
|
|
|
def __generate_circular_mask(self, image, radius_ratio=0.4): |
|
|
|
w = image.shape[0] |
|
h = image.shape[1] |
|
radius = radius_ratio * h |
|
|
|
x_center = h / 2 |
|
y_center = w / 2 |
|
|
|
center = [int(x_center), int(y_center)] |
|
|
|
Y, X = numpy.ogrid[:w, :h] |
|
dist_from_center = numpy.sqrt((X - center[0])**2 + (Y-center[1])**2) |
|
|
|
mask = dist_from_center <= radius |
|
|
|
self.circular_mask = mask |
|
|
|
'''def __generate_circular_mask(self, image, radius_ratio=0.4): |
|
""" |
|
This function will generate a circular mask to be applied to the image. |
|
The mask will be true for the pixels contained in a circle centered in the image center, and with radius equals to radius_ratio * the image's height. |
|
Parameters |
|
---------- |
|
image: numpy.ndarray |
|
The face image. |
|
radius_ratio: float: |
|
The ratio of the image's height to define the radius of the circular region. Defaults to 0.4. |
|
""" |
|
w = image.shape[0] |
|
h = image.shape[1] |
|
|
|
x_center = w / 2 |
|
y_center = h / 2 |
|
|
|
# arrays with the image coordinates |
|
X = numpy.zeros((w, h)) |
|
|
|
import code |
|
code.interact(local=locals()) |
|
|
|
X[:] = range(0, w) |
|
|
|
Y = numpy.zeros((h, w)) |
|
Y[:] = range(0, h) |
|
Y = numpy.transpose(Y) |
|
|
|
# translate s.t. the center is the origin |
|
X -= x_center |
|
Y -= y_center |
|
|
|
# condition to be inside of a circle: x^2 + y^2 < r^2 |
|
radius = radius_ratio * h |
|
|
|
# x ^ 2 + y ^ 2 < r ^ 2 |
|
cm = (X ** 2 + Y ** 2) < (radius ** 2) # dim : w x h |
|
self.circular_mask = cm''' |
|
|
|
def __remove_luma(self, image): |
|
""" |
|
This function remove pixels with extreme luma values. |
|
Some pixels are considered as non-skin if their intensity is either too high or too low. |
|
The luma value for all pixels inside a provided circular mask is calculated. Pixels for which the luma value deviates more than 1.5 * standard deviation are pruned. |
|
Parameters |
|
---------- |
|
image: numpy.ndarray |
|
The face image. |
|
""" |
|
|
|
|
|
R = 0.299 * image[self.circular_mask, 0] |
|
G = 0.587 * image[self.circular_mask, 1] |
|
B = 0.114 * image[self.circular_mask, 2] |
|
|
|
luma = R + G + B |
|
|
|
m = numpy.mean(luma) |
|
s = numpy.std(luma) |
|
|
|
|
|
R = 0.299 * image[:, :, 0] |
|
G = 0.587 * image[:, :, 1] |
|
B = 0.114 * image[:, :, 2] |
|
|
|
luma = R + G + B |
|
|
|
|
|
lm = numpy.logical_and((luma > (m - 1.5 * s)), (luma < (m + 1.5 * s))) |
|
self.luma_mask = lm |
|
|
|
def __RG_Mask(self, image, dtype=None): |
|
|
|
channel_sum = image[:, :, 0].astype('float64') + image[:, :, 1] + image[:, :, 2] |
|
|
|
|
|
nonzero_mask = numpy.logical_or(numpy.logical_or(image[:, :, 0] > 0, image[:, :, 1] > 0), image[:, :, 2] > 0) |
|
|
|
|
|
R = numpy.zeros((image.shape[0], image.shape[1]), dtype) |
|
R[nonzero_mask] = image[nonzero_mask, 0] / channel_sum[nonzero_mask] |
|
|
|
|
|
G = numpy.zeros((image.shape[0], image.shape[1]), dtype) |
|
G[nonzero_mask] = image[nonzero_mask, 1] / channel_sum[nonzero_mask] |
|
|
|
return R, G |
|
|
|
def estimate_gaussian_parameters(self, image): |
|
""" |
|
This function estimates the parameter of the skin color distribution. |
|
The mean and covariance matrix of the skin pixels in the normalised rg colorspace are computed. |
|
Note that only the pixels for which both the circular and the luma mask is 'True' are considered. |
|
Parameters |
|
---------- |
|
image: numpy.ndarray |
|
The face image. |
|
""" |
|
self.__generate_circular_mask(image) |
|
self.__remove_luma(image) |
|
|
|
|
|
mask = numpy.logical_and(self.luma_mask, self.circular_mask) |
|
|
|
|
|
|
|
|
|
R, G = self.__RG_Mask(image) |
|
|
|
|
|
self.mean = numpy.array([numpy.mean(R[mask]), numpy.mean(G[mask])]) |
|
|
|
|
|
R_minus_mean = R[mask] - self.mean[0] |
|
G_minus_mean = G[mask] - self.mean[1] |
|
|
|
samples = numpy.vstack((R_minus_mean, G_minus_mean)) |
|
samples = samples.T |
|
|
|
cov = sum([numpy.outer(s, s) for s in samples]) |
|
|
|
self.covariance = cov / float(samples.shape[0] - 1) |
|
|
|
|
|
if numpy.linalg.det(self.covariance) != 0: |
|
self.covariance_inverse = numpy.linalg.inv(self.covariance) |
|
else: |
|
self.covariance_inverse = numpy.zeros_like(self.covariance) |
|
|
|
def get_skin_mask(self, image, threshold=0.5): |
|
""" |
|
This function computes the probability of skin-color for each pixel in the image. |
|
Parameters |
|
---------- |
|
image: numpy.ndarray |
|
The face image. |
|
threshold: float: 0->1 |
|
The threshold on the skin color probability. Defaults to 0.5 |
|
Returns |
|
------- |
|
skin_mask: numpy.ndarray |
|
The mask where skin color pixels are labeled as True. |
|
""" |
|
|
|
|
|
R, G = self.__RG_Mask(image, 'float64') |
|
|
|
|
|
R_minus_mean = R - self.mean[0] |
|
G_minus_mean = G - self.mean[1] |
|
|
|
n = R.shape[0] * R.shape[1] |
|
V = numpy.dstack((R_minus_mean, G_minus_mean)) |
|
V = V.reshape((n, 2)) |
|
|
|
probs = [numpy.dot(k, numpy.dot(self.covariance_inverse, k)) for k in V] |
|
probs = numpy.array(probs).reshape(R.shape) |
|
|
|
skin_map = numpy.exp(-0.5 * probs) |
|
|
|
return skin_map > threshold |