Linear correction of RGB channels.

The filter performs linear correction of RGB channels by mapping specified channels' input ranges to output ranges. It is similar to the ColorRemapping, but the remapping is linear.

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

Sample usage:

// create filter LevelsLinear filter = new LevelsLinear( ); // set ranges filter.InRed = new IntRange( 30, 230 ); filter.InGreen = new IntRange( 50, 240 ); filter.InBlue = new IntRange( 10, 210 ); // apply the filter filter.ApplyInPlace( image );

Initial image:

Result image:

Inheritance: BaseInPlacePartialFilter
Beispiel #1
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);
        }