private void _motionDetector_MotionDetected(object sender, MotionDetectedEventArgs e) { _view.OnMotionDetectionEvent(e.MotionDetectionEvent); foreach (SecurityEventSubscriber notification in _notifications) { notification.Notify(e.MotionDetectionEvent); } }
private void DetectMotion() { if ((_previousFrame != null) && (_currentFrame != null)) { Grayscale grayscaleFilter = new Grayscale(0.2125, 0.7154, 0.0721); using (Bitmap frame1GS = grayscaleFilter.Apply(_previousFrame)) { using (Bitmap frame2GS = grayscaleFilter.Apply(_currentFrame)) { Difference differenceFilter = new Difference(); IFilter thresholdFilter = new Threshold(15); // set backgroud frame as an overlay for difference filter differenceFilter.OverlayImage = (Bitmap)frame1GS; // apply the filters using (Bitmap tmp1 = differenceFilter.Apply((Bitmap)frame2GS)) { using (Bitmap tmp2 = thresholdFilter.Apply(tmp1)) { IFilter erosionFilter = new Erosion(); // apply the filter using (Bitmap tmp3 = erosionFilter.Apply(tmp2)) { int whitePixelsCount = CalculateWhitePixels(tmp3); if (whitePixelsCount > 1000) { MotionDetectionEvent motionEvent = new MotionDetectionEvent() { CameraDevice = _camera, NumberOfPixelsDetected = whitePixelsCount, InputDevice = _camera }; MotionDetectedEventArgs args = new MotionDetectedEventArgs(motionEvent); OnMotionDetected(args); } //// extract red channel from the original image //IFilter extrachChannel = new ExtractChannel(RGB.R); //Bitmap redChannel = extrachChannel.Apply(frame2); //// merge red channel with motion regions //Merge mergeFilter = new Merge(); //mergeFilter.OverlayImage = tmp3; //Bitmap tmp4 = mergeFilter.Apply(redChannel); //// replace red channel in the original image //ReplaceChannel replaceChannel = new ReplaceChannel(RGB.R, tmp4); //replaceChannel.ChannelImage = tmp4; //Bitmap tmp5 = replaceChannel.Apply(frame2); } } } } } } }