2014-02-16 124 views
2

我試圖改進相機捕捉中的人臉檢測,所以我認爲如果在人臉檢測過程之前我從圖像中刪除了背景, 我使用的是BackgroundSubtractorMOGCascadeClassifierlbpcascade_frontalface面部檢測,使用openCv進行背景減法後的人臉檢測

我的問題是:我如何抓住前景圖像,以便使用它作爲面部檢測的輸入?這是我到目前爲止有:

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); 
} 

感謝

回答

0

如果輸入圖像和前景蒙,這是直線前進。在C++中,我只需簡單地添加(就在你發表評論的地方):image.copyTo(fgimage,fgMaskMOG);

我對java接口不熟悉,但是這應該是非常相似的。只是不要忘了正確初始化fgimage並重新設置每一幀。

+0

優秀,昨晚我意識到我可以使用copyTo ...我也加入侵蝕,以避免噪音,謝謝 –

0

我可以在C++回答使用BackgroundSubtractorMOG2

您可以使用侵蝕或更高的閾值值傳遞給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); 
} 

這樣,就可以減少搜索區域的級聯分類,這不僅使它更快,但也降低了假陽性的面孔。