OpenCV中的肤色检测

我试图在Opencv中进行肤色检测。

1)首先我将图像从RGB转换为HSV

cvCvtColor(frame, hsv, CV_BGR2HSV); 

2)比我在HSV图像中应用了肤色阈值

 cvInRangeS(hsv, hsv_min, hsv_max, mask); // hsv_min & hsv_max are the value for skin detection 

3)因此它生成仅具有肤色但在黑白图像中的混搭,因此我将该图像转换为RGB

  cvCvtColor(mask, temp, CV_GRAY2RGB); 

4)所以现在我想要只有RGB值的肤色。

 for(c = 0; c  height; c++) { uchar* ptr = (uchar*) ((frame->imageData) + (c * frame->widthStep)); uchar* ptr2 = (uchar*) ((temp->imageData) + (c * temp->widthStep)); for(d = 0; d  width; d++) { if(ptr2[3*d+0] != 255 && ptr2[3*d+1] != 255 && ptr2[3*d+2] != 255 && ptr2[3*d+3] != 255 ){ ptr[3 * d + 0] = 0; ptr[3 * d + 1] = 0; ptr[3 * d + 2] = 0; ptr[3 * d + 3] = 0; } } } 

现在我没有得到我想要的图像,只有RGB的肤色。

任何方案,

谢谢

在此处输入图像描述

第1张原始图像第2张皮肤检测图像为黑白第3输出(非实际)

你已经很亲密了。

给定,你已经有一个3通道面具:

 Mat mask, temp; cv::cvtColor(mask, temp, CV_GRAY2RGB); 

你需要做的就是将它与原始图像结合起来,以掩盖所有非肤色:

(不,不要在那里写[易错]循环,更好地依赖内置function!)

 Mat draw = frame & temp; // short for bitwise_and() imshow("skin",draw); waitKey(); 

或者,无需将mask转换为RGB,您可以.copyTo()传递掩码参数

 cv::cvtColor(inputFrame, hsv, CV_BGR2HSV); cv::inRange(hsv, hsv_min, hsv_max, mask); cv::Mat outputFrame; inputFrame.copyTo(outputFrame, mask);