Exemplo n.º 1
0
        private static Bitmap SaltPepperFilterProcess(Bitmap img, int m, int n, double filterOrder, SaltPepperfilterType spfiltType, bool unsharp)
        {
            Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb);

            List <ArraysListInt> ColorList = Helpers.GetPixels(img);
            var Rc = ColorList[0].Color;
            var Gc = ColorList[1].Color;
            var Bc = ColorList[2].Color;

            double[,] filter;
            int[,] resultR = new int[img.Height, img.Width];
            int[,] resultG = new int[img.Height, img.Width];
            int[,] resultB = new int[img.Height, img.Width];

            ArrGen <double> arrGen = new ArrGen <double>();
            double          Depth  = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat);

            if (m >= 1 && n >= 1)
            {
                switch (spfiltType)
                {
                //Arithmetic mean filtering.
                //help with salt noize
                case SaltPepperfilterType.amean:
                    filter = ImageFilter.FspecialSize(m, n, "average");

                    resultR = (ImageFilter.Filter_double(Rc, filter)).ArrayToUint8();
                    resultG = (ImageFilter.Filter_double(Gc, filter)).ArrayToUint8();
                    resultB = (ImageFilter.Filter_double(Bc, filter)).ArrayToUint8();
                    break;

                //Geometric mean filtering.
                //help with salt noize
                case SaltPepperfilterType.gmean:
                    filter = arrGen.ArrOfSingle(m, n, 1);

                    resultR = GmeanCount(Rc, filter, m, n);
                    resultG = GmeanCount(Gc, filter, m, n);
                    resultB = GmeanCount(Bc, filter, m, n);
                    break;

                //harmonic mean filter
                //help with salt noize
                case SaltPepperfilterType.hmean:
                    filter = arrGen.ArrOfSingle(m, n, 1);

                    resultR = HmeanCount(Rc, filter, m, n);
                    resultG = HmeanCount(Gc, filter, m, n);
                    resultB = HmeanCount(Bc, filter, m, n);
                    break;

                //contraharmonic mean filter Q>0 for pepper & <0 for salt
                case SaltPepperfilterType.chmean:
                    filter = arrGen.ArrOfSingle(m, n, 1);

                    resultR = CharmeanCount(Rc, filter, filterOrder);
                    resultG = CharmeanCount(Gc, filter, filterOrder);
                    resultB = CharmeanCount(Bc, filter, filterOrder);
                    break;

                default:
                    resultR = Rc; resultG = Gc; resultB = Bc;
                    break;
                }

                image = Helpers.SetPixels(image, resultR, resultG, resultB);

                if (unsharp)  //spfiltType == SaltPepperfilterType.chmean & unsharp
                {
                    image = Helpers.FastSharpImage(image);
                }

                if (Depth == 8)
                {
                    image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image);
                }
                if (Depth == 1)
                {
                    image = PixelFormatWorks.ImageTo1BppBitmap(image, 0.5);
                }
            }
            else
            {
                Console.WriteLine("m and n parameters must be positive geater or equal 1. Recommended 2 & 2 and higher. Method >SaltandPapperFilter<");
            }

            return(image);
        }
Exemplo n.º 2
0
        //image smoothing by entered size for average filter process
        private static Bitmap SmoothHelper(Bitmap img, int m, int n, SmoothInColorSpace cSpace)
        {
            Bitmap image = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb);
            double Depth = System.Drawing.Image.GetPixelFormatSize(img.PixelFormat);
            List <ArraysListInt> Result = new List <ArraysListInt>();

            double[,] filter;

            if (!Checks.BinaryInput(img))
            {
                List <ArraysListInt> ColorList = Helpers.GetPixels(img);

                if (m >= 1 && n >= 1)
                {
                    //create average filter by entered size
                    filter = ImageFilter.FspecialSize(m, n, "average");

                    //smooth in choosen color space
                    switch (cSpace)
                    {
                    case SmoothInColorSpace.RGB:
                        if (Depth == 8)
                        {
                            var bw = ImageFilter.Filter_double(ColorList[0].Color, filter).ArrayToUint8();
                            Result.Add(new ArraysListInt()
                            {
                                Color = bw
                            }); Result.Add(new ArraysListInt()
                            {
                                Color = bw
                            });
                            Result.Add(new ArraysListInt()
                            {
                                Color = bw
                            });
                        }
                        else
                        {
                            Result.Add(new ArraysListInt()
                            {
                                Color = ImageFilter.Filter_double(ColorList[0].Color, filter).ArrayToUint8()
                            });
                            Result.Add(new ArraysListInt()
                            {
                                Color = ImageFilter.Filter_double(ColorList[1].Color, filter).ArrayToUint8()
                            });
                            Result.Add(new ArraysListInt()
                            {
                                Color = ImageFilter.Filter_double(ColorList[2].Color, filter).ArrayToUint8()
                            });
                        }
                        break;

                    case SmoothInColorSpace.HSV:
                        var hsv      = RGBandHSV.RGB2HSV(img);
                        var hsv_temp = ImageFilter.Filter_double(hsv[2].Color, filter, PadType.replicate);

                        //Filter by V - Value (Brightness/яркость)
                        //artificially if V > 1; make him 1
                        Result = RGBandHSV.HSV2RGB(hsv[0].Color, hsv[1].Color, hsv_temp.ToBorderGreaterZero(1));
                        break;

                    case SmoothInColorSpace.Lab:
                        var lab      = RGBandLab.RGB2Lab(img);
                        var lab_temp = ImageFilter.Filter_double(lab[0].Color, filter, PadType.replicate);

                        //Filter by L - lightness
                        Result = RGBandLab.Lab2RGB(lab_temp.ToBorderGreaterZero(255), lab[1].Color, lab[2].Color);
                        break;

                    case SmoothInColorSpace.fakeCIE1976L:
                        var fakeCIE1976L      = RGBandLab.RGB2Lab1976(img);
                        var fakeCIE1976L_temp = ImageFilter.Filter_double(fakeCIE1976L[0].Color, filter, PadType.replicate);

                        //Filter by L - lightness
                        Result = RGBandLab.Lab1976toRGB(fakeCIE1976L_temp, fakeCIE1976L[1].Color, fakeCIE1976L[2].Color);
                        break;
                    }

                    image = Helpers.SetPixels(image, Result[0].Color, Result[1].Color, Result[2].Color);

                    if (Depth == 8)
                    {
                        image = PixelFormatWorks.Bpp24Gray2Gray8bppBitMap(image);
                    }
                }
                else
                {
                    Console.WriteLine("m and n parameters must be positive and greater or equal 1. Recommended 2 & 2 and higher. Method >Smooth<. Return black square.");
                }
            }
            else
            {
                Console.WriteLine("What did you expected to smooth binaty image? Return black square.");
            }

            return(image);
        }