使用openCv进行背景扣除后的面部检测

时间:2014-02-16 15:31:47

标签: opencv javacv face-detection background-subtraction

我正在尝试通过相机捕捉来改善人脸检测,所以我认为如果在脸部检测过程之前我从图像中删除背景会更好 我正在使用BackgroundSubtractorMOGCascadeClassifierlbpcascade_frontalface进行面部检测,

我的问题是:如何抓取前景图像以将其用作面部检测的输入?这就是我到目前为止所做的:

while (true) {
    capture.retrieve(image);

    mog.apply(image, fgMaskMOG, training?LEARNING_RATE:0);

    if (counter++ > LEARNING_LIMIT) {
        training = false;
    }

    // I think something should be done HERE to 'apply' the foreground mask 
    // to the original image before passing it to the classifier..

    MatOfRect faces = new MatOfRect();
    classifier.detectMultiScale(image, faces);

    // draw faces rect
    for (Rect rect : faces.toArray()) {
        Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(255, 0, 0));
    }

    // show capture in JFrame
    frame.update(image);
    frameFg.update(fgMaskMOG);

    Thread.sleep(1000 / FPS);
}

由于

2 个答案:

答案 0 :(得分:1)

我可以使用BackgroundSubtractorMOG2

在C ++中回答

您可以使用侵蚀或将更高的阈值传递给MOG背景减法器以消除噪音。为了完全消除噪音和误报,您还可以模糊蒙版图像,然后应用阈值:

// Blur the mask image
blur(fgMaskMOG2, fgMaskMOG2, Size(5,5), Point(-1,-1));

// Remove the shadow parts and the noise
threshold(fgMaskMOG2, fgMaskMOG2, 128, 255, 0);

现在您可以轻松找到绑定前景区域的矩形并将此区域传递给级联分类器:

// Find the foreground bounding rectangle
Mat fgPoints;
findNonZero(fgMaskMOG2, fgPoints);
Rect fgBoundRect = boundingRect(fgPoints);

// Crop the foreground ROI
Mat fgROI = image(fgBoundRect);

// Detect the faces
vector<Rect> faces;
face_cascade.detectMultiScale(fgROI, faces, 1.3, 3, 0|CV_HAAR_SCALE_IMAGE, Size(32, 32));

// Display the face ROIs
for(size_t i = 0; i < faces.size(); ++i) 
{
    Point center(fgBoundRect.x + faces[i].x + faces[i].width*0.5, fgBoundRect.y + faces[i].y + faces[i].height*0.5);
    circle(image, center, faces[i].width*0.5, Scalar(255, 255, 0), 4, 8, 0);
} 

通过这种方式,您将减少级联分类器的搜索区域,这不仅使其更快,而且还减少了误报面。

答案 1 :(得分:0)

如果您有输入图像和前景蒙版,这很简单。 在C ++中,我只想添加(只是你评论的位置):image.copyTo(fgimage,fgMaskMOG);

我不熟悉java界面,但这应该非常相似。只是不要忘记正确初始化fgimage并在每一帧重置它。