private void PreProcessImage(String tId) { // preprocess image to make it ready for FR // windows bitmap to MAT for processing cv::Mat imgProc = BitmapConverter.ToMat((Bitmap)imgOriginal.Image); // load classifiers String haarLocation = System.IO.Path.GetDirectoryName(Application.StartupPath) + "\\haar\\"; cv::CascadeClassifier faceC = new cv::CascadeClassifier(haarLocation + "haarcascade_frontalface_alt.xml"); cv::Rect[] faces; faces = faceC.DetectMultiScale(imgProc, 1.1, 2, cv::HaarDetectionType.DoCannyPruning); // find biggest detected face int faceIdx = 0; if (faces.Length >= 1) { // Largest Face Found long size = 0; long max = 0; for (int i = 0; i < faces.Length; i++) { size = faces[i].Height * faces[i].Width; if (size > max) { max = size; faceIdx = i; } } cv::Mat imgResize = imgProc.Clone(faces[faceIdx]); OpenCvSharp.Size newSize = new OpenCvSharp.Size(1280, 1280); imgResize.Resize(newSize); imgProcessed.Image = imgResize.ToBitmap(); imgProcessed.Update(); //imgOriginal.Refresh(); } else { Console.WriteLine(String.Format("No Face Found TomisID {0}", tId)); //MessageBox.Show("No Face Found", "Search Result", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
private void BtnWebCamStart_Click(object sender, EventArgs e) { bool done = false; this.Enabled = false; this.Refresh(); cv::VideoCapture capDev = new cv::VideoCapture(0); cv::Window vidPreview = new cv::Window("Video Preview"); cv::Mat imgRaw = new cv::Mat(); while (!done) { capDev.Read(imgRaw); // same as cvQueryFrame if (!imgRaw.Empty()) { vidPreview.ShowImage(imgRaw); switch (cv::Cv2.WaitKey(100)) { case 13: imgOriginal.Image = imgRaw.ToBitmap(); done = true; break; case 27: done = true; break; } } } vidPreview.Close(); vidPreview.Dispose(); imgRaw.Dispose(); capDev.Dispose(); this.Enabled = true; this.Refresh(); PreProcessImage("WebCam"); }