c - OpenCV 中的肤色检测

标签 c 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 肤色的图像。

任何解决方案,

谢谢

enter image description here

第一张原始图片 第二张黑白皮肤检测图像 第三输出(非实际)

最佳答案

你已经很接近了。

给定,你已经有了一个 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/

相关文章:

opencv - 为什么我们需要移动校准对象进行针孔相机校准?

c - 使用 KEY 的 Openssl/libcrypto AES 128 编码

c++ - 有什么东西我可以用 C 做,但不能用 C++ 做吗?

c - 将 kmalloc 内存映射到用户空间

python - 如何使用 opencv python 从汽车后视摄像头拍摄的视频中删除线条?

opencv - 应该如何找到头部相对于相机的角度?

c++ - C中的GDI不绘制连续的对象

使用链表创建堆栈

opencv - 如何从 haar 分类器检测到的图像中识别数字

c++ - 如何在 OpenCV 中保存检测到的对象的图像?