protected override void ProcessFilter(BitmapData imageData) { if (mask == null) { mask = new Bitmap(context.Background.Width, context.Background.Height, context.Background.PixelFormat); } unsafe { int width = imageData.Width; int height = imageData.Height; PixelFormat pixelFormat = imageData.PixelFormat; BitmapData imageBits = imageData; BitmapData backBits = context.Background.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, pixelFormat); BitmapData maskBits = mask.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, pixelFormat); int stride = imageBits.Stride; byte *imagePtr = (byte *)imageBits.Scan0; byte *backPtr = (byte *)backBits.Scan0; byte *maskPtr = (byte *)maskBits.Scan0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int offset = y * stride + x; byte imagePixel = imagePtr[offset]; byte backPixel = backPtr[offset]; byte maskPixel = 255; if (imagePixel - backPixel > Threshold) { maskPixel = 0; } maskPtr[offset] = maskPixel; } } context.Background.UnlockBits(backBits); mask.UnlockBits(maskBits); } subtract.OverlayImage = mask; subtract.ApplyInPlace(imageData); }
void cam_NewFrame(object sender, NewFrameEventArgs eventArgs) { Bitmap realimage = (Bitmap)eventArgs.Frame.Clone(); Bitmap rimage = (Bitmap)eventArgs.Frame.Clone(); Bitmap bimage = (Bitmap)eventArgs.Frame.Clone(); //Red Object detection GrayscaleBT709 grayscale = new GrayscaleBT709(); Subtract sfilter = new Subtract(grayscale.Apply(rimage)); ExtractChannel rchannel = new ExtractChannel(RGB.R); ExtractChannel bchannel = new ExtractChannel(RGB.B); rimage = rchannel.Apply(rimage); bimage = bchannel.Apply(bimage); sfilter.ApplyInPlace(rimage); sfilter.ApplyInPlace(bimage); Median mfilter = new Median(5); mfilter.ApplyInPlace(rimage); mfilter.ApplyInPlace(bimage); Threshold thresh = new Threshold(50); thresh.ApplyInPlace(rimage); thresh.ApplyInPlace(bimage); BlobCounter rblob = new BlobCounter(); rblob.ObjectsOrder = ObjectsOrder.Area; rblob.ProcessImage(rimage); BlobCounter bblob = new BlobCounter(); bblob.ObjectsOrder = ObjectsOrder.Area; bblob.ProcessImage(bimage); Rectangle[] rrect = rblob.GetObjectsRectangles(); Rectangle[] brect = bblob.GetObjectsRectangles(); p = new int[rrect.Length + brect.Length]; arr = 0; if (rrect.Length > 0) { for (int i = 0; i < rrect.Length; i++) { if (rrect[i].Height > 20 && rrect[i].Width > 20)//Object Min height width { Graphics g = Graphics.FromImage(realimage); g.DrawRectangle(new Pen(Color.Red), rrect[i]); objheight = rrect[i].Height; objwidth = rrect[i].Width; objx = rrect[i].X + (objwidth / 2); objy = rrect[i].Y + (objheight / 2); p[i] = getpartition(); arr++; g.DrawEllipse(new Pen(Color.Yellow), objx, objy, 5, 5); g.DrawString("Red Object", new Font("Arial", 10), new SolidBrush(Color.AntiqueWhite), objx, objy - 15); g.DrawString("X:" + objx + " Y:" + objy, new Font("Arial", 10), new SolidBrush(Color.LimeGreen), objx, objy); g.DrawString("H:" + objheight + " W:" + objwidth, new Font("Arial", 10), new SolidBrush(Color.LightCyan), objx, objy + 15); g.DrawString("P:" + p[i], new Font("Arial", 10), new SolidBrush(Color.LightSkyBlue), objx, objy + 25); } } obj = largest(); if (obj == 0) { obj = prevobj; } prevobj = obj; } if (brect.Length > 0) { for (int i = 0; i < brect.Length; i++) { if (brect[i].Height > 20 && brect[i].Width > 20)//Object Min height width { Graphics g = Graphics.FromImage(realimage); g.DrawRectangle(new Pen(Color.Red), brect[i]); objheight = brect[i].Height; objwidth = brect[i].Width; objx = brect[i].X + (objwidth / 2); objy = brect[i].Y + (objheight / 2); p[i + arr] = getpartition(); g.DrawEllipse(new Pen(Color.Yellow), objx, objy, 5, 5); g.DrawString("Blue Object", new Font("Arial", 10), new SolidBrush(Color.AntiqueWhite), objx, objy - 15); g.DrawString("X:" + objx + " Y:" + objy, new Font("Arial", 10), new SolidBrush(Color.LimeGreen), objx, objy); g.DrawString("H:" + objheight + " W:" + objwidth, new Font("Arial", 10), new SolidBrush(Color.LightCyan), objx, objy + 15); g.DrawString("P:" + p[i + arr], new Font("Arial", 10), new SolidBrush(Color.LightSkyBlue), objx, objy + 25); } } obj = largest(); if (obj == 0) { obj = prevobj; } prevobj = obj; } displayBox.Image = realimage; }