■ 동작을 검출하는 방법을 보여준다.
▶ MainForm.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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
using System; using System.Drawing; using System.Windows.Forms; using Emgu.CV; using Emgu.CV.CvEnum; using Emgu.CV.Structure; using Emgu.CV.VideoSurveillance; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 캡처 /// </summary> private Capture capture; /// <summary> /// 동작 히스토리 /// </summary> private MotionHistory motionHistory; /// <summary> /// BGFG 검출기 /// </summary> private IBGFGDetector<Bgr> bgfgDetector; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); if(this.capture == null) { try { this.capture = new Capture(); } catch(Exception exception) { MessageBox.Show(exception.Message); } } if(this.capture != null) { this.motionHistory = new MotionHistory ( 1.0, // 동작 히스토리 지속 시간 (초 단위) 0.05, // cvCalcMotionGradient용 maxDelta (초 단위) 0.5 // cvCalcMotionGradient용 minDelta (초 단위) ); this.capture.ImageGrabbed += capture_ImageGrabbed; this.capture.Start(); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 폼 닫는 경우 처리하기 - Form_FormClosed(sender, e) /// <summary> /// 폼 닫는 경우 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_FormClosed(object sender, FormClosedEventArgs e) { this.capture.Stop(); } #endregion #region 캡처 이미지 GRAB 처리하기 - capture_ImageGrabbed(sender, e) /// <summary> /// 캡처 이미지 GRAB 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void capture_ImageGrabbed(object sender, EventArgs e) { using(Image<Bgr, Byte> captureImage = this.capture.RetrieveBgrFrame()) { using(MemStorage storage = new MemStorage()) { if(this.bgfgDetector == null) { this.bgfgDetector = new FGDetector<Bgr>(FORGROUND_DETECTOR_TYPE.FGD); } this.bgfgDetector.Update(captureImage); this.captureImageBox.Image = captureImage; this.motionHistory.Update(this.bgfgDetector.ForegroundMask); this.forgroundMaskImageBox.Image = this.bgfgDetector.ForegroundMask; #region 동작 마스크 복사본을 구하고 색상을 보정한다. double[] minimumValueArray; double[] maximumValueArray; Point[] minimumPointArray; Point[] maximumPointArray; this.motionHistory.Mask.MinMax(out minimumValueArray, out maximumValueArray, out minimumPointArray, out maximumPointArray); Image<Gray, Byte> motionMaskImage = this.motionHistory.Mask.Mul(255.0 / maximumValueArray[0]); #endregion #region 동작 이미지를 설정한다. Image<Bgr, Byte> motionImage = new Image<Bgr, byte>(motionMaskImage.Size); motionImage[0] = motionMaskImage; #endregion // 동작 영역을 정의하는 한계점은 미세 동작 검출 값을 줄인다. double minimumArea = 100; storage.Clear(); Seq<MCvConnectedComp> motionComponentSequence = this.motionHistory.GetMotionComponents(storage); foreach(MCvConnectedComp motionComponent in motionComponentSequence) { if(motionComponent.area < minimumArea) { continue; } // 특정 영역의 각도와 동작 픽셀 카운트를 찾는다. double angle; double motionPixelCount; this.motionHistory.MotionInfo(motionComponent.rect, out angle, out motionPixelCount); if(motionPixelCount < motionComponent.area * 0.05) { continue; } DrawMotion(motionImage, motionComponent.rect, angle, new Bgr(Color.Red)); } // 전체 동작 각도를 찾고 그린다. double overallAngle; double overallMotionPixelCount; this.motionHistory.MotionInfo(motionMaskImage.ROI, out overallAngle, out overallMotionPixelCount); DrawMotion(motionImage, motionMaskImage.ROI, overallAngle, new Bgr(Color.Green)); UpdateMessage ( string.Format ( "전체 탐지 동작 : {0}; 동작 픽셀 카운트 : {1}", motionComponentSequence.Total, overallMotionPixelCount ) ); this.motionImageBox.Image = motionImage; } } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 메시지 갱신하기 - UpdateMessage(message) /// <summary> /// 메시지 갱신하기 /// </summary> /// <param name="message">메시지</param> private void UpdateMessage(string message) { if(InvokeRequired && !IsDisposed) { Invoke((Action<string>)UpdateMessage, message); } else { this.message.Text = message; } } #endregion #region 동작 그리기 - DrawMotion(image, motionRectangle, angle, color) /// <summary> /// 동작 그리기 /// </summary> /// <param name="image">이미지</param> /// <param name="motionRectangle">동작 사각형</param> /// <param name="angle">각도</param> /// <param name="color">색상</param> private static void DrawMotion(Image<Bgr, Byte> image, Rectangle motionRectangle, double angle, Bgr color) { float circleRadius = (motionRectangle.Width + motionRectangle.Height) >> 2; Point centerPoint = new Point ( motionRectangle.X + motionRectangle.Width >> 1, motionRectangle.Y + motionRectangle.Height >> 1 ); CircleF circle = new CircleF(centerPoint, circleRadius); int directionX = (int)(Math.Cos(angle * (Math.PI / 180.0)) * circleRadius); int directionY = (int)(Math.Sin(angle * (Math.PI / 180.0)) * circleRadius); Point pointOnCircle = new Point(centerPoint.X + directionX, centerPoint.Y - directionY); LineSegment2D line = new LineSegment2D(centerPoint, pointOnCircle); image.Draw(circle, color, 1); image.Draw(line, color, 2); } #endregion } } |