我尝试在 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 < frame -> height; c++) {
uchar* ptr = (uchar*) ((frame->imageData) + (c * frame->widthStep));
uchar* ptr2 = (uchar*) ((temp->imageData) + (c * temp->widthStep));
for(d = 0; d < frame -> 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 肤色的图像。
任何解决方案,
谢谢
第一张原始图片 第二张黑白皮肤检测图像 第三输出(非实际)
最佳答案
你已经很接近了。
给定,你已经有了一个 3 channel 掩码:
Mat mask, temp;
cv::cvtColor(mask, temp, CV_GRAY2RGB);
您需要做的就是将它与您的原始图像结合起来,以掩盖所有非肤色:
(不,不要在那里写[容易出错的]循环,最好依赖内置功能!)
Mat draw = frame & temp; // short for bitwise_and()
imshow("skin",draw);
waitKey();
关于c - OpenCV 中的肤色检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22553715/