OpenCV系列笔记十二:Using colors for detection - skin tone detection

Overview

关于颜色空间,除了经常用的RGB和之前用于颜色比较的Lab,还有其他很多不同用途的种类。这里我们要检测皮肤,用到的颜色空间是HSV. 原因如下:

In general, to detect an object using color, you first need to collect a large database of image samples that contain the object captured from different viewing conditions. These will be used to define the parameters of your classifier. You also need to select the color representation that you will use for classification. For skin tone detection, many studies have shown that skin color from the diverse ethnical groups clusters well in the hue/saturation space. For this reason, we will simply use the hue and saturation values to identify the skin tones in the following image.

显然,在下面的程序里出现的一些阀值也是来自于一些经验数据。

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include "imageInfo.h"

void detectHScolor(const cv::Mat& image,
double minHue, double maxHue,
double minSat, double maxSat,
cv::Mat& mask){
cv::Mat hsv;
cv::cvtColor(image, hsv, CV_BGR2HSV);
std::vector<cv::Mat>channels;
cv::split(hsv, channels);

// Hue masking
cv::Mat mask1;
cv::threshold(channels[0], mask1, maxHue, 255,cv::THRESH_BINARY_INV); // 0~maxhue
cv::Mat mask2;
cv::threshold(channels[0], mask2, minHue, 255, cv::THRESH_BINARY); // minhue~180

cv::Mat hueMask;
if(minHue<maxHue)
hueMask = mask1 & mask2; // minhue~maxhue
else // if interval crossed the zero-degrees axis
hueMask = mask1 | mask2; // 上面的注释是书上的,一时搞不懂这个else是什么意思...知道的朋友可留言告知...

// Saturation masking
// between minSat and maxSat
cv::Mat satMask;
cv::inRange(channels[1], minSat, maxSat, satMask);

// combined mask
mask = hueMask & satMask;
}




int main(){
cv::Mat image = cv::imread("/home/shensir/Documents/MyPrograming/Cpp/Clions/data/girl.png");
cv::imshow("Original Image", image);
//detect skin tone
cv::Mat mask;
detectHScolor(image, 160,10,25,166, mask);
cv::Mat detected(image.size(),CV_8UC3,cv::Scalar(0,0,0));
image.copyTo(detected,mask);

cv::imshow("Detected", detected);
cv::waitKey(0);
return 0;
}


输出:

本文标题:OpenCV系列笔记十二:Using colors for detection - skin tone detection

文章作者:不秩稚童

发布时间:2017年05月02日 - 12:02:23

最后更新:2017年05月02日 - 12:16:17

原始链接:http://datahonor.com/2017/05/02/Using-colors-for-detection-skin-tone-detection/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

击蒙御寇