Example #1
0
        public Bitmap GetNewFrame(Bitmap source)
        {
            var newFrame = convolution.Apply(source);

            source.Dispose();
            return(newFrame);
        }
Example #2
0
        /// <summary>
        /// Creates a comic rendered copy of the input image.
        /// </summary>
        public override Bitmap Render(Bitmap sourceImage)
        {
            // Converters
            GrayscaleY     convertGray  = new GrayscaleY();
            GrayscaleToRGB convertColor = new GrayscaleToRGB();

            // Convert grayscal images
            if (sourceImage.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                sourceImage = convertColor.Apply(sourceImage);
            }

            Bitmap comicImage = AForge.Imaging.Image.Clone(sourceImage);
            Bitmap edgeLayer  = null;
            Bitmap glowLayer  = null;

            // Glow for smooth colors
            GaussianBlur filterBlur = new GaussianBlur();

            filterBlur.Sigma = 2.0;
            filterBlur.Size  = 4;
            glowLayer        = filterBlur.Apply(comicImage);

            //SmartBlur filterBlur = new SmartBlur(10, 0.2);
            //glowLayer = filterBlur.Apply(comicImage);

            ContrastCorrection filterContrast = new ContrastCorrection(1 - (-this.Coloring * 0.1));

            filterContrast.ApplyInPlace(glowLayer);

            BrightnessCorrection filterBrightness = new BrightnessCorrection((-this.Coloring * 0.1) + 0.1);

            filterBrightness.ApplyInPlace(glowLayer);

            Screen blendScreen = new Screen(glowLayer);

            blendScreen.ApplyInPlace(comicImage);


            // Create a layer for edges
            Convolution filterConvolution = new Convolution(ConvolutionKernel);

            edgeLayer = filterConvolution.Apply(comicImage);

            // Convert to grayscale
            edgeLayer = convertGray.Apply(edgeLayer);

            // Threshold (edge thickness)
            Threshold filterThreshold = new Threshold((byte)(this.Edging * 255 / 100));

            filterThreshold.ApplyInPlace(edgeLayer);
            edgeLayer = convertColor.Apply(edgeLayer);

            // intersect comic with top layer (Darken blend)
            Intersect blendIntersect = new Intersect(edgeLayer);

            blendIntersect.ApplyInPlace(comicImage);

            return(comicImage);
        }
Example #3
0
        private void CameraOne_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap    bitmap1 = (Bitmap)(eventArgs.Frame.DeepClone());
            Grayscale filter  = new Grayscale(0.2, 0.7, 0.07);

            if (checkBox1.Checked)
            {
                bitmap1 = filter.Apply(bitmap1);
            }
            int x = 0;

            Invoke(new MethodInvoker(delegate
            {
                x = trackBar1.Value;
            }));
            RotateNearestNeighbor filterrot = new RotateNearestNeighbor(x, true);

            bitmap1 = filterrot.Apply(bitmap1);
            ResizeBicubic filterResizeBicubic = new ResizeBicubic(320, 240);

            bitmap1 = filterResizeBicubic.Apply(bitmap1);
            if (button1WasClicked)
            {
                Convolution conv = new Convolution(new[, ]
                {
                    { int.Parse(textBox1.Text), int.Parse(textBox2.Text), int.Parse(textBox3.Text) },
                    { int.Parse(textBox4.Text), int.Parse(textBox5.Text), int.Parse(textBox6.Text) },
                    { int.Parse(textBox7.Text), int.Parse(textBox8.Text), int.Parse(textBox9.Text) }
                });
                bitmap1 = conv.Apply(bitmap1);
            }
            pictureBox1.Image = bitmap1;
        }
