问题
At the moment, I am using the SetPixel()
method to change the colour of every pixel in a bitmap. This works fine on small images with small dimensions, but when I test it on large images it does take a while.
I haven't worked with images in VB.Net before, so I might just be overlooking something obvious. I'm doing this to make a program which converts an image to grey scale. This produces the right result but at a low speed, and during this time the UI freezes, so I'm keen to maximize the speed of conversion.
This is my code at the moment:
Dim tmpImg As New Bitmap(img) '"img" is a reference to the original image
For x As Integer = 0 To tmpImg.Width - 1
For y As Integer = 0 To tmpImg.Height - 1
Dim clr As Byte
With tmpImg.GetPixel(x, y)
clr = ConvertToGrey(.R, .G, .B)
End With
tmpImg.SetPixel(x, y, Color.FromArgb(clr, clr, clr))
Next
Next
Private Function ConvertToGrey(ByVal R As Byte, ByVal G As Byte, ByVal B As Byte) As Byte
Return (0.2126 * R) + (0.7152 * B) + (0.0722 * G)
End Function
回答1:
Fast is a relative term, but this will convert a 480x270 image to greyscale in 10-12 ms (obviously system dependent) which does not seem unduly long. I'm quite sure it will be faster than SetPixel.
Private Function GrayedImage(orgBMP As Bitmap) As Bitmap
Dim grayscale As New Imaging.ColorMatrix(New Single()() _
{New Single() {0.3, 0.3, 0.3, 0, 0},
New Single() {0.59, 0.59, 0.59, 0, 0},
New Single() {0.11, 0.11, 0.11, 0, 0},
New Single() {0, 0, 0, 1, 0},
New Single() {0, 0, 0, 0, 1}})
Dim bmpTemp As New Bitmap(orgBMP)
Dim grayattr As New Imaging.ImageAttributes()
grayattr.SetColorMatrix(grayscale)
Using g As Graphics = Graphics.FromImage(bmpTemp)
g.DrawImage(bmpTemp, New Rectangle(0, 0, bmpTemp.Width, bmpTemp.Height),
0, 0, bmpTemp.Width, bmpTemp.Height,
GraphicsUnit.Pixel, grayattr)
End Using
Return bmpTemp
End Function
Values are rounded from .299, .587, .114
来源:https://stackoverflow.com/questions/23595258/is-there-a-faster-method-to-convert-bitmap-pixels-to-greyscale