Binarization with thresholds matrix.

Idea of the filter is the same as idea of Threshold filter - change pixel value to white, if its intensity is equal or higher than threshold value, or to black otherwise. But instead of using single threshold value for all pixel, the filter uses matrix of threshold values. Processing image is divided to adjacent windows of matrix size each. For pixels binarization inside of each window, corresponding threshold values are used from specified threshold matrix.

The filter accepts 8 bpp grayscale images for processing.

Sample usage:

// create binarization matrix byte[,] matrix = new byte[4, 4] { { 95, 233, 127, 255 }, { 159, 31, 191, 63 }, { 111, 239, 79, 207 }, { 175, 47, 143, 15 } }; // create filter OrderedDithering filter = new OrderedDithering( matrix ); // apply the filter filter.ApplyInPlace( image );

Initial image:

Result image:

Inheritance: BaseInPlacePartialFilter
Ejemplo n.º 1
0
        public static Bitmap ApplyFiliter(ImageFiliter imgFilter, Bitmap bmp, byte Value, byte Value2)
        {
            Bitmap newImage = null;
            //ContrastCorrection filter2 = new ContrastCorrection(1.0);
            //newImage = filter2.Apply(bmp);
            if (imgFilter != ImageFiliter.None)
            {
                IFilter filter3 = Grayscale.CommonAlgorithms.Y;
                newImage = filter3.Apply(bmp);

                if (imgFilter == ImageFiliter.Threshold)
                {
                    IFilter filter = null;
                    if (Value == 0) filter = new Threshold();
                    else filter = new Threshold(Value);
                    newImage = filter.Apply(newImage);

                    //IterativeThreshold filter = new IterativeThreshold(Value2, Value);
                    //// apply the filter
                    // newImage = filter.Apply(newImage);
                }
                if (imgFilter == ImageFiliter.ThresholdWithCarry)
                {
                    IFilter filter = new ThresholdWithCarry();
                    newImage = filter.Apply(newImage);
                }
                else if (imgFilter == ImageFiliter.OrderedDithering)
                {
                    IFilter filter = new OrderedDithering();
                    newImage = filter.Apply(newImage);
                }
                else if (imgFilter == ImageFiliter.BayerDithering)
                {
                    IFilter filter = new BayerDithering();
                    newImage = filter.Apply(newImage);
                }
                else if (imgFilter == ImageFiliter.FloydSteinbergDithering)
                {
                    IFilter filter = new FloydSteinbergDithering();
                    newImage = filter.Apply(newImage);
                }
                else if (imgFilter == ImageFiliter.BurkesDithering)
                {
                    IFilter filter = new BurkesDithering();
                    newImage = filter.Apply(newImage);
                }
                else if (imgFilter == ImageFiliter.JarvisJudiceNinkeDithering)
                {
                    IFilter filter = new JarvisJudiceNinkeDithering();
                    newImage = filter.Apply(newImage);
                }
                else if (imgFilter == ImageFiliter.SierraDithering)
                {
                    IFilter filter = new SierraDithering();
                    newImage = filter.Apply(newImage);
                }
                else if (imgFilter == ImageFiliter.StuckiDithering)
                {
                    IFilter filter = new StuckiDithering();
                    newImage = filter.Apply(newImage);

                }
                else if (imgFilter == ImageFiliter.Convolution)
                {
                    // create filter
                    //OtsuThreshold filter = new OtsuThreshold();
                    //// apply the filter
                    //filter.ApplyInPlace(newImage);

                    //// create filter
                    //IterativeThreshold filter = new IterativeThreshold(0);
                    //// apply the filter
                    //newImage = filter.Apply(newImage);

                    int[,] kernel = {
                            { -2, -1,  0 },
                            { -1,  1,  1 },
                            {  0,  1,  2 }
                                };
                    // create filter
                    Convolution filter = new Convolution(kernel);
                    // apply the filter
                    filter.ApplyInPlace(newImage);
                }
                newImage = BitmapTo1Bpp(newImage);
            }
            else newImage = BitmapTo1Bpp(bmp);
            //轉換成 1bit bps
            return newImage;
        }