Example #4
0
        public static Bitmap Convolution(Bitmap Imagem, int[,] kernel, int divisor)
        {
            Convolution filter = new Convolution(kernel, divisor);

            Imagem = Imagem.Clone(new Rectangle(0, 0, Imagem.Width, Imagem.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            Imagem = filter.Apply(Imagem);
            return(Imagem);
        }
Example #5
0
 public static Bitmap Emboss(Bitmap input)
 {
     //emboss efect kernel
     int[,] kernel = {
     { -2, -1,  0 },
     { -1,  1,  1 },
     {  0,  1,  2 } };
     Convolution convultionFilters = new Convolution(kernel);
     return convultionFilters.Apply(input);
 }
Example #6
0
        public static Bitmap Sketch(Bitmap input, int redThreshold, int blueThreshold, int greenThreshold)
        {
            int[,] kernel = {
                { 1, 2, 1, },  
               { 2, 4, 2, },   
               { 1, 2, 1, },
            };
            Convolution convultionFilters = new Convolution(kernel);
            Threshold thresholdFilter = new Threshold(redThreshold);
            input = convultionFilters.Apply(input);
            // extract red channel
            ExtractChannel extractFilter = new ExtractChannel(RGB.R);
            Bitmap channel = extractFilter.Apply(input);
            // threshold channel
            thresholdFilter.ApplyInPlace(channel);
            // put the channel back
            ReplaceChannel replaceFilter = new ReplaceChannel(RGB.R, channel);
            replaceFilter.ApplyInPlace(input);

            // extract blue channel
            extractFilter = new ExtractChannel(RGB.B);
            channel = extractFilter.Apply(input);
            // threshold channel
            thresholdFilter.ThresholdValue = blueThreshold;
            thresholdFilter.ApplyInPlace(channel);
            // put the channel back
            replaceFilter = new ReplaceChannel(RGB.B, channel);
            replaceFilter.ApplyInPlace(input);
            
            // extract green channel
            extractFilter = new ExtractChannel(RGB.G);
            channel = extractFilter.Apply(input);
            // threshold channel
            thresholdFilter.ThresholdValue = greenThreshold;
            thresholdFilter.ApplyInPlace(channel);
            // put the channel back
            replaceFilter = new ReplaceChannel(RGB.G, channel);
            replaceFilter.ApplyInPlace(input);

            return input;
        }
Example #7
0
        private void sharpness()
        {
            if (image1.Source != null)
            {
                Bitmap       bm = new Bitmap(currentFileName);
                BitmapSource bms;
                Convolution  filter;
                int[,] matrix = new int[3, 3];

                if (r2.IsChecked == true)
                {
                    matrix[0, 0] = Convert.ToInt16(t00.Text);
                    matrix[0, 1] = Convert.ToInt16(t01.Text);
                    matrix[0, 2] = Convert.ToInt16(t02.Text);
                    matrix[1, 0] = Convert.ToInt16(t10.Text);
                    matrix[1, 1] = Convert.ToInt16(t11.Text);
                    matrix[1, 2] = Convert.ToInt16(t12.Text);
                    matrix[2, 0] = Convert.ToInt16(t20.Text);
                    matrix[2, 1] = Convert.ToInt16(t21.Text);
                    matrix[2, 2] = Convert.ToInt16(t22.Text);

                    filter = new Convolution(matrix);
                    //bm = sharpen(bm, matrix, 3);
                    bm = filter.Apply(bm);
                }
                else
                {
                    filter = new Convolution(LoG);
                    //bm = sharpen(bm, LoG, 5);
                    bm = filter.Apply(bm);
                }

                bms           = BitmapToBitmapSource(bm);
                image1.Source = bms;
                backgroundWorker2.RunWorkerAsync(bm);
            }
        }
Example #8
0
        /// <summary>
        ///   Process the filter on the specified image.
        /// </summary>
        /// 
        /// <param name="sourceData">Source image data.</param>
        /// <param name="destinationData">Destination image data.</param>
        /// 
        protected unsafe override void ProcessFilter(UnmanagedImage sourceData, UnmanagedImage destinationData)
        {
            int width = sourceData.Width;
            int height = sourceData.Height;
            PixelFormat format = sourceData.PixelFormat;
            int pixelSize = System.Drawing.Bitmap.GetPixelFormatSize(format) / 8;

            sourceData.Clone();

            UnmanagedImage temp = UnmanagedImage.Create(width, height, format);

            int lineWidth = width * pixelSize;

            int srcStride = temp.Stride;
            int srcOffset = srcStride - lineWidth;
            int dstStride = destinationData.Stride;
            int dstOffset = dstStride - lineWidth;

            byte* srcStart = (byte*)temp.ImageData.ToPointer();
            byte* dstStart = (byte*)destinationData.ImageData.ToPointer();


            // first
            Convolution c = new Convolution(masks[0]);
            c.Apply(sourceData, destinationData);

            // others
            for (int i = 1; i < masks.Length; i++)
            {
                c.Kernel = masks[i];
                c.Apply(sourceData, temp);

                byte* src = srcStart;
                byte* dst = dstStart;

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < lineWidth; x++, src++, dst++)
                    {
                        if (*src > *dst)
                            *dst = *src;
                    }

                    dst += dstOffset;
                    src += srcOffset;
                }
            }
        }
        /// <summary>
        ///   Process the filter on the specified image.
        /// </summary>
        /// 
        /// <param name="sourceData">Source image data.</param>
        /// <param name="destinationData">Destination image data.</param>
        /// 
        protected override unsafe void ProcessFilter(UnmanagedImage sourceData, UnmanagedImage destinationData)
        {
            int width = sourceData.Width;
            int height = sourceData.Height;

            int pixelSize = System.Drawing.Image.GetPixelFormatSize(sourceData.PixelFormat) / 8;

            int srcStride = sourceData.Stride;
            int dstStride = destinationData.Stride;

            int srcOffset = srcStride - width * pixelSize;
            int dstOffset = dstStride - width * pixelSize;

            byte* src = (byte*)sourceData.ImageData.ToPointer();
            byte* dst = (byte*)destinationData.ImageData.ToPointer();

            // TODO: Move or cache the creation of those filters
            int[,] kernel = Accord.Math.Matrix.Create(radius * 2 + 1, radius * 2 + 1, 1);
            Convolution conv = new Convolution(kernel);
            FastVariance fv = new FastVariance(radius);

            // Mean filter
            UnmanagedImage mean = conv.Apply(sourceData);

            // Variance filter
            UnmanagedImage var = fv.Apply(sourceData);

            // do the processing job
            if (sourceData.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                byte* srcVar = (byte*)var.ImageData.ToPointer();
                byte* srcMean = (byte*)mean.ImageData.ToPointer();

                // Store maximum value from variance.
                int maxV = Max(width, height, srcVar, srcOffset);

                // Store minimum value from image.
                int minG = Min(width, height, src, srcOffset);

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++, src++, srcMean++, srcVar++, dst++)
                    {
                        double mP = *srcMean;
                        double vP = *srcVar;

                        double threshold = (mP + k * ((Math.Sqrt(vP) / (double)maxV - 1.0) * (mP - (double)minG)));

                        *dst = (byte)(*src > threshold ? 255 : 0);
                    }

                    src += srcOffset;
                    srcMean += srcOffset;
                    srcVar += srcOffset;
                    dst += dstOffset;
                }
            }
        }