【问题标题】:VB.Net - Lockbits - Greater Than / Less Than functionsVB.Net - Lockbits - 大于/小于函数
【发布时间】:2012-07-10 18:29:08
【问题描述】:

以下代码适用于我正在做的事情。但是,它需要的时间比我需要的要长。问题是它迭代每个大于/小于函数,这需要时间。我已经进行了一些研究,但无法弄清楚如何将其全部瘦身以使其运行得更快。

像素 RGB 值必须经过以下测试 - (1) 如果一个值大于 250,其他值必须小于 5 (2) 如果一个值小于 5,其他值必须大于 250 (3) 如果一个值等于 0,其他值必须大于 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)

【问题讨论】:

  • 我对你的逻辑感到困惑。如果 (1) 为真,则 (2) 永远不会为真。如果 (1) 或 (2) 为真,则 (4) 永远不可能为真。这是一系列检查,还是每个 RGB 值都必须符合所有规则?如果是一个序列,那么嵌套的 Ifs 可能会给你带来更好的性能
  • 这是一系列检查,因此每个测试都是独占的。我不希望每个 RGB 值都符合每个测试。我只是想确保我清除了我认为不是颜色的每个像素,包括清除极端差异以提高准确性(255:255:0、0:0:255 等)。我会尝试嵌套几个 If,看看是否有帮助。没想到。谢谢。
  • @Jerry 这两个帐户有关联吗? (杰瑞霍拉克和你)。如果是这样,请告诉我们,以便我们合并您的帐户。

标签: vb.net rgb lockbits


【解决方案1】:

灰度颜色始终具有此规范( R=G=B )。 例如。 #cccccc = [R:204 G:204 B:204]

If Not R = G And G = B Then
 MsgBox("colored")
 Exit Sub
End If

【讨论】:

    猜你喜欢
    • 2020-11-26
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-12
    相关资源
    最近更新 更多