public void TestHOG1() { if (CudaInvoke.HasCuda) { using (CudaHOGDescriptor hog = new CudaHOGDescriptor()) using (Image <Bgr, Byte> image = new Image <Bgr, byte>("pedestrian.png")) { float[] pedestrianDescriptor = CudaHOGDescriptor.GetDefaultPeopleDetector(); hog.SetSVMDetector(pedestrianDescriptor); Stopwatch watch = Stopwatch.StartNew(); Rectangle[] rects; using (CudaImage <Bgr, Byte> CudaImage = new CudaImage <Bgr, byte>(image)) using (CudaImage <Bgra, Byte> gpuBgra = CudaImage.Convert <Bgra, Byte>()) 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)); } } }
/// <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 (CudaHOGDescriptor des = new CudaHOGDescriptor()) { des.SetSVMDetector(CudaHOGDescriptor.GetDefaultPeopleDetector()); watch = Stopwatch.StartNew(); using (GpuMat cudaBgr = new GpuMat(image)) using (GpuMat cudaBgra = new GpuMat()) { CudaInvoke.CvtColor(cudaBgr, cudaBgra, ColorConversion.Bgr2Bgra); regions = des.DetectMultiScale(cudaBgra); } } } 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); }
/// <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, out long processingTime) { Stopwatch watch; Rectangle[] regions; #if !IOS //check if there is a compatible Cuda device to run pedestrian detection if (CudaInvoke.HasCuda) { //this is the Cuda version using (CudaHOGDescriptor des = new CudaHOGDescriptor()) { des.SetSVMDetector(CudaHOGDescriptor.GetDefaultPeopleDetector()); watch = Stopwatch.StartNew(); using (GpuMat cudaBgr = new GpuMat(image)) using (GpuMat cudaBgra = new GpuMat()) { CudaInvoke.CvtColor(cudaBgr, cudaBgra, ColorConversion.Bgr2Bgra); regions = des.DetectMultiScale(cudaBgra); } } } 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); }
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)); } } }