/// <summary> /// Worker thread for image processing. /// </summary> public void CvMainThread() { var faceCascade = new CascadeClassifier(); var eyesCascade = new CascadeClassifier(); faceCascade.load("haarcascade_frontalface_alt.xml"); eyesCascade.load("haarcascade_eye_tree_eyeglasses.xml"); var srcFrame = new Mat(); var dstFrame = new Mat(); var imgProc = new ImgProc(); _videoIo.StartCapture(); while (true) { _videoIo.GetFrame(srcFrame); switch (_processingMethodIndex) { // passthrough case 0: break; // gray case 1: imgProc.cvtColor(srcFrame, dstFrame, ColorConversionCodes.COLOR_RGBA2GRAY); imgProc.cvtColor(dstFrame, srcFrame, ColorConversionCodes.COLOR_GRAY2RGB); break; // canny case 3: imgProc.cvtColor(srcFrame, dstFrame, cvRT.ColorConversionCodes.COLOR_RGBA2GRAY); imgProc.GaussianBlur(dstFrame, dstFrame, new cvRT.Size(7, 7), 1.5, 1.5); imgProc.Canny(dstFrame, dstFrame, 0, 30, 3); imgProc.cvtColor(dstFrame, srcFrame, ColorConversionCodes.COLOR_GRAY2RGB); break; // contour case 4: { var contours = new VectorOfVectorOfPoint(); var hierarchy = new VectorOfVec4i(); var color = new Scalar(255, 255, 255, 255); imgProc.Canny(srcFrame, dstFrame, 100, 100 * 2, 3); imgProc.FindContours(dstFrame, contours, hierarchy, ContourRetrievalAlgorithm.RETR_TREE, ContourApproximationModes.CHAIN_APPROX_SIMPLE, new Point(0, 0)); srcFrame.Set(new Scalar(0, 0, 0, 0)); for (var i = 0 ; i < contours.Count(); i++) { imgProc.DrawContours(srcFrame, contours, i, color, 2, 8, hierarchy, 0, new Point(0, 0)); } break; } // face detect case 5: { imgProc.cvtColor(srcFrame, dstFrame, ColorConversionCodes.COLOR_RGBA2GRAY); imgProc.EqualizeHist(dstFrame, dstFrame); // Faces in the frame. var faces = new List<Rect>(); try { faces = new List<Rect>(); faceCascade.detectMultiScale(dstFrame, faces, 1.1, 2, (int)(0 | CV_HAAR.SCALE_IMAGE), new cvRT.Size(30, 30)); } catch (Exception ex) { Debug.WriteLine("Exception {0}", ex.Message); } // For each face, detect the eyes foreach (var face in faces) { // Draw ellipse for the face. var faceCenter = new Point(face.X + face.Width / 2, face.Y + face.Height / 2); imgProc.Ellipse(srcFrame, faceCenter, new cvRT.Size(face.Width / 2, face.Height / 2), 0, 0, 360, new Scalar(255, 0, 255, 0), 4, 8, 0); // Detect the eyes for the face var faceRoi = dstFrame.RectOfInterest(face); var eyes = new List<Rect>(); eyesCascade.detectMultiScale(faceRoi, eyes, 1.1, 2, (int) (0 | CASCADE_FLAG.CASCADE_SCALE_IMAGE),new cvRT.Size(30, 30)); // Draw the eyes foreach (var eye in eyes) { var eyeCenter = new Point(face.X + eye.X + eye.Width/2, face.Y + eye.Y + eye.Height/2); var radius = (int) Math.Round((eye.Width + eye.Height) * 0.25); imgProc.Circle(srcFrame, eyeCenter, radius, new Scalar(255, 0, 0, 0), 4, 8, 0); } } break; } default: break; } _videoIo.ShowFrame(srcFrame); } }
/// <summary> /// Worker thread for image processing. /// </summary> public void CvMainThread() { var faceCascade = new CascadeClassifier(); var eyesCascade = new CascadeClassifier(); faceCascade.load("haarcascade_frontalface_alt.xml"); eyesCascade.load("haarcascade_eye_tree_eyeglasses.xml"); var srcFrame = new Mat(); var dstFrame = new Mat(); var imgProc = new ImgProc(); _videoIo.StartCapture(); while (true) { _videoIo.GetFrame(srcFrame); switch (_processingMethodIndex) { // passthrough case 0: break; // gray case 1: imgProc.cvtColor(srcFrame, dstFrame, ColorConversionCodes.COLOR_RGBA2GRAY); imgProc.cvtColor(dstFrame, srcFrame, ColorConversionCodes.COLOR_GRAY2RGB); break; // canny case 3: imgProc.cvtColor(srcFrame, dstFrame, cvRT.ColorConversionCodes.COLOR_RGBA2GRAY); imgProc.GaussianBlur(dstFrame, dstFrame, new cvRT.Size(7, 7), 1.5, 1.5); imgProc.Canny(dstFrame, dstFrame, 0, 30, 3); imgProc.cvtColor(dstFrame, srcFrame, ColorConversionCodes.COLOR_GRAY2RGB); break; // contour case 4: { var contours = new VectorOfVectorOfPoint(); var hierarchy = new VectorOfVec4i(); var color = new Scalar(255, 255, 255, 255); imgProc.Canny(srcFrame, dstFrame, 100, 100 * 2, 3); imgProc.FindContours(dstFrame, contours, hierarchy, ContourRetrievalAlgorithm.RETR_TREE, ContourApproximationModes.CHAIN_APPROX_SIMPLE, new Point(0, 0)); srcFrame.Set(new Scalar(0, 0, 0, 0)); for (var i = 0; i < contours.Count(); i++) { imgProc.DrawContours(srcFrame, contours, i, color, 2, 8, hierarchy, 0, new Point(0, 0)); } break; } // face detect case 5: { imgProc.cvtColor(srcFrame, dstFrame, ColorConversionCodes.COLOR_RGBA2GRAY); imgProc.EqualizeHist(dstFrame, dstFrame); // Faces in the frame. var faces = new List <Rect>(); try { faces = new List <Rect>(); faceCascade.detectMultiScale(dstFrame, faces, 1.1, 2, (int)(0 | CV_HAAR.SCALE_IMAGE), new cvRT.Size(30, 30)); } catch (Exception ex) { Debug.WriteLine("Exception {0}", ex.Message); } // For each face, detect the eyes foreach (var face in faces) { // Draw ellipse for the face. var faceCenter = new Point(face.X + face.Width / 2, face.Y + face.Height / 2); imgProc.Ellipse(srcFrame, faceCenter, new cvRT.Size(face.Width / 2, face.Height / 2), 0, 0, 360, new Scalar(255, 0, 255, 0), 4, 8, 0); // Detect the eyes for the face var faceRoi = dstFrame.RectOfInterest(face); var eyes = new List <Rect>(); eyesCascade.detectMultiScale(faceRoi, eyes, 1.1, 2, (int)(0 | CASCADE_FLAG.CASCADE_SCALE_IMAGE), new cvRT.Size(30, 30)); // Draw the eyes foreach (var eye in eyes) { var eyeCenter = new Point(face.X + eye.X + eye.Width / 2, face.Y + eye.Y + eye.Height / 2); var radius = (int)Math.Round((eye.Width + eye.Height) * 0.25); imgProc.Circle(srcFrame, eyeCenter, radius, new Scalar(255, 0, 0, 0), 4, 8, 0); } } break; } default: break; } _videoIo.ShowFrame(srcFrame); } }