Ask Your Question

Cropping a face and removing the background?

asked 2016-09-14 11:42:16 -0500

U-Scripter gravatar image

updated 2016-09-14 18:17:07 -0500

After some research I found that OpenCV is going to by my friend all my way through finishing this project. I want to crop a human's face from an image, remove the background and save the result. Since a picture is worth a thousand words, here's what I want to achieve:


Sorry for the cheap image editing. How can I accomplish this with OpenCV or without OpenCV? I'm going to use Arduino too if anyone has something to do with this :P.

edit retag flag offensive close merge delete


any updates on this?

shiggaz gravatar imageshiggaz ( 2017-02-18 22:33:28 -0500 )edit

Hey! Any updates on this? Have you got the output as shown??

Yuthinkpad gravatar imageYuthinkpad ( 2019-02-19 00:30:54 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2016-09-14 18:15:50 -0500

in my opinion you need some extra lib to find facial landmarks like dlib

(as you can see on the image below) dlib gives you the points you need to cut the down part of the face (chin and cheeks)

image description

afterwards you can try to find forehead and hairs by using cv::grabCut

following links will be useful if you want to try my proposed way...

edit flag offensive delete link more

answered 2016-09-14 14:16:46 -0500

Check out the Face detection module tutorial to detect the face. You should be able to directly apply it to extract the face (along with neck/torso and the surrounding background) as a Mat object.

Are you using a camera with a stationary scene with faces popping in and out? In that case, subtracting a detected face from the background is quite straightforward. You will first need to save template background image Mat background_img without any faces in the scene.

Once you detect a face as Mat roi , find and extract the corresponding sub-region in bgrnd_img to get roi_background_img. Now you can simply perform subtraction: roi - roi_background_img to get only the face (with the neck and parts of the torso).

Note: This will produce poor results if your illumination varies. In this case, you should threshold both roi and roi_background_img before subtraction.

Additionally, you can also check out this module for background subtraction.

Let me know if this helps!

edit flag offensive delete link more

Question Tools



Asked: 2016-09-14 11:42:16 -0500

Seen: 7,982 times

Last updated: Sep 14 '16