Linear correction of RGB channels
Inheritance: IFilter, IInPlaceFilter
        public void ApplyInPlace(Bitmap bmp)
        {
            if (Invert)
            {
                Invert invertF = new Invert();
                invertF.ApplyInPlace(bmp);
            }

            Color c = Scale(DarkestColor, BrightestColor, Brightness); ; 
            LevelsLinear levelsF = new LevelsLinear();

            if (Invert)
            {
                levelsF.Input = new IntRange(0, (int)(255 * TextDarken));
            }
            else
            {
                levelsF.Input = new IntRange(255 - (int)(255 * TextDarken), 255);
            }

            levelsF.OutRed = new IntRange(0, c.R);
            levelsF.OutGreen = new IntRange(0, c.G);
            levelsF.OutBlue = new IntRange(0, c.B);

            levelsF.ApplyInPlace(bmp);
        }
 /// <summary>
 /// Linear correction in RGB colour space.
 /// </summary>
 /// <param name="bitmap">The bitmap.</param>
 public static Bitmap Colour(this Bitmap bitmap) {
     if ((bitmap = bitmap.Channel()) == null) return null;
     var imageStatistics = new ImageStatistics(bitmap);
     var levelsLinear = new LevelsLinear {
         InRed = imageStatistics.Red.GetRange(0.995),
         InGreen = imageStatistics.Green.GetRange(0.995),
         InBlue = imageStatistics.Blue.GetRange(0.995)
     };
     levelsLinear.ApplyInPlace(bitmap);
     return bitmap;
 }
