Contrast adjusting in RGB color space.

The filter operates in RGB color space and adjusts pixels' contrast value by increasing RGB values of bright pixel and decreasing RGB values of dark pixels (or vise versa if contrast needs to be decreased). The filter is based on LevelsLinear filter and simply sets all input ranges to (Factor, 255-Factor) and all output range to (0, 255) in the case if the factor value is positive. If the factor value is negative, then all input ranges are set to (0, 255 ) and all output ranges are set to (-Factor, 255_Factor).

See LevelsLinear documentation forr more information about the base filter.

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

Sample usage:

// create filter ContrastCorrection filter = new ContrastCorrection( 15 ); // apply the filter filter.ApplyInPlace( image );

Initial image:

Result image:

Inheritance: BaseInPlacePartialFilter
Exemple #1
2
        private Bitmap Flatten(Bitmap bmp, int fillint, int contint)
        {
            ColorFiltering colorFilter = new ColorFiltering();

            colorFilter.Red = new IntRange(0, fillint);
            colorFilter.Green = new IntRange(0, fillint);
            colorFilter.Blue = new IntRange(0, fillint);
            colorFilter.FillOutsideRange = false;
            using (Bitmap filteredBmp = colorFilter.Apply(bmp))
            {
                AForge.Imaging.Filters.ContrastCorrection Contrast = new ContrastCorrection(contint);
                AForge.Imaging.Filters.Invert invert = new Invert();
                AForge.Imaging.Filters.ExtractChannel extract_channel = new ExtractChannel(RGB.B);
                AForge.Imaging.Filters.Threshold thresh_hold = new Threshold(44);

                Contrast.ApplyInPlace(filteredBmp);

                Bitmap extractedBmp = extract_channel.Apply(filteredBmp);
                thresh_hold.ApplyInPlace(extractedBmp);
                invert.ApplyInPlace(extractedBmp);

                return extractedBmp;
            }
        }
