Convolution filter.

The filter implements convolution operator, which calculates each pixel of the result image as weighted sum of the correspond pixel and its neighbors in the source image. The weights are set by convolution kernel. The weighted sum is divided by Divisor before putting it into result image and also may be thresholded using Threshold value.

Convolution is a simple mathematical operation which is fundamental to many common image processing filters. Depending on the type of provided kernel, the filter may produce different results, like blur image, sharpen it, find edges, etc.

The filter accepts 8 and 16 bpp grayscale images and 24, 32, 48 and 64 bpp color images for processing. Note: depending on the value of ProcessAlpha property, the alpha channel is either copied as is or processed with the kernel.

Sample usage:

// define emboss kernel int[,] kernel = { { -2, -1, 0 }, { -1, 1, 1 }, { 0, 1, 2 } }; // create filter Convolution filter = new Convolution( kernel ); // apply the filter filter.ApplyInPlace( image );

Initial image:

Result image:

Inheritance: BaseUsingCopyPartialFilter
Esempio n. 1
0
 // Update filter
 private void UpdateFilter( )
 {
     if (kernel != null)
     {
         try
         {
             filter           = new Convolution(kernel);
             filter.Threshold = int.Parse(thresholdBox.Text);
         }
         catch
         {
         }
     }
     filterPreview.Filter = filter;
 }
Esempio n. 2
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;
        }
Esempio n. 3
0
 /// <summary>
 /// Runs a 2dim conv with kernel, saves res in getIm() and sets null to all 
 /// other properties.
 /// </summary>
 public void conv2(int[,] kernel)
 {
     Convolution filter = new Convolution(kernel);
     filter.ApplyInPlace(_imGray);
 }
Esempio n. 4
0
        // Update filter
        private void UpdateFilter( )
        {
            if ( kernel != null )
            {
                try
                {
                    filter = new Convolution( kernel );
                    filter.Threshold = int.Parse( thresholdBox.Text );
                }
                catch
                {

                }
            }
            filterPreview.Filter = filter;
        }
Esempio n. 5
0
        /// <summary>
        /// runs a two dimentional convolution on im,
        /// where f is im and g is filter
        /// </summary>
        /// <param name="im">f(t)</param>
        /// <param name="kernel">g(u-t)</param>
        /// <returns>a new instance of ImageInfo</returns>
        public static ImageInfo conv2(ImageInfo im, int[,] kernel)
        {
            ImageInfo convIm = new ImageInfo(im.getIm());
            Convolution filter = new Convolution(kernel);
            filter.ApplyInPlace(convIm.getIm());

            return convIm;
        }