Channels filters.

The filter does color channels' filtering by clearing (filling with specified values) values, which are inside/outside of the specified value's range. The filter allows to fill certain ranges of RGB color channels with specified value.

The filter is similar to ColorFiltering, but operates with not entire pixels, but with their RGB values individually. This means that pixel itself may not be filtered (will be kept), but one of its RGB values may be filtered if they are inside/outside of specified range.

The filter accepts 24 and 32 bpp color images for processing.

Sample usage:

// create filter ChannelFiltering filter = new ChannelFiltering( ); // set channels' ranges to keep filter.Red = new IntRange( 0, 255 ); filter.Green = new IntRange( 100, 255 ); filter.Blue = new IntRange( 100, 255 ); // apply the filter filter.ApplyInPlace( image );

Initial image:

Result image:

Inheritance: BaseInPlacePartialFilter
        public ColorPart()
        {
            //previousHSV = null;

            resizeFilter = new ResizeBilinear(128, 128);

            pixelateFilter = new Pixellate();
            pixelateFilter.PixelWidth = 32;
            pixelateFilter.PixelHeight = 32;

            redChannelFilter = new ChannelFiltering();
            greenChannelFilter = new ChannelFiltering();
            blueChannelFilter = new ChannelFiltering();
            intRange = new IntRange(0, 0);
            redChannelFilter.Green = intRange; redChannelFilter.Blue = intRange;
            greenChannelFilter.Red = intRange; greenChannelFilter.Blue = intRange;
            blueChannelFilter.Red = intRange; blueChannelFilter.Green = intRange;

            frameMatrices = new List<int[, ,]>();
        }
 private void btnEffectApply_Click(object sender, EventArgs e)
 {
     resizerControl.Visible = true;
     IFilter imgeFilter = default(IFilter);
     Button effect = (Button)sender;
     Bitmap imgEffect = img;
     if (effect.Name == "btnGray")
     {
         imgeFilter = new GrayscaleBT709();
     }
     else if (effect.Name == "btnSeperia")
     {
         imgeFilter = new Sepia();
     }
     else if (effect.Name == "btnInvert")
     {
         imgeFilter = new Invert();
     }
     else if (effect.Name == "btnCommon")
     {
         imgeFilter = new BurkesDithering();
     }
     else if (effect.Name == "btnBlur")
     {
         imgeFilter = new Blur();
     }
     else if (effect.Name == "btnJitter")
     {
         imgeFilter = new Texturer(new AForge.Imaging.Textures.MarbleTexture(10, 11), 0.7f, 0.3f);
     }
     else if (effect.Name == "btnCyan")
     {
         imgeFilter = new ChannelFiltering(new IntRange(0, 0), new IntRange(0, 255), new IntRange(0, 255));
     }
     else if (effect.Name == "btnBlackWhite")
     {
         imgeFilter = new YCbCrExtractChannel(AForge.Imaging.YCbCr.CrIndex);
     }
     imgEffect = imgeFilter.Apply(img);
     panelImage.BackgroundImage = imgEffect;
     //img = imgEffect;
 }
 private void Effect()
 {
     IFilter imgeFilter = new BurkesDithering();
     Bitmap bitimg = new Bitmap(imgeFilter.Apply(img), 90, 111);
     btnCommon.Image = bitimg;
     imgeFilter = new GrayscaleBT709();
     btnGray.Image = new Bitmap(imgeFilter.Apply(img), 90, 111);
     imgeFilter = new Sepia();
     bitimg = new Bitmap(imgeFilter.Apply(img), 90, 111);
     btnSeperia.Image = bitimg;
     imgeFilter = new Invert();
     bitimg = new Bitmap(imgeFilter.Apply(img), 90, 111);
     btnInvert.Image = bitimg;
     imgeFilter = new Blur();
     bitimg = new Bitmap(imgeFilter.Apply(img), 90, 111);
     btnBlur.Image = bitimg;
     imgeFilter = new Texturer(new AForge.Imaging.Textures.MarbleTexture(10, 11), 0.7f, 0.3f);
     bitimg = new Bitmap(imgeFilter.Apply(img), 90, 111);
     btnJitter.Image = bitimg;
     imgeFilter = new ChannelFiltering(new IntRange(0, 0), new IntRange(0, 255), new IntRange(0, 255));
     bitimg = new Bitmap(imgeFilter.Apply(img), 90, 111);
     btnCyan.Image = bitimg;
     imgeFilter = new YCbCrExtractChannel(AForge.Imaging.YCbCr.CrIndex);
     bitimg = new Bitmap(imgeFilter.Apply(img), 90, 111);
     btnBlackWhite.Image = bitimg;
 }
        private Bitmap ProcessImage(Bitmap bitmap)
        {
            ///CROP SYSTEM

            //if (cropx1 != 0 && cropy1 != 0 && cropx2 != 0 && cropy2 != 0 && Worker.selected_square != 0)
            //{
            //    Crop crop_filter = new Crop(new Rectangle(cropx1, cropy1, cropx2, cropy2));
            //     bitmap= crop_filter.Apply(bitmap);
            //    Console.WriteLine(">Crop Cutter:> Cropped\n({0},{1}),({2},{3})", cropx1, cropy1, cropx2, cropy2);
            //}

            //if (Worker.selected_square == 0)
            //{
            //    Crop crop_filter = new Crop(new Rectangle(0, 0, bitmap.Width,bitmap.Height));
            //    bitmap = crop_filter.Apply(bitmap);
            //    //Console.WriteLine(">Crop Cutter:> RESET");
            //}
            //// step 2 - locating objects

            if (cropx1 == 0 && cropy1 == 0 && cropx2 == 0 && cropy2 == 0)
            {
                cropx1 = 0;
                cropy1 = 0;
                cropx2 = bitmap.Width;
                cropy2 = bitmap.Height;

            }
            Bitmap old = bitmap;
            Crop crop_filter = new Crop(new Rectangle(cropx1, cropy1, cropx2, cropy2));

            bitmap = crop_filter.Apply(bitmap);

            // lock image
            BitmapData bitmapData = bitmap.LockBits(
                new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.ReadWrite, bitmap.PixelFormat);

            //  ////invert
            // Invert inv = new Invert();
            //  // //apply the filter
            //inv.ApplyInPlace(bitmapData);

            //// create filter
            //HSLFiltering filter = new HSLFiltering();
            //// set color ranges to keep
            //filter.Hue = new IntRange(90, 300);
            //filter.Saturation = new Range(0.0f, .5f);
            //filter.Luminance = new Range(0f, 1);
            //// apply the filter
            //filter.ApplyInPlace(bitmapData);

            //EuclideanColorFiltering filter = new EuclideanColorFiltering();
            //// set center colol and radius
            //filter.CenterColor = new RGB(209, 206, 196);
            //filter.Radius = 150;
            //// apply the filter
            //filter.ApplyInPlace(bitmapData);

            //// create filter
            ChannelFiltering filter = new ChannelFiltering();
            //// set channels' ranges to keep

            Invert inv = new Invert();

            //// //apply the filter

            inv.ApplyInPlace(bitmapData);

            if (red_max > 255)
            {
                red_max = 255;
                Console.WriteLine("1THRESHOLD OVERIDE");
            }
            if (blue_max > 255)
            {
                blue_max = 255;
                Console.WriteLine("2THRESHOLD OVERIDE");
            }
            if (green_max > 255)
            {
                green_max = 255;
                Console.WriteLine("3THRESHOLD OVERIDE");
            }
            if (red > 255)
            {
                red = 254;
                Console.WriteLine("4THRESHOLD OVERIDE");
            }
            if (blue > 255)
            {
                blue = 254;
                Console.WriteLine("5THRESHOLD OVERIDE");
            }
            if (green > 255)
            {
                green = 254;
                Console.WriteLine("6THRESHOLD OVERIDE");
            }

            if (red < 0)
            {
                red = 1;
                Console.WriteLine("7THRESHOLD UNDERIDE");
            }
            if (blue < 0)
            {
                blue = 1;
                Console.WriteLine("8THRESHOLD UNDERIDE");
            }
            if (green < 0)
            {
                green = 1;
                Console.WriteLine("9THRESHOLD UNDERIDE");
            }

            if (red_max < 0)
            {
                red_max = 255;
                Console.WriteLine("10THRESHOLD UNDERIDE");
            }
            if (blue_max < 0)
            {
                blue_max = 255;
                Console.WriteLine("11THRESHOLD UNDERIDE");
            }
            if (green_max < 0)
            {
                green_max = 255;
                Console.WriteLine("13THRESHOLD UNDERIDE");
            }

            if (red_max < red)
            {
                Console.WriteLine("14THRESHOLD UNION ERROR");
                red = 0;
                red_max = 255;
            }

            if (blue_max < blue)
            {
                blue = 0;
                Console.WriteLine("15THRESHOLD UNION ERROR");
                blue_max = 255;
            }
            if (green_max < green)
            {
                green = 0;
                green_max = 255;
                Console.WriteLine("16THRESHOLD UNION ERROR");
            }

            file.WriteLine("R: " + red + " R_M: " + red_max);
            file.WriteLine("G: " + green + " G_M: " + green_max);
            file.WriteLine("B: " + blue + " B_M: " + blue_max);
            file.WriteLine("EOE");
            //file.Close();
            filter.Red = new IntRange(red, red_max);
            filter.Green = new IntRange(green, green_max);
            filter.Blue = new IntRange(blue, blue_max);
            //// apply the filter
            filter.ApplyInPlace(bitmapData);

            ////invert

            //edge

            //CannyEdgeDetector edge_filter = new CannyEdgeDetector();
            //// apply the filter
            //edge_filter.ApplyInPlace(bitmapData);

            BlobCounter blobCounter = new BlobCounter();
            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight = blob_size * 3;
            blobCounter.MinWidth = blob_size * 4;

            blobCounter.ProcessImage(bitmapData);
            Blob[] blobs = blobCounter.GetObjectsInformation();
            bitmap.UnlockBits(bitmapData);

            // step 3 - check objects' type and highlight
            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();
            Graphics g = Graphics.FromImage(bitmap);
            Pen yellowPen = new Pen(Color.Yellow, 2); // circles
            Pen redPen = new Pen(Color.Red, 2);       // quadrilateral
            Pen brownPen = new Pen(Color.Brown, 2);   // quadrilateral with known sub-type
            Pen greenPen = new Pen(Color.Green, 2);   // known triangle
            Pen bluePen = new Pen(Color.Blue, 2);     // triangle
            double curdist = 0;
            double leastdist = 100;
            //List<IntPoint> bestcorn;//promoted to super
            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
                List<IntPoint> corners; // the list of x,y coordinates.  corners(list)->corner(intpoint)->[x,y]

                // is triangle or quadrilateral

                if (shapeChecker.IsConvexPolygon(edgePoints, out corners))
                {
                    cornide = corners;
                    // get sub-type

                    PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners);
                    Pen pen;
                    if (subType == PolygonSubType.Rectangle || subType == PolygonSubType.Trapezoid || subType == PolygonSubType.Parallelogram || subType == PolygonSubType.Rhombus || subType == PolygonSubType.Square || subType == PolygonSubType.Unknown)
                    {

                        if (corners.Count == 4)
                        {
                            pen = redPen;
                            IntPoint[] array_of_corners = corners.ToArray();//array_of_corners is now an array of corners
                            if (((732 / avdisty(array_of_corners)) + .222) < ((989 / avdistx(array_of_corners)) + 3.37))
                            {
                                curdist = ((732 / avdisty(array_of_corners)) + .222);//from our graph
                            }
                            else
                            {
                                curdist = ((989 / avdistx(array_of_corners)) + 3.37);//from our graph
                            }

                            //if (Worker.selected_square == 1)
                            //{
                            //    Console.WriteLine("called\n");
                            //    IntPoint one = array_of_corners[0];
                            //    IntPoint two = array_of_corners[3];
                            //    cropx1 = one.X - 5;
                            //    cropy1 = one.Y - 5;
                            //    cropx2 = two.X + 5;
                            //    cropy2 = two.Y + 5;
                            //}
                            //else if (i == Worker.selected_square - 1 && Worker.selected_square != 1)
                            //{
                            //    Console.WriteLine("called\n");
                            //    IntPoint one = array_of_corners[0];
                            //    IntPoint two = array_of_corners[3];
                            //    cropx1 = one.X - 5;
                            //    cropy1 = one.Y - 5;
                            //    cropx2 = two.X + 5;
                            //    cropy2 = two.Y + 5;
                            //}
                            //if (Worker.selected_square == 0)
                            //{
                            //    cropx1 = 0;
                            //    cropy1 = 0;
                            //    cropx2 = 0;
                            //    cropy2 = 0;
                            //}

                            if (Worker.selected_square == 0)//PREDATOR VISION
                            {

                                if (curdist < leastdist)
                                {
                                    //  Console.WriteLine((abs(array_of_corners[0].Y - array_of_corners[1].Y)) / (abs(array_of_corners[0].X - array_of_corners[1].X)));
                                    //if ((abs(array_of_corners[0].Y - array_of_corners[3].Y)) / (abs(array_of_corners[0].X - array_of_corners[1].X)) < 1.25 && (abs(array_of_corners[0].Y - array_of_corners[3].Y)) / (abs(array_of_corners[0].X - array_of_corners[1].X)) > .5)
                                    if (true)
                                    {

                                        leastdist = curdist;
                                        if (leastdist < 55 && leastdist > 2)
                                        {

                                            double angle = (array_of_corners[0].X + array_of_corners[3].X) / 2;
                                            Console.WriteLine("0: " + array_of_corners[0].X + " 1: " + array_of_corners[1].X + " 2: " + array_of_corners[2].X + " 3: " + array_of_corners[3].X);
                                            Server.SetData(((array_of_corners[0].X + array_of_corners[1].X) / 2), 1);
                                            Console.WriteLine("PREDATOR: " + Server.GetData(1));
                                            Console.WriteLine(leastdist);
                                            Server.SetData(leastdist, 0);

                                            bestcorn = corners;//superify?
                                            //g.DrawPolygon(pen, ToPointsArray(bestcorn));//leave here as a comment

                                            IntPoint ones = array_of_corners[0];
                                            PointF string_draw = new PointF(ones.X, ones.Y);
                                            SolidBrush tbrush = new SolidBrush(Color.Red);
                                            try
                                            {
                                                if (string_draw.X != 0 && string_draw.Y != 0)
                                                {
                                                    g.DrawString(n.ToString(), this.Font, tbrush, string_draw);
                                                }
                                            }
                                            catch (Exception e)
                                            {
                                                Console.WriteLine("{0} in {1} ({2},{3})", e.Message, e.StackTrace, string_draw.X, string_draw.Y);
                                            }

                                        }

                                    }
                                }
                                try
                                {

                                    Graphics global_graphics_predator = Graphics.FromImage(bitmap);
                                    Pen penc = new Pen(Color.YellowGreen, 2);

                                    //g.DrawPolygon(penc, ToPointsArray(bestcorn));
                                    global_graphics_predator.DrawPolygon(penc, ToPointsArray(bestcorn));
                                    global_graphics_predator.DrawLine(penc, new System.Drawing.Point(320, 1), new System.Drawing.Point(320, 479));
                                    //bitmap = global_bitmap;

                                }
                                catch (Exception e)
                                {
                                }

                                yellowPen.Dispose();
                                redPen.Dispose();
                                greenPen.Dispose();
                                bluePen.Dispose();
                                brownPen.Dispose();
                                g.Dispose();

                                return bitmap;
                            }
                            else
                            {

                                int sel_least_dist = Worker.selected_square;
                                sel_least_dist *= 3;//threefeet
                                sel_least_dist -= 1;
                                int sel_max_dist = sel_least_dist + 5;
                                Console.WriteLine(sel_least_dist);
                                Console.WriteLine(sel_max_dist);
                                //Console.WriteLine(sel_least_dist + " " + sel_max_dist);

                                if (curdist < leastdist && curdist > sel_least_dist && curdist < sel_max_dist)
                                {
                                    //  Console.WriteLine((abs(array_of_corners[0].Y - array_of_corners[1].Y)) / (abs(array_of_corners[0].X - array_of_corners[1].X)));
                                    //if ((abs(array_of_corners[0].Y - array_of_corners[3].Y)) / (abs(array_of_corners[0].X - array_of_corners[1].X)) < 1.25 && (abs(array_of_corners[0].Y - array_of_corners[3].Y)) / (abs(array_of_corners[0].X - array_of_corners[1].X)) > .5)
                                    if (true)//todo
                                    {

                                        leastdist = curdist;
                                        if (leastdist < 55 && leastdist > 2)
                                        {

                                            // Console.WriteLine("0: " + array_of_corners[0].X + " 1: " + array_of_corners[1].X + " 2: " + array_of_corners[2].X + " 3: " + array_of_corners[3].X);

                                            Server.SetData(((array_of_corners[0].X + array_of_corners[1].X) / 2), 1);
                                            Console.WriteLine(Server.GetData(1));
                                            //Console.WriteLine(leastdist);
                                            Server.SetData(leastdist, 0);
                                            bestcorn = corners;//superify?
                                            //g.DrawPolygon(pen, ToPointsArray(bestcorn));//leave here as a comment

                                            IntPoint ones = array_of_corners[0];
                                            PointF string_draw = new PointF(ones.X, ones.Y);
                                            SolidBrush tbrush = new SolidBrush(Color.Red);
                                            try
                                            {
                                                if (string_draw.X != 0 && string_draw.Y != 0)
                                                {
                                                    g.DrawString(n.ToString(), this.Font, tbrush, string_draw);
                                                    // glob.DrawLine(penc, new System.Drawing.Point(10, 10), new System.Drawing.Point(20, 20));
                                                    //g.DrawLine(pen, new System.Drawing.Point(10, 10), new System.Drawing.Point(20, 20));
                                                }
                                            }
                                            catch (Exception e)
                                            {
                                                Console.WriteLine("{0} in {1} ({2},{3})", e.Message, e.StackTrace, string_draw.X, string_draw.Y);
                                            }

                                        }

                                    }
                                }
                            }

                            //pen = (corners.Count == 4) ? redPen : bluePen;
                        }//TODO get rid of anything that ISNT drawn, eg corner
                    }

                    //else
                    //{

                    //    pen = (corners.Count == 4) ? brownPen : greenPen;

                    //}

                }

            }
            //Console.WriteLine(leastdist); how dod oyo

            //double leastdist = 0;
            //for (int i = 0, n = blobs.Length; i < n; i++)
            //{
            //    try
            //    {
            //        List<IntPoint> filterlist;
            //        filterlist = cornide;

            //        IntPoint[] filter_corners = filterlist.ToArray();

            //        double currentdist = 0;
            //        for (int corner_count = 0; corner_count < filter_corners.Length; corner_count++)
            //        {

            //            IntPoint one_f = filter_corners[corner_count];
            //            corner_count++;
            //            IntPoint two_f = filter_corners[corner_count];
            //            corner_count++;
            //            IntPoint three_f = filter_corners[corner_count];
            //            corner_count++;
            //            IntPoint four_f = filter_corners[corner_count];

            //            double y_average_a = ((double)one_f.Y + (double)two_f.Y) / 2;
            //            double y_average_b = ((double)three_f.Y + (double)four_f.Y) / 2;
            //            double averaged_dist = y_average_b - y_average_a;
            //            currentdist = ((732 / averaged_dist) + .222);//from our graph
            //            if (currentdist <= leastdist)
            //            {
            //                leastdist = currentdist;
            //                Pen pencil = redPen;

            //                g.DrawPolygon(pencil, ToPointsArray(filterlist));
            //            }

            //        }

            //    }
            //    catch
            //    {
            //        //do nothING
            //    }

            //}
            //Console.WriteLine(leastdist);

            try
            {

                Graphics global_graphics = Graphics.FromImage(global_bitmap);
                Pen penc = new Pen(Color.YellowGreen, 2);
                {
                    //g.DrawPolygon(penc, ToPointsArray(bestcorn));
                    global_graphics.DrawPolygon(penc, ToPointsArray(bestcorn));
                    global_graphics.DrawLine(penc, new System.Drawing.Point(320, 1), new System.Drawing.Point(320, 479));

                    bitmap = global_bitmap;
                }
            }
            catch (Exception e)
            {
            }
            try
            {
                g.DrawLine(redPen, 220, 1, 220, 100);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
                Console.WriteLine(e.InnerException);

            }
            yellowPen.Dispose();
            redPen.Dispose();
            greenPen.Dispose();
            bluePen.Dispose();
            brownPen.Dispose();

            g.Dispose();

            return bitmap;
        }
 internal void ExceRGBFilter(ChannelFiltering filter)
 {
     if (currentImageHandler != null)
       {
     currentImageHandler.ColorFitler.ExecuteFitler(filter);
       }
 }
