我正在根据计算更改图像中每个像素的颜色值。问题是在我的机器上使用 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
吗?