Pir*_*ica 4 vb.net performance bitmap processing-efficiency
目前,我正在使用该SetPixel()方法来更改位图中每个像素的颜色。这对于小尺寸的小图像效果很好,但是当我在大图像上测试它时,它确实需要一段时间。
我以前没有在 VB.Net 中处理过图像,所以我可能只是忽略了一些明显的东西。我这样做是为了制作一个将图像转换为灰度的程序。这会产生正确的结果,但速度较低,并且在此期间 UI 会冻结,因此我热衷于最大限度地提高转换速度。
这是我目前的代码:
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
Run Code Online (Sandbox Code Playgroud)
快速是一个相对术语,但这将在 10-12 毫秒内将 480x270 图像转换为灰度图像(显然取决于系统),这看起来并不太长。我很确定它会比 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
Run Code Online (Sandbox Code Playgroud)
值从 0.299、0.587、0.114 舍入
| 归档时间: |
|
| 查看次数: |
1563 次 |
| 最近记录: |