/// <summary> /// Find the pedestrian in the image /// </summary> /// <param name="image">The image</param> /// <returns>The region where pedestrians are detected</returns> public Rectangle[] Find(IInputArray image) { Rectangle[] regions; using (InputArray iaImage = image.GetInputArray()) { //if the input array is a GpuMat //check if there is a compatible Cuda device to run pedestrian detection if (iaImage.Kind == InputArray.Type.CudaGpuMat && _hogCuda != null) { //this is the Cuda version using (GpuMat cudaBgra = new GpuMat()) using (VectorOfRect vr = new VectorOfRect()) { CudaInvoke.CvtColor(image, cudaBgra, ColorConversion.Bgr2Bgra); _hogCuda.DetectMultiScale(cudaBgra, vr); regions = vr.ToArray(); } } else { //this is the CPU/OpenCL version MCvObjectDetection[] results = _hog.DetectMultiScale(image); regions = new Rectangle[results.Length]; for (int i = 0; i < results.Length; i++) { regions[i] = results[i].Rect; } } return(regions); } }
public void TestCudaOrbDetector() { if (!CudaInvoke.HasCuda) { return; } using (Image <Bgr, Byte> img = new Image <Bgr, byte>("box.png")) using (GpuMat cudaImage = new GpuMat(img)) using (GpuMat grayCudaImage = new GpuMat()) using (CudaORBDetector detector = new CudaORBDetector(500)) using (VectorOfKeyPoint kpts = new VectorOfKeyPoint()) using (GpuMat keyPointMat = new GpuMat()) using (GpuMat descriptorMat = new GpuMat()) { CudaInvoke.CvtColor(cudaImage, grayCudaImage, ColorConversion.Bgr2Gray); detector.DetectAsync(grayCudaImage, keyPointMat); detector.Convert(keyPointMat, kpts); //detector.ComputeRaw(grayCudaImage, null, keyPointMat, descriptorMat); //detector.DownloadKeypoints(keyPointMat, kpts); foreach (MKeyPoint kpt in kpts.ToArray()) { img.Draw(new CircleF(kpt.Point, 3.0f), new Bgr(0, 255, 0), 1); } //ImageViewer.Show(img); } }
/// <summary> /// Find the pedestrian in the image /// </summary> /// <param name="image">The image</param> /// <param name="processingTime">The pedestrian detection time in milliseconds</param> /// <returns>The region where pedestrians are detected</returns> public static Rectangle[] Find(Mat image, bool tryUseCuda, bool tryUseOpenCL, out long processingTime) { Stopwatch watch; Rectangle[] regions; #if !(IOS || NETFX_CORE) //check if there is a compatible Cuda device to run pedestrian detection if (tryUseCuda && CudaInvoke.HasCuda) { //this is the Cuda version using (CudaHOG des = new 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 (GpuMat cudaBgr = new GpuMat(image)) using (GpuMat cudaBgra = new GpuMat()) using (VectorOfRect vr = new VectorOfRect()) { CudaInvoke.CvtColor(cudaBgr, cudaBgra, ColorConversion.Bgr2Bgra); des.DetectMultiScale(cudaBgra, vr); regions = vr.ToArray(); } } } else #endif { //Many opencl functions require opencl compatible gpu devices. //As of opencv 3.0-alpha, opencv will crash if opencl is enable and only opencv compatible cpu device is presented //So we need to call CvInvoke.HaveOpenCLCompatibleGpuDevice instead of CvInvoke.HaveOpenCL (which also returns true on a system that only have cpu opencl devices). CvInvoke.UseOpenCL = tryUseOpenCL && CvInvoke.HaveOpenCLCompatibleGpuDevice; //this is the CPU/OpenCL version using (HOGDescriptor des = new HOGDescriptor()) { des.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); //load the image to umat so it will automatically use opencl is available UMat umat = image.ToUMat(AccessType.Read); watch = Stopwatch.StartNew(); MCvObjectDetection[] results = des.DetectMultiScale(umat); 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); }
public Rectangle[] FindBodyHOG(Mat image, out double[] confidence) { // If can't use Cuda then go for Without cuda implementation if (!Global.canRunCuda) { confidence = null; return(FindBodyHOG_WithoutGpu(image)); } if (des == null) { InitalizeBodyTracker(); } List <Rectangle> regions = new List <Rectangle>(); VectorOfRect rects = new VectorOfRect(); VectorOfDouble confColl = new VectorOfDouble(); confidence = new double[0]; try { des.SetSVMDetector(des.GetDefaultPeopleDetector()); des.GroupThreshold = 1; des.HitThreshold = 0; des.NumLevels = 15; des.ScaleFactor = 1.05; using (GpuMat cudaBgr = new GpuMat(image)) using (GpuMat cudaBgra = new GpuMat()) { CudaInvoke.CvtColor(cudaBgr, cudaBgra, ColorConversion.Bgr2Bgra); //des.DetectMultiScale(cudaBgra, rects, confColl); des.DetectMultiScale(cudaBgra, rects, null); } //confidence = confColl.ToArray(); for (int i = 0; i < rects.ToArray().Length; i++) { //if (confidence[i] > 0.5) regions.Add(rects[i]); } } catch (Exception ex) { MessageBox.Show(ex.Message); } return(regions.ToArray());//rects.ToArray(); }
/// <summary> /// Find the pedestrian in the image /// </summary> /// <param name="image">The image</param> /// <param name="processingTime">The pedestrian detection time in milliseconds</param> /// <returns>The region where pedestrians are detected</returns> public static Rectangle[] Find(Mat image, bool tryUseCuda, out long processingTime) { Stopwatch watch; Rectangle[] regions; #if !(__IOS__ || NETFX_CORE) //check if there is a compatible Cuda device to run pedestrian detection if (tryUseCuda && CudaInvoke.HasCuda) { //this is the Cuda version using (CudaHOG des = new 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 (GpuMat cudaBgr = new GpuMat(image)) using (GpuMat cudaBgra = new GpuMat()) using (VectorOfRect vr = new VectorOfRect()) { CudaInvoke.CvtColor(cudaBgr, cudaBgra, ColorConversion.Bgr2Bgra); des.DetectMultiScale(cudaBgra, vr); regions = vr.ToArray(); } } } else #endif { //this is the CPU/OpenCL version using (HOGDescriptor des = new HOGDescriptor()) { des.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); //load the image to umat so it will automatically use opencl is available UMat umat = image.ToUMat(AccessType.Read); watch = Stopwatch.StartNew(); MCvObjectDetection[] results = des.DetectMultiScale(umat); 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); }
/// <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(IInputArray image, out long processingTime) { Stopwatch watch; Rectangle[] regions; using (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 == InputArray.Type.CudaGpuMat) { //this is the Cuda version using (CudaHOG des = new 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 (GpuMat cudaBgra = new GpuMat()) using (VectorOfRect vr = new VectorOfRect()) { CudaInvoke.CvtColor(image, cudaBgra, ColorConversion.Bgr2Bgra); des.DetectMultiScale(cudaBgra, vr); regions = vr.ToArray(); } } } else #endif { //this is the CPU/OpenCL version using (HOGDescriptor des = new HOGDescriptor()) { des.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); watch = Stopwatch.StartNew(); 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); } }
public List <IImage> ProcessFrame(IImage original) { Rectangle[] peopleRegion; using (InputArray iaImage = original.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 == InputArray.Type.CudaGpuMat) { //this is the Cuda version using (CudaHOG des = new CudaHOG(new Size(64, 128), new Size(16, 16), new Size(8, 8), new Size(8, 8))) { des.SetSVMDetector(des.GetDefaultPeopleDetector()); using (GpuMat cudaBgra = new GpuMat()) using (VectorOfRect vr = new VectorOfRect()) { CudaInvoke.CvtColor(original, cudaBgra, ColorConversion.Bgr2Bgra); des.DetectMultiScale(cudaBgra, vr); peopleRegion = vr.ToArray(); } } } else #endif { //this is the CPU/OpenCL version using (HOGDescriptor peopleDescriptor = new HOGDescriptor()) { peopleDescriptor.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); MCvObjectDetection[] peopleFound = peopleDescriptor .DetectMultiScale(original, 0, default(Size), default(Size), AdjustableParameters["Scale"].CurrentValue, AdjustableParameters["SimilarityThreshold"].CurrentValue, AdjustableParameters["MeanShiftGrouping"].CurrentValue == 1); peopleRegion = new Rectangle[peopleFound.Length]; for (int i = 0; i < peopleFound.Length; i++) { peopleRegion[i] = peopleFound[i].Rect; } } } IImage copy = CopyAndDraw(original, peopleRegion); return(new List <IImage> { copy }); } }
/// <summary> /// Find the pedestrian in the image /// </summary> /// <param name="image">The image</param> /// <returns>The region where pedestrians are detected</returns> public static Rectangle[] Find(IInputArray image, HOGDescriptor hog, CudaHOG hogCuda = null) { //Stopwatch watch; Rectangle[] regions; using (InputArray iaImage = image.GetInputArray()) { //if the input array is a GpuMat //check if there is a compatible Cuda device to run pedestrian detection if (iaImage.Kind == InputArray.Type.CudaGpuMat && hogCuda != null) { //this is the Cuda version //watch = Stopwatch.StartNew(); using (GpuMat cudaBgra = new GpuMat()) using (VectorOfRect vr = new VectorOfRect()) { CudaInvoke.CvtColor(image, cudaBgra, ColorConversion.Bgr2Bgra); hogCuda.DetectMultiScale(cudaBgra, vr); regions = vr.ToArray(); } } else { //this is the CPU/OpenCL version //watch = Stopwatch.StartNew(); MCvObjectDetection[] results = hog.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); } }
public void TestHOG2() { if (CudaInvoke.HasCuda) { using (CudaHOGDescriptor hog = new CudaHOGDescriptor( new Size(48, 96), //winSize new Size(16, 16), //blockSize new Size(8, 8), //blockStride new Size(8, 8), //cellSize 9, //nbins -1, //winSigma 0.2, //L2HysThreshold true, //gammaCorrection 64 //nLevels )) using (Image <Bgr, Byte> image = new Image <Bgr, byte>("pedestrian.png")) { float[] pedestrianDescriptor = CudaHOGDescriptor.GetPeopleDetector48x96(); hog.SetSVMDetector(pedestrianDescriptor); Stopwatch watch = Stopwatch.StartNew(); Rectangle[] rects; using (GpuMat cudaImage = new GpuMat(image)) using (GpuMat gpuBgra = new GpuMat()) { CudaInvoke.CvtColor(cudaImage, gpuBgra, ColorConversion.Bgr2Bgra); rects = hog.DetectMultiScale(gpuBgra); } watch.Stop(); //Assert.AreEqual(1, rects.Length); foreach (Rectangle rect in rects) { image.Draw(rect, new Bgr(Color.Red), 1); } Trace.WriteLine(String.Format("HOG detection time: {0} ms", watch.ElapsedMilliseconds)); //ImageViewer.Show(image, String.Format("Detection Time: {0}ms", watch.ElapsedMilliseconds)); } } }
public (List <Rectangle>, List <Rectangle>) Detect(IInputArray image) { var faces = new List <Rectangle>(); var eyes = new List <Rectangle>(); using (var ugray = new GpuMat()) { CudaInvoke.CvtColor(image, ugray, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray); //normalizes brightness and increases contrast of the image CudaInvoke.EqualizeHist(ugray, ugray); //Detect the faces from the gray scale image and store the locations as rectangle //The first dimensional is the channel //The second dimension is the index of the rectangle in the specific channel var facesDetected = GetRectangles(faceClassifier, ugray); faces.AddRange(facesDetected); foreach (var f in facesDetected) { //Get the region of interest on the faces using (var faceRegion = new GpuMat(ugray, new Range(f.Bottom, f.Top), new Range(f.Left, f.Right))) { var eyesDetected = GetRectangles(eyeClassifier, faceRegion); foreach (var e in eyesDetected) { var eyeRect = e; eyeRect.Offset(f.X, f.Y); eyes.Add(eyeRect); } } } } return(faces, eyes); }
public void TestCudaImageAsyncOps() { if (CudaInvoke.HasCuda) { int counter = 0; Stopwatch watch = Stopwatch.StartNew(); using (GpuMat img1 = new GpuMat(3000, 2000, DepthType.Cv8U, 3)) using (GpuMat img2 = new GpuMat(3000, 2000, DepthType.Cv8U, 3)) using (GpuMat img3 = new GpuMat()) using (Stream stream = new Stream()) using (GpuMat mat1 = new GpuMat()) { img1.ConvertTo(mat1, DepthType.Cv8U, 1, 0, stream); while (!stream.Completed) { if (counter <= int.MaxValue) { counter++; } } Trace.WriteLine(String.Format("Counter has been incremented {0} times", counter)); counter = 0; CudaInvoke.CvtColor(img2, img3, CvToolbox.GetColorCvtCode(typeof(Bgr), typeof(Gray)), 1, stream); while (!stream.Completed) { if (counter <= int.MaxValue) { counter++; } } Trace.WriteLine(String.Format("Counter has been incremented {0} times", counter)); } watch.Stop(); Trace.WriteLine(String.Format("Total time: {0} milliseconds", watch.ElapsedMilliseconds)); } }