示例#6
0
        private void RGBFilter_Clicked(object sender, RoutedEventArgs e)
        {
            if (mainPhoto != null)
            {
                chf = new ChannelFiltering();

                // set channels' ranges to keep
                chf.Red = new AForge.IntRange(int.Parse(RedIn.Text), int.Parse(RedOut.Text));
                chf.Green = new AForge.IntRange(int.Parse(GreenIn.Text), int.Parse(GreenOut.Text));
                chf.Blue = new AForge.IntRange(int.Parse(BlueIn.Text), int.Parse(BlueOut.Text));

                // apply the filter
                System.Drawing.Bitmap tmp = chf.Apply((System.Drawing.Bitmap)mainPhoto.Clone());
                //BitmapImage tmpBmpIs = ToBitmapImage(tmp);

                /*RenderTargetBitmap rtb = new RenderTargetBitmap((int)Photography.ActualWidth, (int)Photography.ActualHeight, mainPhoto.HorizontalResolution, mainPhoto, PixelFormats.Pbgra32);
                rtb.Render(Photography);

                PngBitmapEncoder png = new PngBitmapEncoder();
                png.Frames.Add(BitmapFrame.Create(rtb));
                MemoryStream stream = new MemoryStream();
                png.Save(stream);
                System.Drawing.Bitmap image = (System.Drawing.Bitmap)System.Drawing.Image.FromStream(stream);*/

                Photography.Source = ToBitmapImage(tmp);

                UpdateHistograms(tmp);
                UpdateChannelPreviews(tmp);
            }
        }