■ 보행자를 검출하는 방법을 보여준다. (Pedestrain Detection)
▶ PedestrianDetectionHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
using System; using System.Diagnostics; using Emgu.CV; using Emgu.CV.Structure; using System.Drawing; #if !IOS using Emgu.CV.GPU; #endif namespace TestProject { /// <summary> /// 보행자 검출 헬퍼 /// </summary> public static class PedestrianDetectionHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 찾기 - Find(image, processingTime) /// <summary> /// 찾기 /// </summary> /// <param name="image">이미지</param> /// <param name="processingTime">처리 시간</param> /// <returns>결과 사각형 배열</returns> public static Rectangle[] Find(Image<Bgr, Byte> image, out long processingTime) { Stopwatch stopwatch; Rectangle[] resultRectangleArray; stopwatch = Stopwatch.StartNew(); #if !IOS if(GpuInvoke.HasCuda) { using(GpuHOGDescriptor descriptor = new GpuHOGDescriptor()) { descriptor.SetSVMDetector(GpuHOGDescriptor.GetDefaultPeopleDetector()); using(GpuImage<Bgr, Byte> bgrImage = new GpuImage<Bgr, byte>(image)) { using(GpuImage<Bgra, Byte> bgraImage = bgrImage.Convert<Bgra, Byte>()) { resultRectangleArray = descriptor.DetectMultiScale(bgraImage); } } } } else #endif { using(HOGDescriptor descriptor = new HOGDescriptor()) { descriptor.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector()); resultRectangleArray = descriptor.DetectMultiScale(image); } } stopwatch.Stop(); processingTime = stopwatch.ElapsedMilliseconds; return resultRectangleArray; } #endregion } } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
using System; using System.Drawing; using System.Windows.Forms; using Emgu.CV; using Emgu.CV.Structure; using Emgu.CV.UI; using Emgu.CV.GPU; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> [STAThread] private static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); using(Image<Bgr, Byte> image = new Image<Bgr, byte>("sample.png")) { long processingTime; Rectangle[] resultRectangleArray = PedestrianDetectionHelper.Find(image, out processingTime); foreach(Rectangle resultRectangle in resultRectangleArray) { image.Draw(resultRectangle, new Bgr(Color.Red), 1); } ImageViewer.Show ( image, string.Format ( "{0}를 사용해 보행자 검출하기 : {1} 밀리초", GpuInvoke.HasCuda ? "GPU" : "CPU", processingTime ) ); } } #endregion } } |