jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include <limits.h> |
| 12 | #include <math.h> |
| 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "modules/video_processing/util/skin_detection.h" |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | |
| 18 | // Fixed-point skin color model parameters. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 19 | static const int skin_mean[5][2] = {{7463, 9614}, |
| 20 | {6400, 10240}, |
| 21 | {7040, 10240}, |
| 22 | {8320, 9280}, |
| 23 | {6800, 9614}}; |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame] | 24 | static const int skin_inv_cov[4] = {4107, 1663, 1663, 2157}; // q16 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 25 | static const int skin_threshold[6] = {1570636, 1400000, 800000, |
| 26 | 800000, 800000, 800000}; // q18 |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame] | 27 | |
| 28 | // Thresholds on luminance. |
jackychen | 3ad4bd3 | 2016-03-11 11:57:03 -0800 | [diff] [blame] | 29 | static const int y_low = 40; |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame] | 30 | static const int y_high = 220; |
| 31 | |
| 32 | // Evaluates the Mahalanobis distance measure for the input CbCr values. |
jackychen | 3ad4bd3 | 2016-03-11 11:57:03 -0800 | [diff] [blame] | 33 | static int EvaluateSkinColorDifference(int cb, int cr, int idx) { |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame] | 34 | const int cb_q6 = cb << 6; |
| 35 | const int cr_q6 = cr << 6; |
jackychen | 3ad4bd3 | 2016-03-11 11:57:03 -0800 | [diff] [blame] | 36 | const int cb_diff_q12 = |
| 37 | (cb_q6 - skin_mean[idx][0]) * (cb_q6 - skin_mean[idx][0]); |
| 38 | const int cbcr_diff_q12 = |
| 39 | (cb_q6 - skin_mean[idx][0]) * (cr_q6 - skin_mean[idx][1]); |
| 40 | const int cr_diff_q12 = |
| 41 | (cr_q6 - skin_mean[idx][1]) * (cr_q6 - skin_mean[idx][1]); |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame] | 42 | const int cb_diff_q2 = (cb_diff_q12 + (1 << 9)) >> 10; |
| 43 | const int cbcr_diff_q2 = (cbcr_diff_q12 + (1 << 9)) >> 10; |
| 44 | const int cr_diff_q2 = (cr_diff_q12 + (1 << 9)) >> 10; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 45 | const int skin_diff = |
| 46 | skin_inv_cov[0] * cb_diff_q2 + skin_inv_cov[1] * cbcr_diff_q2 + |
| 47 | skin_inv_cov[2] * cbcr_diff_q2 + skin_inv_cov[3] * cr_diff_q2; |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame] | 48 | return skin_diff; |
| 49 | } |
| 50 | |
jackychen | 3ad4bd3 | 2016-03-11 11:57:03 -0800 | [diff] [blame] | 51 | static int SkinPixel(const uint8_t y, const uint8_t cb, const uint8_t cr) { |
| 52 | if (y < y_low || y > y_high) { |
| 53 | return 0; |
| 54 | } else { |
| 55 | if (MODEL_MODE == 0) { |
| 56 | return (EvaluateSkinColorDifference(cb, cr, 0) < skin_threshold[0]); |
| 57 | } else { |
| 58 | // Exit on grey. |
| 59 | if (cb == 128 && cr == 128) |
| 60 | return 0; |
| 61 | // Exit on very strong cb. |
| 62 | if (cb > 150 && cr < 110) |
| 63 | return 0; |
| 64 | // Exit on (another) low luminance threshold if either color is high. |
| 65 | if (y < 50 && (cb > 140 || cr > 140)) |
| 66 | return 0; |
| 67 | for (int i = 0; i < 5; i++) { |
| 68 | int diff = EvaluateSkinColorDifference(cb, cr, i); |
| 69 | if (diff < skin_threshold[i + 1]) { |
| 70 | return 1; |
| 71 | } else if (diff > (skin_threshold[i + 1] << 3)) { |
| 72 | // Exit if difference is much large than the threshold. |
| 73 | return 0; |
| 74 | } |
| 75 | } |
| 76 | return 0; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame] | 81 | bool MbHasSkinColor(const uint8_t* y_src, |
| 82 | const uint8_t* u_src, |
| 83 | const uint8_t* v_src, |
| 84 | const int stride_y, |
| 85 | const int stride_u, |
| 86 | const int stride_v, |
| 87 | const int mb_row, |
| 88 | const int mb_col) { |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 89 | const uint8_t* y = y_src + ((mb_row << 4) + 8) * stride_y + (mb_col << 4) + 8; |
| 90 | const uint8_t* u = u_src + ((mb_row << 3) + 4) * stride_u + (mb_col << 3) + 4; |
| 91 | const uint8_t* v = v_src + ((mb_row << 3) + 4) * stride_v + (mb_col << 3) + 4; |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame] | 92 | // Use 2x2 average of center pixel to compute skin area. |
mflodman | 99ab944 | 2015-12-07 22:54:50 -0800 | [diff] [blame] | 93 | uint8_t y_avg = (*y + *(y + 1) + *(y + stride_y) + *(y + stride_y + 1)) >> 2; |
| 94 | uint8_t u_avg = (*u + *(u + 1) + *(u + stride_u) + *(u + stride_u + 1)) >> 2; |
| 95 | uint8_t v_avg = (*v + *(v + 1) + *(v + stride_v) + *(v + stride_v + 1)) >> 2; |
jackychen | 3ad4bd3 | 2016-03-11 11:57:03 -0800 | [diff] [blame] | 96 | return SkinPixel(y_avg, u_avg, v_avg) == 1; |
jackychen | 8f9902a | 2015-11-26 02:59:48 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | } // namespace webrtc |