繁体   English   中英

OpenCV中的肤色检测

[英]Skin Color Detection in 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 <  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通道遮罩:

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

您需要做的就是将其与原始图像结合起来,以遮盖所有非皮肤颜色:

(不,不要在此处编写[容易出错的]循环,最好依靠内置功能!)

Mat draw = frame & temp; // short for bitwise_and()

imshow("skin",draw);
waitKey();

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

cv::cvtColor(inputFrame, hsv, CV_BGR2HSV);
cv::inRange(hsv, hsv_min, hsv_max, mask);

cv::Mat outputFrame;
inputFrame.copyTo(outputFrame, mask);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM