public void Video_NewFrame(object sender, NewFrameEventArgs eventArgs) { UnmanagedImage image = UnmanagedImage.FromManagedImage((Bitmap) eventArgs.Frame.Clone()); var extractChannel = new ExtractChannel(RGB.R); UnmanagedImage channel = extractChannel.Apply(image); // UnmanagedImage originalRed = channel.Clone(); if (true) { var threshold = new Threshold(200); threshold.ApplyInPlace(channel); ////filter to convert RGB image to 8bpp gray scale for image processing //IFilter gray_filter = new GrayscaleBT709(); //gray_image = gray_filter.Apply(gray_image); ////thresholding a image //Threshold th_filter = new Threshold(color_data.threshold); //th_filter.ApplyInPlace(gray_image); //erosion filter to filter out small unwanted pixels Erosion3x3 erosion = new Erosion3x3(); erosion.ApplyInPlace(channel); //dilation filter //Dilatation3x3 dilatation = new Dilatation3x3(); //dilatation.ApplyInPlace(channel); //GrayscaleToRGB filter = new GrayscaleToRGB(); //image = filter.Apply(channel); //ReplaceChannel replaceFilter = new ReplaceChannel(RGB.B, channel); //replaceFilter.ApplyInPlace(image); } BlobCounter bc = new BlobCounter(); //arrange blobs by area bc.ObjectsOrder = ObjectsOrder.Area; bc.FilterBlobs = true; bc.MinHeight = minObjectSize; bc.MinWidth = minObjectSize; bc.MaxHeight = maxObjectSize; bc.MaxWidth = maxObjectSize; //process image for blobs bc.ProcessImage(channel); channel.Dispose(); // if (motionDetector.ProcessFrame(image) > 0.02) { // for (int i = 0; i < blobCountingObjectsProcessing.ObjectRectangles.Length; i++) { Rectangle[] rectangles = bc.GetObjectsRectangles(); Blob[] blobs = bc.GetObjectsInformation(); for (int i = 0; i < bc.ObjectsCount; i++) { Rectangle rectangle = rectangles[i]; int width = rectangle.Width; int height = rectangle.Height; // if (width < maxObjectSize && height < maxObjectSize && width > minObjectSize && height > minObjectSize) { Drawing.Rectangle(image, rectangle, colorList[i % colorList.Length]); if (i == 0) { Position = GetCenterOfMass(image, rectangle); Drawing.FillRectangle(image, rectangle, Color.BlanchedAlmond); Drawing.FillRectangle(image, new Rectangle((int) Position.U - dotSize, (int) Position.V - dotSize, dotSize * 3, dotSize * 3), Color.Indigo); } // } } // } Image = image.ToManagedImage(); // videoForm.ImageDestination.Image = image.ToManagedImage(); }
static void Main(string[] args) { Threshold thresh = new Threshold(10); Median median = new Median(9); Erosion3x3 erode = new Erosion3x3(); Dilatation3x3 dilate = new Dilatation3x3(); GrahamConvexHull hullFinder = new GrahamConvexHull(); ConnectedComponentsLabeling ccLabeler = new ConnectedComponentsLabeling(); BorderFollowing contourFinder = new BorderFollowing(); GrayscaleToRGB rgb = new GrayscaleToRGB(); ConvexHullDefects defectFinder = new ConvexHullDefects(10); Bitmap img = (Bitmap)Bitmap.FromFile("hand3.jpg"); Bitmap image = Grayscale.CommonAlgorithms.BT709.Apply(img); thresh.ApplyInPlace(image); //median.ApplyInPlace(image); erode.ApplyInPlace(image); dilate.ApplyInPlace(image); BlobCounter counter = new BlobCounter(image); counter.ObjectsOrder = ObjectsOrder.Area; Blob[] blobs = counter.GetObjectsInformation(); if (blobs.Length > 0) { counter.ExtractBlobsImage(image, blobs[0], true); UnmanagedImage hand = blobs[0].Image; var contour = contourFinder.FindContour(hand); if (contour.Count() > 0) { var initialHull = hullFinder.FindHull(contour); var defects = defectFinder.FindDefects(contour, initialHull); var filteredHull = initialHull.ClusterHullPoints().FilterLinearHullPoints(); var palmCenter = defects.Centroid(contour); var wristPoints = filteredHull.SelectWristPoints(defects, contour); Bitmap color = rgb.Apply(hand).ToManagedImage(); //BitmapData data = color.LockBits(new Rectangle(0, 0, color.Width, color.Height), ImageLockMode.ReadWrite, color.PixelFormat); //Drawing.Polyline(data, contour, Color.Blue); //Drawing.Polygon(data, filteredHull, Color.Red); //color.UnlockBits(data); Graphics gr = Graphics.FromImage(color); gr.DrawPolygon(new Pen(Brushes.Red, 3), filteredHull.ToPtArray()); gr.DrawLines(new Pen(Brushes.Blue, 3), contour.ToPtArray()); gr.DrawEllipse(new Pen(Brushes.Red, 3), palmCenter.X - 10, palmCenter.Y - 10, 20, 20); foreach (ConvexityDefect defect in defects) { gr.DrawEllipse(new Pen(Brushes.Green, 6), contour[defect.Point].X - 10, contour[defect.Point].Y - 10, 20, 20); } foreach (AForge.IntPoint pt in filteredHull) { gr.DrawEllipse(new Pen(Brushes.Yellow, 6), pt.X - 10, pt.Y - 10, 20, 20); } foreach (AForge.IntPoint pt in wristPoints) { gr.DrawEllipse(new Pen(Brushes.PowderBlue, 6), pt.X - 10, pt.Y - 10, 20, 20); } ImageBox.Show(color); } } }