使用LockBits确定像素颜色(慢)

时间:2015-01-31 22:26:54

标签: vb.net performance image-processing

我正在尝试使用Lockbits来确定图像中每个像素的颜色。特别是黑色与白色对比其他。我最初使用GetPixel写了这个,但它太慢了;但是,使用Lockbits也一样慢。它在几秒钟内飞过前200个像素,然后非常慢(大约一个像素/秒)。是否存在实施错误,疏忽或更好的解决方案?

    Dim bmp As Bitmap = New Bitmap("path") 
    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)
    Dim ptr As IntPtr = bmpData.Scan0
    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)
    pbStatus.Maximum = bmp.Width * bmp.Height
    ' Retrieve RGB values
    Dim RedValue As Int32
    Dim GreenValue As Int32
    Dim BlueValue As Int32
    Dim l As Integer = 0        
    Dim progress As Integer = 0          
    For x = 0 To bmp.Width - 1 
        For y = 0 To bmp.Height - 1
            Application.DoEvents()
            l = ((bmp.Width * 3 * y) + (x * 3))
            RedValue = rgbValues(l)
            GreenValue = rgbValues(l + 1)
            BlueValue = rgbValues(l + 2)
            If RedValue & GreenValue & BlueValue = "000" Then
                txtTX.Text = txtTX.Text + "Black, "
            ElseIf RedValue & GreenValue & BlueValue = "255255255" Then
                txtTX.Text = txtTX.Text + "White, "
            Else
                txtTX.Text = txtTX.Text + "Neither, "
            End If
            progress = progress + 1
            lblProgress.text = progress
        Next
    Next
    bmp.UnlockBits(bmpData)

1 个答案:

答案 0 :(得分:0)

如果你需要来更新UI线程上的控件,你经常调用Application.DoEvents,然后执行以下操作:

Dim bmp As Bitmap = New Bitmap("path") 
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)
Dim ptr As IntPtr = bmpData.Scan0
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)
pbStatus.Maximum = bmp.Width * bmp.Height
' Retrieve RGB values
Dim RedValue As Int32
Dim GreenValue As Int32
Dim BlueValue As Int32
Dim l As Integer = 0        
Dim progress As Integer = 0          
DIM BreatheUI as integer = 0

For x = 0 To bmp.Width - 1 
    For y = 0 To bmp.Height - 1

        ' Update the UI every 500 cycles
        BreatheUI += 1 
        IF BreatheUI = 500 Then Application.DoEvents(): BreatheUI = 0

        l = ((bmp.Width * 3 * y) + (x * 3))
        RedValue = rgbValues(l)
        GreenValue = rgbValues(l + 1)
        BlueValue = rgbValues(l + 2)
        If RedValue & GreenValue & BlueValue = "000" Then
            txtTX.Text = txtTX.Text + "Black, "
        ElseIf RedValue & GreenValue & BlueValue = "255255255" Then
            txtTX.Text = txtTX.Text + "White, "
        Else
            txtTX.Text = txtTX.Text + "Neither, "
        End If
        progress = progress + 1
        lblProgress.text = progress
    Next
Next
bmp.UnlockBits(bmpData)