You can add a padding variable to control how much extra space is added around each detected face to ensure the entire face is captured. You can adjust the padding value as you need. The expanded ROI (region of interest) is calculated based on the detected face's position and size, and it takes into account the image boundaries to prevent going out of bounds -
if (facesDetectionEnabled)
{
Mat grayImage = new Mat();
CvInvoke.CvtColor(currentFrame, grayImage, ColorConversion.Bgr2Gray);
CvInvoke.EqualizeHist(grayImage, grayImage);
System.Drawing.Rectangle[] faces = faceCasacdeClassifier.DetectMultiScale(grayImage, 1.1, 3);
if (faces.Length > 0)
{
foreach (var face in faces)
{
CvInvoke.Rectangle(currentFrame, face, new Bgr(System.Drawing.Color.Red).MCvScalar, 2);
int padding = 10;
Rectangle expandedFace = new Rectangle(
Math.Max(face.Left - padding, 0),
Math.Max(face.Top - padding, 0),
Math.Min(face.Width + 2 * padding, currentFrame.Width - face.Left),
Math.Min(face.Height + 2 * padding, currentFrame.Height - face.Top));
Image<Bgr, byte> resultImage = currentFrame.Convert<Bgr, byte>();
resultImage.ROI = expandedFace;
picDetected.SizeMode = PictureBoxSizeMode.StretchImage;
picDetected.Image = resultImage.AsBitmap();
}
}
}