Search code examples
c#emgucvface-detection

Finding the position of a face using emgucv (c#)


I have got my code to be able to detect a face and then put a box around where it finds a face but I am looking to get the position of the box that it puts. Any position would be great to get as I can adjust other code for a particular position.

How may I be able to get the position from this?

Many Thanks

 private void Device_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();
            Image<Bgr, byte> grayImage = new Image<Bgr, byte>(bitmap);
            Rectangle[] rectangles = cascadeClassifier1.DetectMultiScale(grayImage, 1.2, 1);
            foreach (Rectangle rectangle in rectangles)
            {
                using (Graphics graphics = Graphics.FromImage(bitmap))
                {
                    using (Pen pen = new Pen(Color.Red, 1))
                    {
                        graphics.DrawRectangle(pen, rectangle);
                    }
                }
            }
            plc.Image = bitmap;
        }

Solution

  • You can get the top left x and y coordinate of the rectangle, and the width and height.

    //Get the top left cord
    int rectX = rectangle.X;
    int rectY = rectangle.Y;
    
    //Get the width and height of the rectangle
    int rectWidth = rectangle.Width;
    int rectHeight = rectangle.Height;
    

    Using those above values, you can find the cords of the other three points of the rectangle.

    //The top right cord
    int topRightX = rectX + rectWidth;
    int topRightY = rectY;
    
    //The bottom left cord
    int bottomX = rectX;
    int bottomY = rectY + rectHeight;
    
    //The bottom right cord
    int bottomRightX = rectX + rectWidth;
    int bottomRightY = rectY + rectHeight;