我改变基于计算的图像中的每个像素的颜色值。问题是,这需要我的机器上超过5秒以1000x1333的图像,我正在寻找一种方式来优化它要快得多。

我觉得ColorMatrix可能是一种选择,但我有困难的时候搞清楚我将如何得到一组像素的RGB值,使用的是计算和再设置新的像素值。我可以看到如何可以做到这一点,如果我只是修改(相乘,相减等)嘉洛斯原始值,但是现在我如何可以使用像素返回值,用它来计算和新的价值。

例如:

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

有帮助吗?

解决方案

没有。使乘以与其自身或任何其它组件的颜色分量的任一种是不可能用嘉洛斯。可以ONY乘法与常量的组件,然后添加部分组合在一起。

但你的可以的做的是写入C#和使用.LockBits代替GetPixel / SetPixel这一点。这将是更快什么你可能有一个嘉洛斯实现。

<强>更新:一些示例代码:

    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;
                }
            }
        }
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top