以下代码适用于我正在做的事情。但是,它需要的时间比我需要的时间长。问题是它遍历每个大于/小于函数并且需要时间。我已经做了一些研究,但无法弄清楚如何减少它以便它运行得更快。
像素RGB值必须通过以下测试 - (1)如果一个值大于250,则其他值必须小于5 (2)如果一个值小于5,则其他值必须大于250 (3)如果一个值等于零,则其他值必须大于0 (4)任何2个值之间的差异必须小于15(或我设定的任何其他阈值) (5)看两个值是否等于零
另外,在这些功能有用之后会做'退出'吗?
' Create new bitmap from filepath in TextBox1
Dim bmp As New Bitmap(TextBox1.Text)
' Lock the bitmap's pixels
Dim rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
Dim bmpData As System.Drawing.Imaging.BitmapData = bmp.LockBits(rect, _
Drawing.Imaging.ImageLockMode.ReadWrite, _
Imaging.PixelFormat.Format24bppRgb)
' Get the address of the first line
Dim ptr As IntPtr = bmpData.Scan0
' Declare an array to hold the bytes of the bitmap
Dim bytes As Integer = Math.Abs(bmpData.Stride) * bmp.Height
Dim rgbValues(bytes - 1) As Byte
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)
' Retrieve RGB values
Dim RedValue As Int32
Dim GreenValue As Int32
Dim BlueValue As Int32
Dim l As Integer = 0
For x = 0 To bmp.Width
For y = 0 To bmp.Height - 1
l = ((bmp.Width * 3 * y) + (x * 3))
RedValue = rgbValues(l)
GreenValue = rgbValues(l + 1)
BlueValue = rgbValues(l + 2)
If RedValue < 5 AndAlso GreenValue < 5 AndAlso BlueValue > 250 Then
ElseIf RedValue < 5 AndAlso GreenValue > 250 AndAlso BlueValue < 5 Then
ElseIf RedValue > 250 AndAlso GreenValue < 5 AndAlso BlueValue < 5 Then
ElseIf RedValue > 250 AndAlso GreenValue > 250 AndAlso BlueValue < 5 Then
ElseIf RedValue > 250 AndAlso GreenValue < 5 AndAlso BlueValue > 250 Then
ElseIf RedValue < 5 AndAlso GreenValue > 250 AndAlso BlueValue > 250 Then
ElseIf RedValue > 0 AndAlso GreenValue > 0 AndAlso BlueValue.Equals(0) Then
ElseIf RedValue > 0 AndAlso GreenValue.Equals(0) AndAlso BlueValue > 0 Then
ElseIf RedValue.Equals(0) AndAlso GreenValue > 0 AndAlso BlueValue > 0 Then
ElseIf (RedValue - GreenValue) < 15 AndAlso (RedValue - BlueValue) < 15 AndAlso _
(GreenValue - RedValue) < 15 AndAlso (GreenValue - BlueValue) < 15 AndAlso _
(BlueValue - RedValue) < 15 AndAlso (BlueValue - GreenValue) < 15 Then
ElseIf RedValue.Equals(GreenValue) Then
ElseIf RedValue.Equals(BlueValue) Then
ElseIf GreenValue.Equals(BlueValue) Then
ElseIf RedValue.Equals(BlueValue) AndAlso RedValue.Equals(GreenValue) _
AndAlso BlueValue.Equals(GreenValue) Then
Else
MsgBox("Image is color.")
Exit Sub
End If
Next
Next
MsgBox("Image is grayscale.")
' Unlock the bitmap
bmp.UnlockBits(bmpData)
答案 0 :(得分:0)
灰度颜色始终为此规格(R = G = B)。 例如。 #cccccc = [R:204 G:204 B:204]
If Not R = G And G = B Then
MsgBox("colored")
Exit Sub
End If