【问题标题】:Skin Color Detection in OpenCVOpenCV中的肤色检测
【发布时间】:2014-03-21 08:30:32
【问题描述】:

我尝试在 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 肤色的图像。

任何解决方案,

谢谢

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

【问题讨论】:

  • 请不要不要使用过时的c-api,而是使用c++的。
  • 未来会的。但是有什么解决方案吗?

标签: c opencv


【解决方案1】:

你已经很接近了。

假设,您已经有一个 3 通道掩码:

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

您需要做的就是将它与您的原始图像结合起来以屏蔽所有非肤色:

(不,不要在那里写[容易出错的]循环,最好依赖内置功能!)

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

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

【讨论】:

  • 我们可以直接在图像之间放置 & 吗??
  • 好吧,用 cv::Mat,是的。否则使用 bitwise_and()
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 2011-12-19
    • 2012-10-09
    • 2011-05-24
    • 2013-06-03
    • 2011-04-11
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    相关资源
    最近更新 更多