【问题标题】:How to Use Calculated Color Values with ColorMatrix?如何使用 ColorMatrix 计算的颜色值?
【发布时间】:2010-06-16 17:25:22
【问题描述】:

我正在根据计算更改图像中每个像素的颜色值。问题是在我的机器上使用 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 的 M00、M11、M22 分别设置为 0、0、0,然后在 M40、M41、M42 中放入一个新值,但是该新值是根据值的乘法和加法计算得出的像素的分量(((0.21 * (Clr.R) + (0.72 * (Clr.G)) + (0.07 * (Clr.B)) 和它的结果 - Luminance - 乘以颜色分量)。

ColorMatrix 甚至可以做到这一点吗?

【问题讨论】:

    标签: .net image-processing gdi+ image-manipulation colormatrix


    【解决方案1】:

    没有。使用 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;
                    }
                }
            }
        }
    

    【讨论】:

    • 谢谢,我去看看。我正在使用 VB.NET,所以我了解编组在 VB 中的速度不如 C# 中的不安全锁定/解锁位。不过我会尝试让它运行。
    • 您可以将 C# 项目添加到您的 VB.Net 解决方案中,然后将这个单一的关键方法添加到其中并从您的 VB 代码中调用它。我猜你的速度会提高 30 倍左右。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多