Ask Your Question
1

Face detection bounding box

asked 2017-10-27 11:35:35 -0600

John M gravatar image

Hi

I was wondering how the size of the bounding box around a detected face is determined. Is it the smallest box that satisfies the detection? Sometimes the boxes cover the whole face, but sometimes not. I appreciate that the box has to be a square of a size between minSize and maxSize. How does the algorithm create the bounding box edges?

Thanks

edit retag flag offensive close merge delete

Comments

Are you using detectMultiScale?

eshirima gravatar imageeshirima ( 2017-10-27 11:48:43 -0600 )edit

@eshirima Yes I am. The rectangles are of different sizes, as expected I guess? But how are the sizes determined?

John M gravatar imageJohn M ( 2017-10-28 02:16:12 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
3

answered 2017-10-28 09:45:32 -0600

Before worrying about how the bounding boxes are calculated, I'd recommend you first read this, this, this and this which would help you understand what is actually happening behind the scenes with detectMultiScale.

After the read, you'll know that a sliding window approach is used and depending on the parameters, you will get multiple areas in your frame which resemble your template. For each such region, a bounding box is actually drawn around it. As the window slides over and discovers more similar features, a bounding box is drawn. By the end of the day, for a single face you might end up having a couple of boxes overlaying one another.

Having multiple boxes represent a single face is redundant so a non-max suppression is applied. In a nutshell, you try finding the average location of the overlapping boxes and encompass them together. Look into this blog to get the general idea. Here's a follow up link.

Depending on the parameters you used for detectMultiScale, the final averaged box's location will differ.

edit flag offensive delete link more

Comments

nice explanation. thanks

sturkmen gravatar imagesturkmen ( 2017-10-28 09:50:01 -0600 )edit
2

answered 2017-10-28 07:07:33 -0600

updated 2017-10-28 07:17:43 -0600

let me try to guide you finding your own answer by experiment.

you can try the code below online OpenCV.js Tutorials (just copy and paste it on the page)

let src = cv.imread('canvasInput');
let gray = new cv.Mat();
cv.cvtColor(src, gray, cv.COLOR_RGBA2GRAY, 0);
let faces = new cv.RectVector();
let eyes = new cv.RectVector();
let faceCascade = new cv.CascadeClassifier();
// load pre-trained classifiers
faceCascade.load('haarcascade_frontalface_default.xml');
// detect faces
let msize = new cv.Size(0, 0);

// try to change scaleFactor  and minNeighbors values
faceCascade.detectMultiScale(gray, faces,1.05,0);
for (let i = 0; i < faces.size(); ++i) {
    let roiGray = gray.roi(faces.get(i));
    let roiSrc = src.roi(faces.get(i));
    let point1 = new cv.Point(faces.get(i).x, faces.get(i).y);
    let point2 = new cv.Point(faces.get(i).x + faces.get(i).width,
                              faces.get(i).y + faces.get(i).height);
    cv.rectangle(src, point1, point2, [255, 0, 0, 255]);
    roiGray.delete(); roiSrc.delete();
}
cv.imshow('canvasOutput', src);
src.delete(); gray.delete(); faceCascade.delete();
faces.delete(); eyes.delete();

try to change faceCascade.detectMultiScale parameters like given examples below

faceCascade.detectMultiScale(gray, faces,1.05,0);
faceCascade.detectMultiScale(gray, faces,1.05,1);

faceCascade.detectMultiScale(gray, faces,2,0);
faceCascade.detectMultiScale(gray, faces,2,1);

faceCascade.detectMultiScale(gray, faces,3,0);
faceCascade.detectMultiScale(gray, faces,3,1);

faceCascade.detectMultiScale(gray, faces,4,0);
faceCascade.detectMultiScale(gray, faces,4,1);

i am sure you will find your own answer when you understand scaleFactor and minNeighbors parameters well

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-10-27 11:35:35 -0600

Seen: 2,979 times

Last updated: Oct 28 '17