Exemple #1
0
        public static void TestColorFiltering()
        {
            var bmp = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(colorImgName);

            var            image = bmp.ToImage <Bgr, byte>();
            UnmanagedImage uIm   = UnmanagedImage.FromManagedImage(bmp);

            measure(() =>
            {
                var hsvIm = image.Convert <Hsv, byte>();
                var mask  = hsvIm.InRange(new Hsv(335 / 2, 0, 0), new Hsv(180, 0, 0), 0 /*just first channel*/);

                var maskedIm = image.CopyBlank();
                image.CopyTo(maskedIm, mask);
            },
                    () =>
            {
                AForge.Imaging.Filters.HSLFiltering f = new AForge.Imaging.Filters.HSLFiltering();
                f.Hue = new AForge.IntRange(335, 0);
                f.Apply(uIm);
            },
                    100,
                    "Image<,> HSV filtering",
                    "AForge HSL filtering");
        }
Exemple #2
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 #3
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 #4
0
        /// <summary>
        /// Look at the input image and find a suitable blob.
        /// </summary>
        /// <param name="filter">HSL filter to use for image segmentation.</param>
        /// <param name="image">Input image to segment.</param>
        /// <param name="minRect">Minimum size of output blob.</param>
        /// <param name="colourBitmap">Bitmap to write binary image to.</param>
        /// <param name="maxSize">Maximum size of output blob (default value will allow any size).</param>
        /// <returns>A rectangle array of all suitable blobs, or an empty array if no suitable blobs are found.</returns>
        protected System.Drawing.Rectangle[] DetectObstacle(AForge.Imaging.Filters.HSLFiltering filter, Bitmap image, System.Drawing.Point minRect, out Bitmap colourBitmap, System.Drawing.Point maxSize = default(System.Drawing.Point))
        {
            Bitmap filtered;

            filtered = filter.Apply(image);

            //short[,] structuringElement = new short[,] { { 0, 1, 0 }, { 1, 1, 1 }, { 0, 1, 0 } };

            filtered = BinaryImage(filtered);
            //AForge.Imaging.Filters.Opening openingFilter = new AForge.Imaging.Filters.Opening(structuringElement);
            //filtered = openingFilter.Apply(filtered);
            colourBitmap = filtered;

            AForge.Imaging.BlobCounter blobs = new AForge.Imaging.BlobCounter();
            blobs.MinWidth  = (int)minRect.X;
            blobs.MinHeight = (int)minRect.Y;
            if (!maxSize.IsEmpty)
            {
                blobs.MaxWidth  = maxSize.X;
                blobs.MaxHeight = maxSize.Y;
            }
            blobs.FilterBlobs  = true;
            blobs.ObjectsOrder = AForge.Imaging.ObjectsOrder.Size;
            blobs.ProcessImage(filtered);

            System.Drawing.Rectangle[] rectangles = blobs.GetObjectsRectangles();

            return(rectangles);
        }
Exemple #5
0
        /// <summary>
        /// Pass in a filter to set up.
        /// </summary>
        /// <param name="dict">Dictionary of filter values.</param>
        /// <param name="filter">Filter to be set.</param>
        /// <param name="colour">Name of the colour filter being modified.</param>
        public void SetIndividualFilters(Dictionary <string, float> dict, out AForge.Imaging.Filters.HSLFiltering filter, string colour)
        {
            filter = new AForge.Imaging.Filters.HSLFiltering();
            float left  = 0f;
            float right = 0f;

            dict.TryGetValue(colour + "HueMin", out left);
            dict.TryGetValue(colour + "HueMax", out right);
            filter.Hue = new AForge.IntRange((int)left, (int)right);
            dict.TryGetValue(colour + "SatMin", out left);
            dict.TryGetValue(colour + "SatMax", out right);
            filter.Saturation = new AForge.Range(left, right);
            dict.TryGetValue(colour + "LumMin", out left);
            dict.TryGetValue(colour + "LumMax", out right);
            filter.Luminance = new AForge.Range(left, right);
        }
Exemple #6
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 #7
0
        public void SetFilters(object values)
        {
            Dictionary<string, float> dict = (Dictionary<string, float>)values;

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

            float left = 80;

            float right = 0;

            dict.TryGetValue("greenHueMin", out left);
            dict.TryGetValue("greenHueMax", out right);
            greenFilter.Hue = new AForge.IntRange((int)left, (int)right);

            dict.TryGetValue("greenSatMin", out left);
            dict.TryGetValue("greenSatMax", out right);
            greenFilter.Saturation = new AForge.Range(left, right);

            dict.TryGetValue("greenLumMin", out left);
            dict.TryGetValue("greenLumMax", out right);
            greenFilter.Luminance = new AForge.Range(left, right);

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

            dict.TryGetValue("redHueMin", out left);
            dict.TryGetValue("redHueMax", out right);
            redFilter.Hue = new AForge.IntRange((int)left, (int)right);

            dict.TryGetValue("redSatMin", out left);
            dict.TryGetValue("redSatMax", out right);
            redFilter.Saturation = new AForge.Range(left, right);

            dict.TryGetValue("redLumMin", out left);
            dict.TryGetValue("redLumMax", out right);
            redFilter.Luminance = new AForge.Range(left, right);

            whiteFilter = new AForge.Imaging.Filters.HSLFiltering();
            whiteFilter.Hue = new AForge.IntRange(160, 240);
            whiteFilter.Saturation = new AForge.Range(0.1f, 1.0f);
            whiteFilter.Luminance = new AForge.Range(0.1f, 0.7f);
        }
Exemple #8
0
        /// <summary>
        /// Pass in a filter to set up.
        /// </summary>
        /// <param name="dict">Dictionary of filter values.</param>
        /// <param name="filter">Filter to be set.</param>
        /// <param name="colour">Name of the colour filter being modified.</param>
        public void SetIndividualFilters(Dictionary<string, float> dict, out AForge.Imaging.Filters.HSLFiltering filter, string colour)
        {
            filter = new AForge.Imaging.Filters.HSLFiltering();
            float left = 0f;
            float right = 0f;

            dict.TryGetValue(colour + "HueMin", out left);
            dict.TryGetValue(colour + "HueMax", out right);
            filter.Hue = new AForge.IntRange((int)left, (int)right);
            dict.TryGetValue(colour + "SatMin", out left);
            dict.TryGetValue(colour + "SatMax", out right);
            filter.Saturation = new AForge.Range(left, right);
            dict.TryGetValue(colour + "LumMin", out left);
            dict.TryGetValue(colour + "LumMax", out right);
            filter.Luminance = new AForge.Range(left, right);
        }
Exemple #9
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);
        }
        public static void TestColorFiltering()
        {
            var bmp = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(colorImgName);

            var image = bmp.ToImage<Bgr, byte>();
            UnmanagedImage uIm = UnmanagedImage.FromManagedImage(bmp);

            measure(() =>
            {
                var hsvIm = image.Convert<Hsv, byte>();
                var mask = hsvIm.InRange(new Hsv(335 / 2, 0, 0), new Hsv(180, 0, 0), 0 /*just first channel*/);

                var maskedIm = image.CopyBlank();
                image.CopyTo(maskedIm , mask);
            },
            () =>
            {

                AForge.Imaging.Filters.HSLFiltering f = new AForge.Imaging.Filters.HSLFiltering();
                f.Hue = new AForge.IntRange(335, 0);
                f.Apply(uIm);
            },
            100,
            "Image<,> HSV filtering",
            "AForge HSL filtering");
        }