Esempio n. 3
0
		static void TestLevels()
		{
			var bmp1 = (Bitmap)Bitmap.FromFile("meta1.png");
			var bmp2 = (Bitmap)Bitmap.FromFile("meta2.png");

			LevelsLinear filter = new LevelsLinear();
			// set ranges
			filter.Input = new IntRange(150, 255);
			// apply the filter
			var sw = Stopwatch.StartNew();
			filter.ApplyInPlace(bmp2);
			sw.Stop();

			var contrast = new ContrastStretch();
			var bmp4 = contrast.Apply(bmp2);

			var bmp3 = ImageUtil.AdjustImage(bmp2, contrast: 2f);
		}
 /// <summary>
 /// Linear correction in RGB colour space.
 /// </summary>
 /// <param name="Bitmap">The bitmap.</param>
 public static Bitmap Colour(this Bitmap Bitmap)
 {
     // Convert grayscale to RGB colour space.
     if ((Bitmap = Bitmap.Channel()) != null) {
         // Initialize a new instance of the LevelsLinear class.
         ImageStatistics ImageStatistics = new ImageStatistics(Bitmap);
         // Initialize a new instance of the LevelsLinear class.
         LevelsLinear LevelsLinear = new LevelsLinear {
             // Retrieve and set the range around the median for the red-channel.
             InRed = ImageStatistics.Red.GetRange(0.995),
             // Retrieve and set the range around the median for the green-channel.
             InGreen = ImageStatistics.Green.GetRange(0.995),
             // Retrieve and set the range around the median for the blue-channel.
             InBlue = ImageStatistics.Blue.GetRange(0.995)
         };
         // Apply the filter to the image.
         LevelsLinear.ApplyInPlace(Bitmap);
     }
     // Return the bitmap.
     return Bitmap;
 }
        public double GetTemperature()
        {
            var temp = 0.0;

            var image = Image.FromFile(filename);

            var grayscale = new Grayscale(0.2125, 0.7154, 0.0721);
            image = grayscale.Apply(image);

            var invert = new Invert();
            image = invert.Apply(image);

            var stats = new ImageStatistics(image);
            var levelsLinear = new LevelsLinear
            {
                InGray = stats.Gray.GetRange(2.90)
            };

            image = levelsLinear.Apply(image);

            var contrast = new ContrastStretch();
            image = contrast.Apply(image);

            var erosion = new Erosion();
            image = erosion.Apply(image);

            var blur = new GaussianBlur(2, 3);
            image = blur.Apply(image);

            var threshold = new Threshold(79);
            image = threshold.Apply(image);

            image.Save(processedFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
            image.Dispose();
            var text = Recognise();

            double.TryParse(text.Replace(',', '.'), out temp);

            return temp;
        }
Esempio n. 6
0
 private void CorrectLevel(int MinLevel, int MaxLevel)
 {
     LevelsLinear filter = new LevelsLinear();
     filter.Input = new IntRange(MinLevel, MaxLevel);
     filter.ApplyInPlace(_recogimg);
 }
Esempio n. 7
0
        private Bitmap filter(string ft)
        {
            Bitmap effect = sourceImage;

            filteredImage = sourceImage;

            if (ft == "none")
            {
                effect = sourceImage;
            }
            else if (ft == "grayscale")
            {
                effect = ApplyFilter(Grayscale.CommonAlgorithms.BT709);
            }
            else if (ft == "sepia")
            {
                effect = ApplyFilter(new Sepia());
            }
            else if (ft == "invert")
            {
                effect = ApplyFilter(new Invert());
            }
            else if (ft == "rotate")
            {
                effect = ApplyFilter(new RotateChannels());
            }
            else if (ft == "color")
            {
                effect = ApplyFilter(new ColorFiltering(new IntRange(25, 230), new IntRange(25, 230), new IntRange(25, 230)));
            }
            else if (ft == "hue")
            {
                effect = ApplyFilter(new HueModifier(50));
            }
            else if (ft == "saturation")
            {
                effect = ApplyFilter(new SaturationCorrection(0.15f));
            }
            else if (ft == "brightness")
            {
                effect = ApplyFilter(new BrightnessCorrection());
            }
            else if (ft == "contrast")
            {
                effect = ApplyFilter(new ContrastCorrection());
            }
            else if (ft == "hsl")
            {
                effect = ApplyFilter(new HSLFiltering(new IntRange(330, 30), new Range(0, 1), new Range(0, 1)));
            }
            else if (ft == "YCbCr")
            {
                YCbCrLinear f = new YCbCrLinear();
                f.InCb = new Range(-0.3f, 0.3f);
                effect = ApplyFilter(f);
            }
            else if (ft == "YCbCr")
            {
                effect = ApplyFilter(new YCbCrFiltering(new Range(0.2f, 0.9f), new Range(-0.3f, 0.3f), new Range(-0.3f, 0.3f)));
            }
            else if (ft == "threshold")
            {
                Bitmap originalImage = sourceImage;
                // get grayscale image
                sourceImage = Grayscale.CommonAlgorithms.RMY.Apply(sourceImage);
                // apply threshold filter
                effect = ApplyFilter(new Threshold());
                // delete grayscale image and restore original
                sourceImage.Dispose();
                sourceImage = originalImage;
            }
            else if (ft == "floyd")
            {
                Bitmap originalImage = sourceImage;
                // get grayscale image
                sourceImage = Grayscale.CommonAlgorithms.RMY.Apply(sourceImage);
                // apply threshold filter
                effect = ApplyFilter(new FloydSteinbergDithering());
                // delete grayscale image and restore original
                sourceImage.Dispose();
                sourceImage = originalImage;
            }
            else if (ft == "ordered")
            {
                // save original image
                Bitmap originalImage = sourceImage;
                // get grayscale image
                sourceImage = Grayscale.CommonAlgorithms.RMY.Apply(sourceImage);
                // apply threshold filter
                effect = ApplyFilter(new OrderedDithering());
                // delete grayscale image and restore original
                sourceImage.Dispose();
                sourceImage = originalImage;
            }
            else if (ft == "correlation")
            {
                effect = ApplyFilter(new Convolution(new int[,] {
                                { 1, 2, 3, 2, 1 },
                                { 2, 4, 5, 4, 2 },
                                { 3, 5, 6, 5, 3 },
                                { 2, 4, 5, 4, 2 },
                                { 1, 2, 3, 2, 1 } }));
            }
            else if (ft == "sharpen")
            {
                effect = ApplyFilter(new Sharpen());
            }
            else if (ft == "edgedetector")
            {
                // save original image
                Bitmap originalImage = sourceImage;
                // get grayscale image
                sourceImage = Grayscale.CommonAlgorithms.RMY.Apply(sourceImage);
                // apply edge filter
                effect = ApplyFilter(new DifferenceEdgeDetector());
                // delete grayscale image and restore original
                sourceImage.Dispose();
                sourceImage = originalImage;
            }
            else if (ft == "homogenity")
            {
                // save original image
                Bitmap originalImage = sourceImage;
                // get grayscale image
                sourceImage = Grayscale.CommonAlgorithms.RMY.Apply(sourceImage);
                // apply edge filter
                effect = ApplyFilter(new HomogenityEdgeDetector());
                // delete grayscale image and restore original
                sourceImage.Dispose();
                sourceImage = originalImage;
            }
            else if (ft == "sobel")
            {
                // save original image
                Bitmap originalImage = sourceImage;
                // get grayscale image
                sourceImage = Grayscale.CommonAlgorithms.RMY.Apply(sourceImage);
                // apply edge filter
                effect = ApplyFilter(new SobelEdgeDetector());
                // delete grayscale image and restore original
                sourceImage.Dispose();
                sourceImage = originalImage;
            }
            else if (ft == "rgbLinear")
            {
                LevelsLinear f = new LevelsLinear();

                f.InRed = new IntRange(30, 230);
                f.InGreen = new IntRange(50, 240);
                f.InBlue = new IntRange(10, 210);

                effect = ApplyFilter(f);
            }
            else if (ft == "jitter")
            {
                effect = ApplyFilter(new Jitter());
            }
            else if (ft == "oilpainting")
            {
                effect = ApplyFilter(new OilPainting());
            }
            else if (ft == "gaussinblur")
            {
                effect = ApplyFilter(new GaussianBlur(2.0, 7));
            }
            else if (ft == "texture")
            {
                effect = ApplyFilter(new Texturer(new TextileTexture(), 1.0, 0.8));
            }

            return effect;
        }
Esempio n. 8
0
        // On Filters->Levels Linear Correction
        private void rgbLinearFiltersItem_Click( object sender, System.EventArgs e )
        {
            LevelsLinear filter = new LevelsLinear( );

            filter.InRed = new IntRange( 30, 230 );
            filter.InGreen = new IntRange( 50, 240 );
            filter.InBlue = new IntRange( 10, 210 );

            ApplyFilter( filter );
            rgbLinearFiltersItem.Checked = true;
        }
        /// <summary>
        /// Process the filter on the specified image.
        /// </summary>
        /// 
        /// <param name="image">Source image data.</param>
        /// <param name="rect">Image rectangle for processing by the filter.</param>
        ///
        protected override unsafe void ProcessFilter( UnmanagedImage image, Rectangle rect )
        {
            int pixelSize = Image.GetPixelFormatSize( image.PixelFormat ) / 8;

            int startX = rect.Left;
            int startY = rect.Top;
            int stopX  = startX + rect.Width;
            int stopY  = startY + rect.Height;
            int stride = image.Stride;
            int offset = stride - rect.Width * pixelSize;

            // levels linear correction filter is going to be used on STEP 2
            LevelsLinear levelsLinear = new LevelsLinear( );

            // STEP 1 - search for min and max pixel values
            byte* ptr = (byte*) image.ImageData.ToPointer( );

            // check image format
            if ( image.PixelFormat == PixelFormat.Format8bppIndexed )
            {
                // allign pointer to the first pixel to process
                ptr += ( startY * stride + startX );

                byte min = 255;
                byte max = 0;

                for ( int y = startY; y < stopY; y++ )
                {
                    for ( int x = startX; x < stopX; x++, ptr++ )
                    {
                        byte value = *ptr;

                        if ( value < min )
                            min = value;

                        if ( value > max )
                            max = value;
                    }
                    ptr += offset;
                }

                levelsLinear.InGray = new IntRange( min, max );
            }
            else
            {
                // allign pointer to the first pixel to process
                ptr += ( startY * stride + startX * pixelSize );

                byte minR = 255, minG = 255, minB = 255;
                byte maxR = 0,   maxG = 0,   maxB = 0;

                for ( int y = startY; y < stopY; y++ )
                {
                    for ( int x = startX; x < stopX; x++, ptr += pixelSize )
                    {
                        // red
                        byte value = ptr[RGB.R];

                        if ( value < minR )
                            minR = value;

                        if ( value > maxR )
                            maxR = value;

                        // green
                        value = ptr[RGB.G];

                        if ( value < minG )
                            minG = value;

                        if ( value > maxG )
                            maxG = value;

                        // blue
                        value = ptr[RGB.B];

                        if ( value < minB )
                            minB = value;

                        if ( value > maxB )
                            maxB = value;
                    }
                    ptr += offset;
                }

                levelsLinear.InRed   = new IntRange( minR, maxR );
                levelsLinear.InGreen = new IntRange( minG, maxG );
                levelsLinear.InBlue  = new IntRange( minB, maxB );
            }

            // STEP 2 - run levels linear correction
            levelsLinear.ApplyInPlace( image, rect );
        }
Esempio n. 10
0
        /// <summary>
        /// Process the filter on the specified image.
        /// </summary>
        ///
        /// <param name="image">Source image data.</param>
        /// <param name="rect">Image rectangle for processing by the filter.</param>
        ///
        protected override unsafe void ProcessFilter(UnmanagedImage image, Rectangle rect)
        {
            int pixelSize = Image.GetPixelFormatSize(image.PixelFormat) / 8;

            int startX = rect.Left;
            int startY = rect.Top;
            int stopX  = startX + rect.Width;
            int stopY  = startY + rect.Height;
            int stride = image.Stride;
            int offset = stride - rect.Width * pixelSize;

            // levels linear correction filter is going to be used on STEP 2
            LevelsLinear levelsLinear = new LevelsLinear( );

            // STEP 1 - search for min and max pixel values
            byte *ptr = (byte *)image.ImageData.ToPointer( );

            // check image format
            if (image.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                // allign pointer to the first pixel to process
                ptr += (startY * stride + startX);

                byte min = 255;
                byte max = 0;

                for (int y = startY; y < stopY; y++)
                {
                    for (int x = startX; x < stopX; x++, ptr++)
                    {
                        byte value = *ptr;

                        if (value < min)
                        {
                            min = value;
                        }

                        if (value > max)
                        {
                            max = value;
                        }
                    }
                    ptr += offset;
                }

                levelsLinear.InGray = new IntRange(min, max);
            }
            else
            {
                // allign pointer to the first pixel to process
                ptr += (startY * stride + startX * pixelSize);

                byte minR = 255, minG = 255, minB = 255;
                byte maxR = 0, maxG = 0, maxB = 0;

                for (int y = startY; y < stopY; y++)
                {
                    for (int x = startX; x < stopX; x++, ptr += pixelSize)
                    {
                        // red
                        byte value = ptr[RGB.R];

                        if (value < minR)
                        {
                            minR = value;
                        }

                        if (value > maxR)
                        {
                            maxR = value;
                        }

                        // green
                        value = ptr[RGB.G];

                        if (value < minG)
                        {
                            minG = value;
                        }

                        if (value > maxG)
                        {
                            maxG = value;
                        }

                        // blue
                        value = ptr[RGB.B];

                        if (value < minB)
                        {
                            minB = value;
                        }

                        if (value > maxB)
                        {
                            maxB = value;
                        }
                    }
                    ptr += offset;
                }

                levelsLinear.InRed   = new IntRange(minR, maxR);
                levelsLinear.InGreen = new IntRange(minG, maxG);
                levelsLinear.InBlue  = new IntRange(minB, maxB);
            }

            // STEP 2 - run levels linear correction
            levelsLinear.ApplyInPlace(image, rect);
        }
Esempio n. 11
0
        protected unsafe override void ProcessFilter(UnmanagedImage image, Rectangle rect)
        {
            int          num          = System.Drawing.Image.GetPixelFormatSize(image.PixelFormat) / 8;
            int          left         = rect.Left;
            int          top          = rect.Top;
            int          num2         = left + rect.Width;
            int          num3         = top + rect.Height;
            int          stride       = image.Stride;
            int          num4         = stride - rect.Width * num;
            LevelsLinear levelsLinear = new LevelsLinear();
            byte *       ptr          = (byte *)image.ImageData.ToPointer();

            if (image.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                ptr += top * stride + left;
                byte b  = byte.MaxValue;
                byte b2 = 0;
                for (int i = top; i < num3; i++)
                {
                    int num5 = left;
                    while (num5 < num2)
                    {
                        byte b3 = *ptr;
                        if (b3 < b)
                        {
                            b = b3;
                        }
                        if (b3 > b2)
                        {
                            b2 = b3;
                        }
                        num5++;
                        ptr++;
                    }
                    ptr += num4;
                }
                levelsLinear.InGray = new IntRange(b, b2);
            }
            else
            {
                ptr += top * stride + left * num;
                byte b4 = byte.MaxValue;
                byte b5 = byte.MaxValue;
                byte b6 = byte.MaxValue;
                byte b7 = 0;
                byte b8 = 0;
                byte b9 = 0;
                for (int j = top; j < num3; j++)
                {
                    int num6 = left;
                    while (num6 < num2)
                    {
                        byte b10 = ptr[2];
                        if (b10 < b4)
                        {
                            b4 = b10;
                        }
                        if (b10 > b7)
                        {
                            b7 = b10;
                        }
                        b10 = ptr[1];
                        if (b10 < b5)
                        {
                            b5 = b10;
                        }
                        if (b10 > b8)
                        {
                            b8 = b10;
                        }
                        b10 = *ptr;
                        if (b10 < b6)
                        {
                            b6 = b10;
                        }
                        if (b10 > b9)
                        {
                            b9 = b10;
                        }
                        num6++;
                        ptr += num;
                    }
                    ptr += num4;
                }
                levelsLinear.InRed   = new IntRange(b4, b7);
                levelsLinear.InGreen = new IntRange(b5, b8);
                levelsLinear.InBlue  = new IntRange(b6, b9);
            }
            levelsLinear.ApplyInPlace(image, rect);
        }
        protected override unsafe void ProcessFilter(UnmanagedImage image, Rectangle rect)
        {
            int          num    = Image.GetPixelFormatSize(image.PixelFormat) / 8;
            int          left   = rect.Left;
            int          top    = rect.Top;
            int          num4   = left + rect.Width;
            int          num5   = top + rect.Height;
            int          stride = image.Stride;
            int          num7   = stride - (rect.Width * num);
            LevelsLinear linear = new LevelsLinear();
            byte *       numPtr = (byte *)image.ImageData.ToPointer();

            if (image.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                numPtr += (top * stride) + left;
                byte min = 0xff;
                byte max = 0;
                for (int i = top; i < num5; i++)
                {
                    int num11 = left;
                    while (num11 < num4)
                    {
                        byte num12 = numPtr[0];
                        if (num12 < min)
                        {
                            min = num12;
                        }
                        if (num12 > max)
                        {
                            max = num12;
                        }
                        num11++;
                        numPtr++;
                    }
                    numPtr += num7;
                }
                linear.InGray = new IntRange(min, max);
            }
            else
            {
                numPtr += (top * stride) + (left * num);
                byte num13 = 0xff;
                byte num14 = 0xff;
                byte num15 = 0xff;
                byte num16 = 0;
                byte num17 = 0;
                byte num18 = 0;
                for (int j = top; j < num5; j++)
                {
                    int num20 = left;
                    while (num20 < num4)
                    {
                        byte num21 = numPtr[2];
                        if (num21 < num13)
                        {
                            num13 = num21;
                        }
                        if (num21 > num16)
                        {
                            num16 = num21;
                        }
                        num21 = numPtr[1];
                        if (num21 < num14)
                        {
                            num14 = num21;
                        }
                        if (num21 > num17)
                        {
                            num17 = num21;
                        }
                        num21 = numPtr[0];
                        if (num21 < num15)
                        {
                            num15 = num21;
                        }
                        if (num21 > num18)
                        {
                            num18 = num21;
                        }
                        num20++;
                        numPtr += num;
                    }
                    numPtr += num7;
                }
                linear.InRed   = new IntRange(num13, num16);
                linear.InGreen = new IntRange(num14, num17);
                linear.InBlue  = new IntRange(num15, num18);
            }
            linear.ApplyInPlace(image, rect);
        }