skin_detection.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2017 The WebM 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. #include "vpx_dsp/skin_detection.h"
  11. #define MODEL_MODE 1
  12. // Fixed-point skin color model parameters.
  13. static const int skin_mean[5][2] = { { 7463, 9614 },
  14. { 6400, 10240 },
  15. { 7040, 10240 },
  16. { 8320, 9280 },
  17. { 6800, 9614 } };
  18. static const int skin_inv_cov[4] = { 4107, 1663, 1663, 2157 }; // q16
  19. static const int skin_threshold[6] = { 1570636, 1400000, 800000,
  20. 800000, 800000, 800000 }; // q18
  21. // Thresholds on luminance.
  22. static const int y_low = 40;
  23. static const int y_high = 220;
  24. // Evaluates the Mahalanobis distance measure for the input CbCr values.
  25. static int vpx_evaluate_skin_color_difference(const int cb, const int cr,
  26. const int idx) {
  27. const int cb_q6 = cb << 6;
  28. const int cr_q6 = cr << 6;
  29. const int cb_diff_q12 =
  30. (cb_q6 - skin_mean[idx][0]) * (cb_q6 - skin_mean[idx][0]);
  31. const int cbcr_diff_q12 =
  32. (cb_q6 - skin_mean[idx][0]) * (cr_q6 - skin_mean[idx][1]);
  33. const int cr_diff_q12 =
  34. (cr_q6 - skin_mean[idx][1]) * (cr_q6 - skin_mean[idx][1]);
  35. const int cb_diff_q2 = (cb_diff_q12 + (1 << 9)) >> 10;
  36. const int cbcr_diff_q2 = (cbcr_diff_q12 + (1 << 9)) >> 10;
  37. const int cr_diff_q2 = (cr_diff_q12 + (1 << 9)) >> 10;
  38. const int skin_diff =
  39. skin_inv_cov[0] * cb_diff_q2 + skin_inv_cov[1] * cbcr_diff_q2 +
  40. skin_inv_cov[2] * cbcr_diff_q2 + skin_inv_cov[3] * cr_diff_q2;
  41. return skin_diff;
  42. }
  43. // Checks if the input yCbCr values corresponds to skin color.
  44. int vpx_skin_pixel(const int y, const int cb, const int cr, int motion) {
  45. if (y < y_low || y > y_high) {
  46. return 0;
  47. } else if (MODEL_MODE == 0) {
  48. return (vpx_evaluate_skin_color_difference(cb, cr, 0) < skin_threshold[0]);
  49. } else {
  50. int i = 0;
  51. // Exit on grey.
  52. if (cb == 128 && cr == 128) return 0;
  53. // Exit on very strong cb.
  54. if (cb > 150 && cr < 110) return 0;
  55. for (; i < 5; ++i) {
  56. int skin_color_diff = vpx_evaluate_skin_color_difference(cb, cr, i);
  57. if (skin_color_diff < skin_threshold[i + 1]) {
  58. if (y < 60 && skin_color_diff > 3 * (skin_threshold[i + 1] >> 2)) {
  59. return 0;
  60. } else if (motion == 0 &&
  61. skin_color_diff > (skin_threshold[i + 1] >> 1)) {
  62. return 0;
  63. } else {
  64. return 1;
  65. }
  66. }
  67. // Exit if difference is much large than the threshold.
  68. if (skin_color_diff > (skin_threshold[i + 1] << 3)) {
  69. return 0;
  70. }
  71. }
  72. return 0;
  73. }
  74. }