/// <summary> /// Find the pedestrian in the image /// </summary> /// <param name="image">The image</param> /// <param name="processingTime">The processing time in milliseconds</param> /// <returns>The region where pedestrians are detected</returns> public static Rectangle[] Find(newWorld::Emgu.CV.IInputArray image, out long processingTime) { Stopwatch watch; Rectangle[] regions; using (newWorld::Emgu.CV.InputArray iaImage = image.GetInputArray()) { #if !(__IOS__ || NETFX_CORE) //if the input array is a GpuMat //check if there is a compatible Cuda device to run pedestrian detection if (iaImage.Kind == newWorld::Emgu.CV.InputArray.Type.CudaGpuMat) { //this is the Cuda version using (newWorld::Emgu.CV.Cuda.CudaHOG des = new newWorld::Emgu.CV.Cuda.CudaHOG(new Size(64, 128), new Size(16, 16), new Size(8, 8), new Size(8, 8))) { des.SetSVMDetector(des.GetDefaultPeopleDetector()); watch = Stopwatch.StartNew(); using (newWorld::Emgu.CV.Cuda.GpuMat cudaBgra = new newWorld::Emgu.CV.Cuda.GpuMat()) using (newWorld::Emgu.CV.Util.VectorOfRect vr = new newWorld::Emgu.CV.Util.VectorOfRect()) { newWorld::Emgu.CV.Cuda.CudaInvoke.CvtColor(image, cudaBgra, newWorld::Emgu.CV.CvEnum.ColorConversion.Bgr2Bgra); des.DetectMultiScale(cudaBgra, vr); regions = vr.ToArray(); } } } else #endif { //this is the CPU/OpenCL version using (newWorld::Emgu.CV.HOGDescriptor des = new newWorld::Emgu.CV.HOGDescriptor()) { des.SetSVMDetector(newWorld::Emgu.CV.HOGDescriptor.GetDefaultPeopleDetector()); watch = Stopwatch.StartNew(); newWorld::Emgu.CV.Structure.MCvObjectDetection[] results = des.DetectMultiScale(image); regions = new Rectangle[results.Length]; for (int i = 0; i < results.Length; i++) { regions[i] = results[i].Rect; } watch.Stop(); } } processingTime = watch.ElapsedMilliseconds; return(regions); } }
void RecognizePeopleDefault() { listBox1.Items.Clear(); currentMainFrameBitmap.Save("Промежуточное изображение.png"); allContours.Clear(); gr = Graphics.FromImage(frameForDrawing); newWorld::Emgu.CV.Mat image = new newWorld::Emgu.CV.Mat("Промежуточное изображение.png"); long processingTime; Rectangle[] results; if (newWorld::Emgu.CV.Cuda.CudaInvoke.HasCuda) { using (newWorld::Emgu.CV.Cuda.GpuMat gpuMat = new newWorld::Emgu.CV.Cuda.GpuMat(image)) results = FindMen.Find(gpuMat, out processingTime); } else { using (newWorld::Emgu.CV.UMat uImage = image.GetUMat(newWorld::Emgu.CV.CvEnum.AccessType.ReadWrite)) results = FindMen.Find(uImage, out processingTime); } int i = 1; foreach (Rectangle rect in results) { allContours.Add(rect); listBox1.Items.Add("Спортсмен " + i.ToString()); i++; // newWorld::Emgu.CV.CvInvoke.Rectangle(image, rect, new newWorld::Emgu.CV.Structure.Bgr(Color.Red).MCvScalar); } int j = 1; foreach (Rectangle _contour in allContours) { gr.DrawRectangle(new Pen(Color.Red), _contour); // gr.DrawLines(new Pen(Color.Red), _contour.ToArray()); gr.DrawString(j.ToString(), new Font("Arial", 14), Brushes.AliceBlue, _contour.X, _contour.Y); j++; } pictureBox1.Image = frameForDrawing; // тут же подключается отрисовка nRecogLabel.Text = results.Length.ToString(); // ToBinContours(); }