Exemple #1
0
 //Filter to find red
 private Bitmap redF(Bitmap image)
 {
     //Create Filter and set value ranges
     AForge.Imaging.Filters.HSLFiltering filter = new AForge.Imaging.Filters.HSLFiltering();
     filter.Hue        = new AForge.IntRange(350, 25);
     filter.Saturation = new AForge.Range(0.5f, 1);
     filter.Luminance  = new AForge.Range(0.15f, 1);
     filter.ApplyInPlace(image);
     //Create a blob counter to search through the image
     AForge.Imaging.BlobCounter extractor = new AForge.Imaging.BlobCounter();
     extractor.MinHeight   = 5;
     extractor.MinWidth    = 5;
     extractor.FilterBlobs = true;
     extractor.ProcessImage(image);
     //Gets the rectangles so that we can draw around them
     Rectangle[] rects = extractor.GetObjectsRectangles();
     if (rects.Length > 0)
     {
         Rectangle objectRect = rects[0];
         Graphics  g          = Graphics.FromImage(image);
         using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 2))
         {
             g.DrawRectangle(pen, objectRect);
         }
         g.Dispose();
         //Get the direction and change state.
         objDirectionRed(objectRect, image.Width);
         currentState = RoboState.FOLLOW_RED;
     }
     return(image);
 }
Exemple #2
0
 private Bitmap blueF(Bitmap image)
 {
     //Create Filter and set value ranges
     AForge.Imaging.Filters.HSLFiltering filter = new AForge.Imaging.Filters.HSLFiltering();
     filter.Hue        = new AForge.IntRange(200, 240);
     filter.Saturation = new AForge.Range(0.3f, 1);
     filter.Luminance  = new AForge.Range(0.1f, 1);
     filter.ApplyInPlace(image);
     AForge.Imaging.BlobCounter extractor = new AForge.Imaging.BlobCounter();
     extractor.MinHeight   = 5;
     extractor.MinWidth    = 5;
     extractor.FilterBlobs = true;
     extractor.ProcessImage(image);
     //Create a blob counter to search through the image
     Rectangle[] rects = extractor.GetObjectsRectangles();
     if (rects.Length > 0)
     {
         Rectangle objectRect = rects[0];
         Graphics  g          = Graphics.FromImage(image);
         using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 2))
         {
             g.DrawRectangle(pen, objectRect);
         }
         g.Dispose();
         //Get the direction and change state.
         if (currentState != RoboState.FOLLOW_RED && currentState != RoboState.FOLLOW_GREEN)
         {
             currentState = RoboState.SEARCH;
         }
     }
     return(image);
 }
