sha*_*rgy 4 .net vb.net graphics gdi+ image
我剪掉了部分图像,并通过 12 个轨迹栏定义了 2 个颜色范围 (H/S/L)。我还有一个“精度/速度”滑块,范围从 1 到 10。
我需要分析图像的多少像素属于每个指定的颜色范围。
根据精度/速度滑块,我跳过一些行/像素。
它工作得很好,但太慢了。高精度(轨迹条值 = 1)时,大约需要 550 ms。
精度低但速度快(轨迹条值 = 10),大约需要 5 毫秒。
有没有办法加快这段代码的速度?理想情况下我需要它快 5 倍。
For y As Integer = 0 To 395
If y Mod 2 = 0 Then
startpixel = tbval / 2
Else
startpixel = 0
End If
If y Mod tbval = 0 Then
For x As Integer = 0 To 1370
If x Mod tbval - startpixel = 0 Then
analyzedpixels = analyzedpixels + 1
Dim pColor As Color = crop.GetPixel(x, y)
Dim h As Integer = pColor.GetHue
Dim s As Integer = pColor.GetSaturation * 100
Dim l As Integer = pColor.GetBrightness * 100
'verify if it is part of the first color
If h >= h1min And h <= h1max And s >= s1min And s <= s1max And l >= l1min And l <= l1max Then
color1pixels = color1pixels + 1
End If
If h >= h2min And h <= h2max And s >= s2min And s <= s2max And l >= l2min And l <= l2max Then
color2pixels = color2pixels + 1
End If
End If
Next
End If
Next
Run Code Online (Sandbox Code Playgroud)
编辑:
这是工作代码..
Dim rect As New Rectangle(0, 0, crop.Width, crop.Height)
Dim bdata As Imaging.BitmapData = crop.LockBits(rect, Imaging.ImageLockMode.ReadOnly, crop.PixelFormat)
Dim ptr As IntPtr = bdata.Scan0
Dim bytes As Integer = Math.Abs(bdata.Stride) * crop.Height
Dim rgbValues As Byte() = New Byte(bytes - 1) {}
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)
For i As Integer = 0 To crop.Height - 1
If i Mod 2 = 0 Then
startpixel = tbval / 2
Else
startpixel = 0
End If
If i Mod tbval = 0 Then
For j As Integer = 0 To crop.Width - 1
If j Mod tbval - startpixel = 0 Then
analyzedpixels = analyzedpixels + 1
Dim position = (bdata.Stride * i) + j * 4
Dim c = Color.FromArgb(BitConverter.ToInt32(rgbValues, position))
Dim h As Integer = c.GetHue
Dim s As Integer = c.GetSaturation * 100
Dim l As Integer = c.GetBrightness * 100
If h >= h1min And h <= h1max And s >= s1min And s <= s1max And l >= l1min And l <= l1max Then
color1pixels = color1pixels + 1
End If
If h >= h2min And h <= h2max And s >= s2min And s <= s2max And l >= l2min And l <= l2max Then
color2pixels = color2pixels + 1
End If
End If
stride += 4
Next
End If
Next
crop.UnlockBits(bdata)
Run Code Online (Sandbox Code Playgroud)
当对位图的颜色数据执行顺序操作时,Bitmap.LockBits方法可以显着提高性能,因为位图数据只需在内存中加载一次,这与顺序 GetPixel/SetPixel 调用不同:每次调用都会加载一个内存中的 Bitmap 数据的部分部分然后将其丢弃,以在再次调用这些方法时重复该过程。
如果需要对 GetPixel/SetPixel 进行一次调用,这些方法可能比Bitmap.LockBits(). 但是,在这种情况下,实际上性能并不是一个因素。
Bitmap.LockBits()工作原理:
这是函数调用:
public BitmapData LockBits (Rectangle rect, ImageLockMode flags, PixelFormat format);
// VB.Net
Public LockBits (rect As Rectangle, flags As ImageLockMode, format As PixelFormat) As BitmapData
Run Code Online (Sandbox Code Playgroud)
rect As Rectangle:该参数指定我们感兴趣的Bitmap数据部分;该部分的字节将被加载到内存中。它可以是位图的整个大小,也可以是位图的较小部分。
flags As ImageLockMode:指定要执行的锁定类型。对存储器的访问可以限制为读或写,或者允许并发读/写操作。
它还可用于指定(设置ImageLockMode.UserInputBuffer)BitmapData对象由调用代码提供。
该BitmapData对象定义了一些位图属性(Width以及Height位图的扫描线宽度(:构成单行像素的字节数,由乘以每个像素的字节数Stride表示,四舍五入为 4-字节边界。请参阅有关)Bitmap.Width的注释
。BitmapData.Scan0属性是指向存储位图数据的初始内存位置的指针 ( )。
此属性允许指定预先存在的位图数据缓冲区已存在的内存位置存储。当使用指针在进程之间交换位图数据时,它变得有用。
请注意,MSDN 文档令人困惑(如果不是错误的话)。 StrideIntPtrImageLockMode.UserInputBuffer
format As PixelFormat:用于描述单个像素颜色的格式。实际上,它转换为用于表示颜色的字节数。PixelFormat = Format24bppRgb,每个颜色由 3 个字节(RGB 值)表示。对于PixelFormat.Format32bppArgb,每个颜色由 4 个字节表示(RGB 值 + Alpha)。Format8bppIndexed条目的索引。它是位图信息的一部分,除非像素格式为:在这种情况下,每个值都是系统颜色表中的一个条目。如果未指定,新 Bitmap 对象的
默认值为, 或。 PalettePixelFormat.IndexedPixelFormatPixelFormat.Format32bppArgbPixelFormat.Canonical关于跨步的重要注意事项:
如前所述,Stride(也称为扫描线)表示组成单行像素的字节数。由于硬件对齐要求,它始终向上舍入为 4 字节边界(4 的整数倍)。
Stride = [Bitmap Width] * [bytes per Color]
Stride += (Stride Mod 4) * [bytes per Color]
Run Code Online (Sandbox Code Playgroud)
这就是为什么我们总是使用使用创建的位图的原因之一PixelFormat.Format32bppArgb:位图Stride总是已经与所需的边界对齐。
如果位图的格式改为 (每种颜色 3 个字节)怎么办? PixelFormat.Format24bppRgb
如果位图Width乘以每像素字节数不是 的倍数4,则将Stride用 s 填充0以填补空白。
大小的位图(100 x 100)在 32 位和 24 位格式中都没有填充:
100 * 3 = 300 : 300 Mod 4 = 0 : Stride = 300
100 * 4 = 400 : 400 Mod 4 = 0 : Stride = 400
Run Code Online (Sandbox Code Playgroud)
对于大小为 Bitmap 的情况会有所不同(99 x 100):
99 * 3 = 297 : 297 Mod 4 = 1 : Stride = 297 + ((297 Mod 4) * 3) = 300
99 * 4 = 396 : 396 Mod 4 = 0 : Stride = 396
Run Code Online (Sandbox Code Playgroud)
24 位位图的Stride被填充添加 3 个字节(设置为0)以填充边界。
当我们检查/修改通过坐标访问单个像素的内部值时,这不是问题,类似于 SetPixel/GetPixel 的操作方式:始终会正确找到像素的位置。
(98, 70)假设我们需要检查/更改大小为 的位图中位置处的像素(99 x 100)。
仅考虑每个像素的字节数。Buffer内的像素位置为:
[Bitmap] = new Bitmap(99, 100, PixelFormat = Format24bppRgb)
[Bytes x pixel] = Image.GetPixelFormatSize([Bitmap].PixelFormat) / 8
[Pixel] = new Point(98, 70)
[Pixel Position] = ([Pixel].Y * [BitmapData.Stride]) + ([Pixel].X * [Bytes x pixel])
[Color] = Color.FromArgb([Pixel Position] + 2, [Pixel Position] + 1, [Pixel Position])
Run Code Online (Sandbox Code Playgroud)
将像素的垂直位置乘以扫描线的宽度,缓冲区内的位置将始终是正确的:计算中包含填充的大小。
下一个位置 的像素颜色(0, 71)将返回预期结果:
顺序读取颜色字节时会有所不同。
第一个扫描行将返回直到最后一个像素(最后 3 个字节)的有效结果:接下来的 3 个字节将返回用于舍入 的字节值Stride,全部设置为0。
这或许也不是问题。例如,应用过滤器时,表示像素的每个字节序列都会使用过滤器矩阵的值进行读取和修改:我们只需修改 3 个字节的序列,在渲染位图时不会考虑该序列。
但如果我们正在搜索特定的像素序列,这确实很重要:读取不存在的像素颜色可能会损害结果和/或使算法不平衡。
对位图颜色执行统计分析时也是如此。
当然,我们可以在循环中添加一个检查:if [Position] Mod [BitmapData].Width = 0 : continue。
但这会为每次迭代添加新的计算。
实际操作
简单的解决方案(更常见的解决方案)是创建一个格式为 的新位图PixelFormat.Format32bppArgb,因此Stride将始终正确对齐:
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices
Private Function CopyTo32BitArgb(image As Image) As Bitmap
Dim imageCopy As New Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb)
imageCopy.SetResolution(image.HorizontalResolution, image.VerticalResolution)
For Each propItem As PropertyItem In image.PropertyItems
imageCopy.SetPropertyItem(propItem)
Next
Using g As Graphics = Graphics.FromImage(imageCopy)
g.DrawImage(image,
New Rectangle(0, 0, imageCopy.Width, imageCopy.Height),
New Rectangle(0, 0, image.Width, image.Height),
GraphicsUnit.Pixel)
g.Flush()
End Using
Return imageCopy
End Function
Run Code Online (Sandbox Code Playgroud)
这会生成具有相同 DPI 定义的字节兼容位图;Image.PropertyItems也是从源图像复制的 。
为了测试它,让我们将棕褐色色调滤镜应用于图像,使用它的副本来执行位图数据所需的所有修改:
Public Function BitmapFilterSepia(source As Image) As Bitmap
Dim imageCopy As Bitmap = CopyTo32BitArgb(source)
Dim imageData As BitmapData = imageCopy.LockBits(New Rectangle(0, 0, source.Width, source.Height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb)
Dim buffer As Byte() = New Byte(Math.Abs(imageData.Stride) * imageCopy.Height - 1) {}
Marshal.Copy(imageData.Scan0, buffer, 0, buffer.Length)
Dim bytesPerPixel = Image.GetPixelFormatSize(source.PixelFormat) \ 8;
Dim red As Single = 0, green As Single = 0, blue As Single = 0
Dim pos As Integer = 0
While pos < buffer.Length
Dim color As Color = Color.FromArgb(BitConverter.ToInt32(buffer, pos))
' Dim h = color.GetHue()
' Dim s = color.GetSaturation()
' Dim l = color.GetBrightness()
red = buffer(pos) * 0.189F + buffer(pos + 1) * 0.769F + buffer(pos + 2) * 0.393F
green = buffer(pos) * 0.168F + buffer(pos + 1) * 0.686F + buffer(pos + 2) * 0.349F
blue = buffer(pos) * 0.131F + buffer(pos + 1) * 0.534F + buffer(pos + 2) * 0.272F
buffer(pos + 2) = CType(Math.Min(Byte.MaxValue, red), Byte)
buffer(pos + 1) = CType(Math.Min(Byte.MaxValue, green), Byte)
buffer(pos) = CType(Math.Min(Byte.MaxValue, blue), Byte)
pos += bytesPerPixel
End While
Marshal.Copy(buffer, 0, imageData.Scan0, buffer.Length)
imageCopy.UnlockBits(imageData)
imageData = Nothing
Return imageCopy
End Function
Run Code Online (Sandbox Code Playgroud)
Bitmap.LockBits并不总是可用的最佳选择。使用ColorMatrix
类
也可以非常轻松地执行应用滤镜的相同过程,该类允许仅使用简单的 float () 值数组将矩阵转换应用于位图。 5x5Single
例如,让我们使用该类ColorMatrix和众所周知的5x5矩阵来应用灰度滤镜:
Public Function BitmapMatrixFilterGreyscale(source As Image) As Bitmap
' A copy of the original is not needed but maybe desirable anyway
' Dim imageCopy As Bitmap = CopyTo32BitArgb(source)
Dim filteredImage = New Bitmap(source.Width, source.Height, source.PixelFormat)
filteredImage.SetResolution(source.HorizontalResolution, source.VerticalResolution)
Dim grayscaleMatrix As New ColorMatrix(New Single()() {
New Single() {0.2126F, 0.2126F, 0.2126F, 0, 0},
New Single() {0.7152F, 0.7152F, 0.7152F, 0, 0},
New Single() {0.0722F, 0.0722F, 0.0722F, 0, 0},
New Single() {0, 0, 0, 1, 0},
New Single() {0, 0, 0, 0, 1}
})
Using g As Graphics = Graphics.FromImage(filteredImage), attributes = New ImageAttributes()
attributes.SetColorMatrix(grayscaleMatrix)
g.DrawImage(source, New Rectangle(0, 0, source.Width, source.Height),
0, 0, source.Width, source.Height, GraphicsUnit.Pixel, attributes)
End Using
Return filteredImage
End Function
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
676 次 |
| 最近记录: |