2

我正在根据计算更改图像中每个像素的颜色值。问题是在我的机器上使用 1000x1333 图像需要超过 5 秒,我正在寻找一种方法来优化它以使其更快。

我认为ColorMatrix可能是一种选择,但我很难弄清楚如何获得一组像素 RGB 值,使用它来计算然后设置新的像素值。如果我只是用 ColorMatrix 修改(乘法、减法等)原始值,我可以看到如何做到这一点,但现在我可以如何使用像素返回值来计算新值。

例如:

Sub DarkenPicture()
    Dim clrTestFolderPath = "C:\Users\Me\Desktop\ColorTest\"
    Dim originalPicture = "original.jpg"
    Dim Luminance As Single
    Dim bitmapOriginal As Bitmap = Image.FromFile(clrTestFolderPath + originalPicture)
    Dim Clr As Color
    Dim newR As Byte
    Dim newG As Byte
    Dim newB As Byte
    For x = 0 To bitmapOriginal.Width - 1
        For y = 0 To bitmapOriginal.Height - 1
            Clr = bitmapOriginal.GetPixel(x, y)
            Luminance = ((0.21 * (Clr.R) + (0.72 * (Clr.G)) + (0.07 * (Clr.B))/ 255
            newR = Clr.R * Luminance
            newG = Clr.G * Luminance
            newB = Clr.B * Luminance
            bitmapOriginal.SetPixel(x, y, Color.FromArgb(newR, newG, newB))
        Next
    Next
    bitmapOriginal.Save(clrTestFolderPath + "colorized.jpg", ImageFormat.Jpeg)
End Sub

Luminance值是计算出来的。我知道我可以将ColorMatrix's M00、M11、M22 分别设置为 0、0、0,然后在 M40、M41、M42 中输入一个新值,但是该新值是基于该像素的乘积和相加计算得出的(((0.21 * (Clr.R) + (0.72 * (Clr.G)) + (0.07 * (Clr.B))并且结果 - Luminance- 乘以颜色分量)。

这甚至可能ColorMatrix吗?

4

1 回答 1

3

不可以。使用 ColorMatrix 无法使任何一种颜色分量与其自身或任何其他分量相乘。您只能将组件与常量相乘,然后将这些部分相加。

但是您可以做的是将其写入 C# 并使用 .LockBits 而不是 GetPixel/SetPixel。这将比使用 ColorMatrix 可能实现的更快。

更新:一些示例代码:

    private static void myVerySpecialSepia(
        IntPtr source,
        IntPtr destination,
        int height,
        int width,
        int sourceStride,
        int destinationStride,
        int sourceBytesPerPixel,
        int destinationBytesPerPixel )
    {
        unsafe
        {
            for ( int y = 0 ; y < height ; y++ )
            {
                byte* pOrig = (byte*)source.ToPointer() + sourceStride * y;
                byte* pDest = (byte*)destination.ToPointer() + destinationStride * y;
                for ( int x = width ; x > 0 ; x-- )
                {
                    float b = pOrig[0];
                    float g = pOrig[1];
                    float r = pOrig[2];
                    float b2 = b * b;
                    float g2 = g * g;
                    float r2 = r * r;
                    pDest[0] = (byte)(
                        b * 0.400367618f + b2 * 0.00011502471f +
                        g * (-0.0337239578f) + g2 * 0.00056673412f +
                        r * 0.221445322f + r2 * 0.0008506606f +
                        6.2766808485f);
                    pDest[1] = (byte)(
                        b * 0.493460029f + b2 * (-0.00023297003f) +
                        g * (-0.008577178f) + g2 * 0.00031247039f +
                        r * 0.5043012 + r2 * (-0.00006892065f) +
                        0.2746957206f);
                    pDest[2] = (byte)(
                        b * 0.617727f + b2 * (-0.00070876251f) +
                        g * 0.00271902746f + g2 * 0.00007401942f +
                        r * 0.6954346f + r2 * (-0.00065937551f) +
                        0.116103285f);
                    pOrig += sourceBytesPerPixel;
                    pDest += destinationBytesPerPixel;
                }
            }
        }
    }
于 2010-06-15T08:15:45.223 回答