Exemple #3
0
        void videoPlayer_NewFrame(object sender, ref System.Drawing.Bitmap image)
        {
            //do something, we got a new frame from the video source.
            lock (this)
            {
                if (bIsMotionDetectionOn)
                {
                    if (motionDetector != null)
                    {
                        float motionLevel = motionDetector.ProcessFrame(image);
                    }
                }

                if (bIsColorFilteringOn)
                {
                    //try filtering for a color

                    AForge.Imaging.Filters.HSLFiltering hslFilter = new AForge.Imaging.Filters.HSLFiltering();

                    // set color ranges to keep
                    if (this.userhslFilter != null)
                    {
                        //hslFilter = this.userhslFilter;

                        // apply the filter
                        //hslFilter.ApplyInPlace(image);



                        hslFilter.Hue = new IntRange(335, 0);//335,0 = nice saturated red
                        //hslFilter.Hue = new IntRange(45, 65);//yellow
                        //hslFilter.Hue = new IntRange(170, 190);//light blue
                        //hslFilter.Saturation = new DoubleRange(0.5, 1);
                        hslFilter.Saturation = new DoubleRange(0.5, 1).ToIntRange(false);
                        //hslFilter.Luminance = new DoubleRange(0.1, 1);
                        hslFilter.Luminance = new DoubleRange(0.1, 1).ToIntRange(false);
                        hslFilter.ApplyInPlace(image);

                        /*
                         * hslFilter.Hue.Max = Convert.ToInt32(selectedColor.GetHue());
                         * hslFilter.Hue.Min = Convert.ToInt32(selectedColor.GetHue());
                         * hslFilter.Luminance.Max = Convert.ToDouble(selectedColor.GetBrightness());
                         * hslFilter.Luminance.Min = 0.01;
                         * hslFilter.Saturation.Max = Convert.ToDouble(selectedColor.GetSaturation());
                         * hslFilter.Saturation.Min = 0.01;
                         */


                        /*
                         * AForge.Imaging.Filters.ColorFiltering colorFiltering = new AForge.Imaging.Filters.ColorFiltering();
                         * colorFiltering.Red = new IntRange(150,255);
                         * colorFiltering.Green = new IntRange(0, 70);
                         * colorFiltering.Blue = new IntRange(0, 70);
                         * colorFiltering.ApplyInPlace(image);
                         */
                        /*
                         * AForge.Imaging.Filters.EuclideanColorFiltering euclidClrFltr = new AForge.Imaging.Filters.EuclideanColorFiltering();
                         * //euclidClrFltr.CenterColor = Color.FromArgb(200, 0, 0);
                         * euclidClrFltr.CenterColor = Color.Empty;
                         * euclidClrFltr.CenterColor = Color.FromName(selectedColor.Name);
                         * euclidClrFltr.Radius = 100;
                         * euclidClrFltr.ApplyInPlace(image);
                         * */
                    }
                }

                if (this.bIsBlobCountingOn)
                {
                    //grayscale the filtered image.
                    AForge.Imaging.Filters.Grayscale grayscaling = new AForge.Imaging.Filters.Grayscale(
                        0.2125, 0.7154, 0.0721);
                    image = grayscaling.Apply(image);

                    //blob detection
                    ProcessBlobs(image);
                }
            }
        }
Exemple #4
0
        Bitmap RemoveGreenBackground(Bitmap image)
        {
            AForge.Imaging.Filters.HSLFiltering filter = new AForge.Imaging.Filters.HSLFiltering();
            if (Hue == 0)
            {
                filter.UpdateHue = false;
            }
            else
            {
                filter.Hue = new AForge.IntRange(Hue, 0);
            }
            if (Saturation == 0)
            {
                filter.UpdateSaturation = false;
            }
            else
            {
                filter.Saturation = new AForge.Range((float)Saturation, 1);
            }
            if (Luminance == 0)
            {
                filter.UpdateLuminance = false;
            }
            else
            {
                filter.Luminance = new AForge.Range(Luminance, 1);
            }
            //image = ResizeBitmap(image, 1620, 1080);
            Bitmap bmp = new Bitmap(image);

            filter.ApplyInPlace(bmp);
            Bitmap output = new Bitmap(bmp.Width, bmp.Height);

            // Iterate over all piels from top to bottom...
            for (int y = 0; y < output.Height; y++)
            {
                // ...and from left to right
                for (int x = 0; x < output.Width; x++)
                {
                    // Determine the pixel color
                    Color camColor = bmp.GetPixel(x, y);

                    // Every component (red, green, and blue) can have a value from 0 to 255, so determine the extremes
                    byte max = Math.Max(Math.Max(camColor.R, camColor.G), camColor.B);
                    byte min = Math.Min(Math.Min(camColor.R, camColor.G), camColor.B);

                    // Should the pixel be masked/replaced?
                    bool replace =
                        camColor.G != min && // green is not the smallest value
                        (camColor.G == max || // green is the biggest value
                         max - camColor.G < 2) && // or at least almost the biggest value
                        (max - min) > 40;    // minimum difference between smallest/biggest value (avoid grays)

                    if (replace)
                    {
                        camColor = Color.Transparent;
                    }

                    // Set the output pixel
                    output.SetPixel(x, y, camColor);
                }
            }
            output = TrimBitmap(output);
            return(output);
        }