Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello. I want to detect a face and draw line exactly around the face to crop it.
I searched a lot and used the EmguCV face detector. but now my code just draw a "rectangle" around the face and if I crop it, I will have a rectangle with a face inside it and it's not useful for me.

What I have tried:

this is the core of my simple code:
C#
CascadeClassifier _cascadeClassifier = new CascadeClassifier(Application.StartupPath + "/haarcascade_frontalface_alt_tree.xml");
using (var firstImage = Image.FromStream(saveImageStream))
{
    var bgrImage = new Image(new Bitmap(firstImage));
    Image grayFrame = bgrImage.Convert();
    var faces = _cascadeClassifier.DetectMultiScale(grayFrame, 1.01, 1, Size.Empty);
    foreach (var face in faces)
    {
        bgrImage.Draw(face, new Bgr(Color.BurlyWood), 3);
    }
    Image detectedImage = bgrImage.ToBitmap();
    detectedImage.Save("detectedImage .jpg");
}

the output of this code is a picture with "rectangle" around the face.
here you can see current result and expected result
But how can I detect face and "draw line around the face" like an oval that consist all elements of the face and not anything else?
Posted
Updated 14-Jan-19 13:38pm

1 solution

A face is detected as a rectangular area bounding the face. What you need is an edge detection algorithm, that detects the bounding areas from the edges and then you clear the internal pixels and get a mask. You can mix the face detection and edge detection algorithm to first find a bounding box, and then do an edge detection inside. This way, the mask that you get will be used to detect the region where the face is and you can then use that polygon to draw around the face.

A deep-learning solution would be a bit simpler one, I was using this technique in one of my own application a few weeks back, segmentation detection in images using deep learning.

It will detect the segments in the image, segments are basically the objects that appear to be connected. To increase the efficiency of the algorithm you can run this in the rectangle that you get.

How to do Semantic Segmentation using Deep learning[^]
 
Share this answer
 
v2
Comments
Maciej Los 15-Jan-19 2:13am    
5ed!
Afzaal Ahmad Zeeshan 15-Jan-19 4:21am    
Thank you, Maciej!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900