Exemple #2
1
 private string reconhecerCaptcha(Image img)
 {
     Bitmap imagem = new Bitmap(img);
     imagem = imagem.Clone(new Rectangle(0, 0, img.Width, img.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
     Erosion erosion = new Erosion();
     Dilatation dilatation = new Dilatation();
     Invert inverter = new Invert();
     ColorFiltering cor = new ColorFiltering();
     cor.Blue = new AForge.IntRange(200, 255);
     cor.Red = new AForge.IntRange(200, 255);
     cor.Green = new AForge.IntRange(200, 255);
     Opening open = new Opening();
     BlobsFiltering bc = new BlobsFiltering();
     Closing close = new Closing();
     GaussianSharpen gs = new GaussianSharpen();
     ContrastCorrection cc = new ContrastCorrection();
     bc.MinHeight = 10;
     FiltersSequence seq = new FiltersSequence(gs, inverter, open, inverter, bc, inverter, open, cc, cor, bc, inverter);
     pictureBox.Image = seq.Apply(imagem);
     string reconhecido = OCR((Bitmap)pictureBox.Image);
     return reconhecido;
 }
		/// <summary>
		/// Adjust contrast in RGB colour space.
		/// </summary>
		/// <param name="bitmap">The bitmap.</param>
		public static Bitmap Contrast(this Bitmap bitmap) {
			if ((bitmap = bitmap.Channel()) != null) {
				var contrastCorrection = new ContrastCorrection();
				contrastCorrection.ApplyInPlace(bitmap);
			}
			return bitmap;
		}
 /// <summary>
 /// Adjust contrast in RGB colour space.
 /// </summary>
 public static Bitmap Contrast(this Bitmap Bitmap)
 {
     // Convert grayscale to RGB colour space.
     if ((Bitmap = Bitmap.Channel()) != null) {
         // Initialize a new instance of the ContrastCorrection class.
         var ContrastCorrection = new ContrastCorrection();
         // Apply the filter to the image.
         ContrastCorrection.ApplyInPlace(Bitmap);
     }
     // Return the bitmap.
     return Bitmap;
 }
 private static BitmapSource Correction(int f, Bitmap original)
 {
     var bitmap = new ContrastCorrection(f).Apply(new BrightnessCorrection(f).Apply(original));
     return bitmap.ToBitmapImage();
 }
Exemple #6
0
 internal void ReapplyFilters()
 {
     if (this.currentDicomElement != null)
     {
         Bitmap bmp = this.visibleImage.Clone() as Bitmap;
         if (!imageSettings.IsDefaultBrightness())
         {
             BrightnessCorrection filter = new BrightnessCorrection(imageSettings.Brightness);
             bmp = filter.Apply(bmp);
         }
         if (!imageSettings.IsDefaultContrast())
         {
             ContrastCorrection filter = new ContrastCorrection(imageSettings.Contrast);
             // apply filter
             bmp = filter.Apply(bmp);
         }
         previewBox.Image = bmp;
     }
 }
Exemple #7
0
        // Process image
        private void ProcessImage(Bitmap bitmap)
        {
            // create filter
            HistogramEqualization hisFilter = new HistogramEqualization();
            // process image
            //hisFilter.ApplyInPlace(Image);

            // create filter
            ContrastCorrection contrastFilter = new ContrastCorrection(4);
            // apply the filter
            contrastFilter.ApplyInPlace(bitmap);

            //QuantizeImage(bitmap);

            Grayscale grayscaleFilter = new Grayscale(0.33, 0.33, 0.34);
            // apply the filter
            Bitmap grayImage = grayscaleFilter.Apply(bitmap);

            // create filter
            SobelEdgeDetector sobelFilter = new SobelEdgeDetector();
            // apply the filter
            sobelFilter.ApplyInPlace(grayImage);
            Threshold filter = new Threshold(110);
            // apply the filter
            filter.ApplyInPlace(grayImage);

            if (GetIntersect(grayImage, bitmap, intersectPoint)) {
                //MessageBox.Show("draw");
                Graphics g = Graphics.FromImage(bitmap);
                //Sorting

                for (int i = 0; i < 19; i++)
                {
                    for (int j = 0; j < 19; j++)
                    {
                        for (int k = 0; k < 19 - 1; k++)
                        {
                            if (Math.Abs(intersectPoint[1, i, k]) > Math.Abs(intersectPoint[1, i, k + 1]))
                            {
                                int tempX = Math.Abs(intersectPoint[0, i, k]);
                                int tempY = Math.Abs(intersectPoint[1, i, k]);
                                intersectPoint[0, i, k] = Math.Abs(intersectPoint[0, i, k + 1]);
                                intersectPoint[1, i, k] = Math.Abs(intersectPoint[1, i, k + 1]);
                                intersectPoint[0, i, k + 1] = tempX;
                                intersectPoint[1, i, k + 1] = tempY;
                                //MessageBox.Show(i + " " + k +" " +tempX + " " + tempY + " " + intersectPoint[0, i, k] + " " + intersectPoint[1, i, k] ,"ok", MessageBoxButtons.OK);
                            }
                        }
                    }
                }

                for (int i = 0; i < 19; i++)
                {
                    for (int j = 0; j < 19; j++)
                    {
                        for (int k = 0; k < 19 - 1; k++)
                        {
                            if (Math.Abs(intersectPoint[0, k, i]) > Math.Abs(intersectPoint[0, k + 1, i]))
                            {
                                int tempX = Math.Abs(intersectPoint[0, k, i]);
                                int tempY = Math.Abs(intersectPoint[1, k, i]);
                                intersectPoint[0, k, i] = intersectPoint[0, k + 1, i];
                                intersectPoint[1, k, i] = intersectPoint[1, k + 1, i];
                                intersectPoint[0, k + 1, i] = tempX;
                                intersectPoint[1, k + 1, i] = tempY;
                            }
                        }
                    }
                }

                Pen redPen = new Pen(Color.Red, 4);
                for (int i = 0; i < 19; i++)
                {
                    for (int j = 0; j < 19; j++)
                    {
                        g.DrawEllipse(redPen, Math.Abs(intersectPoint[0, i, j]) , Math.Abs(intersectPoint[1, i, j]) , (int)5, (int)5);
                        //g.DrawEllipse(redPen, 0, 0, (int)5, (int)5);

                        //Debug.WriteLine((Math.Abs(intersectPoint[0, i, j]) - 2) + " " + (Math.Abs(intersectPoint[1, i, j]) - 2));
                    }
                }

                Pen greenPen = new Pen(Color.Green, 4);

                greenPen.Dispose();
                redPen.Dispose();
                g.Dispose();

                // Initializes the variables to pass to the MessageBox.Show method.
                EspacioCamera.Image = bitmap;
                string message = "Do you accecpt this detection?";
                string caption = "The system found totally 38 lines.";
                MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                DialogResult result;

                // Displays the MessageBox.

                result = MessageBox.Show(message, caption, buttons);

                if (result == System.Windows.Forms.DialogResult.Yes)
                {
                    // Closes the parent form.
                    locked = true;
                }
            }

            EspacioCamera.Image = bitmap;
        }
        /// <summary>
        /// Modifica el brillo y contraste de la imagen que aparece en pantalla
        /// </summary>
        private void Filtrar()
        {
            Bitmap temp = new Bitmap(padre.actual.datacuboHigh.dataCube[trackElementos.Value - 1].bmp);

            AForge.Imaging.Filters.BrightnessCorrection brillo = new AForge.Imaging.Filters.BrightnessCorrection(Convert.ToInt32(trackBrillo.Value));
            AForge.Imaging.Filters.ContrastCorrection contraste = new AForge.Imaging.Filters.ContrastCorrection(Convert.ToInt32(trackContraste.Value));

            contraste.ApplyInPlace(temp);
            brillo.ApplyInPlace(temp);

            pictElemento.Image = temp;

            //pictElemento.Invalidate();
        }
Exemple #9
0
        private void Contrast_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            ContrastLabel.Content = Contrast.Value;
            contrast = (int)Contrast.Value;

            if (mainPhoto != null)
            {
                cc = new ContrastCorrection(contrast);
                System.Drawing.Bitmap tmp = cc.Apply((System.Drawing.Bitmap)mainPhoto.Clone());
                BitmapImage tmpBmpIs = ToBitmapImage(tmp);

                Photography.Source = ToBitmapImage(tmp);

                UpdateHistograms(tmp);
                UpdateChannelPreviews(tmp);
            }
        }
Exemple #10
0
        public static bool ColorCorrections(string fileName, int brightness, int contrast, double gamma, int hue, float saturation)
        {
            if (String.IsNullOrEmpty(fileName)) throw new ArgumentNullException("fileName");

            bool result = false;

            using (var ms = new MemoryStream(File.ReadAllBytes(fileName)))
            {
                using (Bitmap bmp = new Bitmap(ms))
                {
                    //The filter accepts 8 bpp grayscale and 24/32 bpp color images for processing, value [-255, +255] default 10
                    if (brightness >= -255 && brightness != 0 && brightness <= 255)
                    {
                        BrightnessCorrection filter = new BrightnessCorrection(brightness);
                        filter.ApplyInPlace(bmp);
                        result = true;
                    }

                    //The filter accepts 8 bpp grayscale and 24/32 bpp color images for processing, value [-127, +127] default 10
                    if (contrast >= -127 && contrast != 0 && contrast <= 127)
                    {
                        ContrastCorrection filter = new ContrastCorrection(contrast);
                        filter.ApplyInPlace(bmp);
                        result = true;
                    }

                    //The filter accepts 8 bpp grayscale and 24 bpp color images for processing, value [0.1, 5.0] default 1.0
                    if (gamma >= 0.1D && gamma != 1D && gamma <= 5D)
                    {
                        GammaCorrection filter = new GammaCorrection(gamma);
                        filter.ApplyInPlace(bmp);
                        result = true;
                    }

                    //The filter accepts 8 bpp grayscale and 24/32 bpp color images for processing, value [-180, +180] default 0
                    if (hue >= -180 && hue != 0 && hue <= 180)
                    {
                        HueModifier filter = new HueModifier(hue);
                        filter.ApplyInPlace(bmp);
                        result = true;
                    }

                    //The filter accepts 24 and 32 bpp color images for processing, value specified percentage [-1, +1] default 0.1
                    if (saturation >= -100f && saturation != 0f && saturation <= 100f)
                    {
                        SaturationCorrection filter = new SaturationCorrection(saturation / 100);
                        filter.ApplyInPlace(bmp);
                        result = true;
                    }

                    bmp.Save(fileName);
                }
            }

            return result;
        }
Exemple #11
0
        public static BitmapSource ColorCorrections(BitmapSource source, int brightness, int contrast, double gamma, int hue, float saturation)
        {
            if (source == null) throw new ArgumentNullException("source");

            BitmapSource output = null;

            if (source.Format == PixelFormats.Bgr32 && gamma != 1D)
            {
                source = new FormatConvertedBitmap(source, PixelFormats.Bgr24, null, 0);
            }

            using (Bitmap bmp = Convert(source))
            {
                int bpp = source.Format.BitsPerPixel;

                if (bpp == 8 || bpp == 24 || bpp == 32)
                {
                    //The filter accepts 8 bpp grayscale and 24/32 bpp color images for processing, value [-255, +255] default 10
                    if (brightness >= -255 && brightness != 0 && brightness <= 255)
                    {
                        BrightnessCorrection filter = new BrightnessCorrection(brightness);
                        filter.ApplyInPlace(bmp);
                    }

                    //The filter accepts 8 bpp grayscale and 24/32 bpp color images for processing, value [-127, +127] default 10
                    if (contrast >= -127 && contrast != 0 && contrast <= 127)
                    {
                        ContrastCorrection filter = new ContrastCorrection(contrast);
                        filter.ApplyInPlace(bmp);
                    }
                }

                if (bpp == 8 || bpp == 24)
                {
                    //The filter accepts 8 bpp grayscale and 24 bpp color images for processing, value [0.1, 5.0] default 1.0
                    if (gamma >= 0.1D && gamma != 1D && gamma <= 5D)
                    {
                        GammaCorrection filter = new GammaCorrection(gamma);
                        filter.ApplyInPlace(bmp);
                    }
                }

                if (bpp == 8 || bpp == 24 || bpp == 32)
                {
                    //The filter accepts 8 bpp grayscale and 24/32 bpp color images for processing, value [-180, +180] default 0
                    if (hue >= -180 && hue != 0 && hue <= 180)
                    {
                        HueModifier filter = new HueModifier(hue);
                        filter.ApplyInPlace(bmp);
                    }
                }

                if (bpp == 24 || bpp == 32)
                {
                    //The filter accepts 24 and 32 bpp color images for processing, value specified percentage [-1, +1] default 0.1
                    if (saturation >= -100f && saturation != 0f && saturation <= 100f)
                    {
                        SaturationCorrection filter = new SaturationCorrection(saturation / 100);
                        filter.ApplyInPlace(bmp);
                    }
                }

                output = Convert(bmp);
            }

            return CloneImage(output);
        }
Exemple #12
0
        /// <summary>
        /// [Português]
        /// Método que realiza a subtração entre duas imagens.
        /// <para></para>
        /// [English]
        /// Method that performs the subtraction between two images.
        /// </summary>
        /// <param name="patientImg"></param>
        /// <param name="backgroundImg"></param>
        /// <returns></returns>
        public Bitmap ProcessImage(Bitmap patientImg, Bitmap backgroundImg)
        {
            //#if RELEASE
            backgroundImg.SetResolution(96, 96);
            patientImg.SetResolution(96, 96);

            var subtract = new AForge.Imaging.Filters.Subtract(backgroundImg);
            subtract.ApplyInPlace(patientImg);

            var contrast = new AForge.Imaging.Filters.ContrastCorrection(60);
            contrast.ApplyInPlace(patientImg);

            Grayscale escalaCinza = Grayscale.CommonAlgorithms.BT709;
            patientImg = escalaCinza.Apply(patientImg);

            Threshold threshold = new Threshold(10);
            threshold.ApplyInPlace(patientImg);
            return patientImg;
            //#endif
        }