/// <summary> /// processes Frame for Motion Detection based on background generation /// </summary> /// <param name="frame"> /// Takes in 2 Bitmap parameters, currentFrame and backgroundFrame /// </param> /// <returns> /// frame in which motion is marked /// </returns> public Bitmap processFrame(params Bitmap[] frame) { Bitmap currentFrame = frame[0]; // create grayscale filter (BT709) Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721); Bitmap GScurrentFrame = filter.Apply(currentFrame); if (this.backgroundFrame == null) { this.backgroundFrame = (Bitmap)GScurrentFrame.Clone(); GScurrentFrame.Dispose(); return currentFrame; } else { Morph filterx = new Morph(GScurrentFrame); filterx.SourcePercent = 0.75; Bitmap tmp = filterx.Apply(backgroundFrame); // dispose old background backgroundFrame.Dispose(); backgroundFrame = tmp; // create processing filters sequence FiltersSequence processingFilter = new FiltersSequence(); processingFilter.Add(new Difference(backgroundFrame)); processingFilter.Add(new Threshold(threshold_val)); processingFilter.Add(new Opening()); processingFilter.Add(new Edges()); // apply the filter Bitmap tmp1 = processingFilter.Apply(GScurrentFrame); IFilter extractChannel = new ExtractChannel(RGB.R); Bitmap redChannel = extractChannel.Apply(currentFrame); Merge mergeFilter = new Merge(); mergeFilter.OverlayImage = tmp1; Bitmap t3 = mergeFilter.Apply(redChannel); ReplaceChannel rc = new ReplaceChannel(RGB.R, t3); t3 = rc.Apply(currentFrame); redChannel.Dispose(); tmp1.Dispose(); GScurrentFrame.Dispose(); return t3; } }
private Bitmap getChangingBackgroundDifference(Bitmap frame) { if (background == null || lastFrame == null) { background = frame; return null; } if (background != lastFrame) { Morph filter = new Morph(background); filter.SourcePercent = 0.5; background = filter.Apply(lastFrame); } Difference differenceFilter = new Difference(background); Bitmap differenceBitmap = differenceFilter.Apply(frame); return differenceBitmap; }
// Process max 200 frames (5 min) in 320x240 resolution. So 76KB memory per frame (grayscale). 1200 frames is max 93 MB of RAM (normally less because of area) private void processFilePart() { int nrofframes = imageStack.Length; int i; int sum; // create filters Morph morphFilter = new Morph(); // filter for adapting background morphFilter.SourcePercent = 0.8; Difference differenceFilter = new Difference(); // filter for subtracting two frames Threshold thresholdFilter = new Threshold(); // filter for thresholding FiltersSequence filters = new FiltersSequence(); // all filters in one filters.Add(morphFilter); filters.Add(differenceFilter); filters.Add(thresholdFilter); thresholdFilter.ThresholdValue = threshold; // Process here for (i = 0; i < nrofframes; i++) { // move background towards current frame morphFilter.OverlayImage = imageStack[i]; Bitmap Temp = morphFilter.Apply(backgroundFrame); backgroundFrame = Temp.Clone(new Rectangle(0, 0, Temp.Width, Temp.Height), Temp.PixelFormat); Temp.Dispose(); // apply rest of the filters differenceFilter.OverlayImage = imageStack[i]; Bitmap Temp2 = filters.Apply(backgroundFrame); sum = 0; // Calculate sum of white pixels for (int j = 0; j < Temp2.Width; j++) { for (int k = 0; k < Temp2.Height; k++) { if (Temp2.GetPixel(j, k) != Color.FromArgb(255, 0, 0, 0)) { sum += 1; } } } Temp2.Dispose(); if (sum > objectsize) { tracker.addFrame(currentFrame); } currentFrame += 1; } // Discard Array for (i = 0; i < nrofframes; i++) { imageStack[i].Dispose(); } }