0

我正在使用 Aforge.net HueModifier 和 SaturationCorrection 过滤器来更改图像颜色。我的问题是如何在这些滤镜旁边设置亮度以获得所需的颜色?

4

1 回答 1

0

我能够通过编写自定义 Aforge.net 过滤器来解决这个问题。过滤器采用 HSL 颜色值并将颜色色调应用于提供的图像。

protected override unsafe void ProcessFilter(UnmanagedImage image, Rectangle rect)
    {
        int pixelSize = Bitmap.GetPixelFormatSize(image.PixelFormat)/8;

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

        var rgb = new RGB();
        var hsl = new HSL();

        // do the job
        byte* ptr = (byte*) image.ImageData.ToPointer();

        // allign pointer to the first pixel to process
        ptr += (startY*image.Stride + startX*pixelSize);

        // for each row
        for (int y = startY; y < stopY; y++)
        {
            // for each pixel
            for (int x = startX; x < stopX; x++, ptr += pixelSize)
            {
                rgb.Red = ptr[RGB.R];
                rgb.Green = ptr[RGB.G];
                rgb.Blue = ptr[RGB.B];

                // convert to HSL
                HSL.FromRGB(rgb, hsl);

                // modify hsl values
                hsl.Hue = hue;
                hsl.Saturation = saturation;
                hsl.Luminance = Math.Min(0.97f, hsl.Luminance * (120 * luminance / 65));

                // convert back to RGB
                HSL.ToRGB(hsl, rgb);

                ptr[RGB.R] = (byte)rgb.Red;
                ptr[RGB.G] = (byte)rgb.Green;
                ptr[RGB.B] = (byte)rgb.Blue;
            }
            ptr += offset;
        }

    }
于 2013-11-13T23:22:08.817 回答