3 Replies - 41813 Views - Last Post: 08 May 2010 - 05:44 AM Rate Topic: -----

#1 tom_b   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 29
  • Joined: 31-December 08

Something faster than bitmap.getpixel?

Posted 24 April 2010 - 01:51 AM

What I'm trying to do is iterate through each pixel in a bitmap, and get the color value of each pixel.
I've tried using .getpixel, but it's fairly slow. On a 640x480 image, it takes roughly .35 seconds to go through it. Which can add up if doing many pictures...

myBitmap = New Bitmap("c:\test.png")
Dim Width As Int32 = myBitmap.Width

Dim Height As Int32 = myBitmap.Height

For y As Int32 = 0 To Height - 1
  For x As Int32 = 0 To Width - 1
    myColor = myBitmap.GetPixel(x, y)
  Next
Next


I then tried, following some examples on the internet, to use LockBits. Which ends up being a little bit faster, at .27 seconds, but still not as fast as I thought it'd be. I'm also not sure how to get the color value out of it.

myBitmap = New Bitmap("c:\test.png")
Dim Width As Int32 = myBitmap.Width
Dim Height As Int32 = myBitmap.Height

For y As Int32 = 0 To Height - 1
  For x As Int32 = 0 To Width - 1
    Dim g_PixBytes() As Byte
    Dim bounds As Rectangle = New Rectangle(x, y, 1, 1)
    Dim m_BitmapData As New BitmapData

    m_BitmapData = myBitmap.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb)

    Dim total_size As Integer = m_BitmapData.Stride * m_BitmapData.Height
    ReDim g_PixBytes(total_size)
    Marshal.Copy(m_BitmapData.Scan0, g_PixBytes, 0, total_size)

    myBitmap.UnlockBits(m_BitmapData)
  Next
Next


How would I go about doing this faster or more efficient? And also, am I doing the above examples incorrectly which is leading to speed loss? Thanks for any help.

Is This A Good Question/Topic? 0
  • +

Replies To: Something faster than bitmap.getpixel?

#2 adamarek   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-November 09

Re: Something faster than bitmap.getpixel?

Posted 27 April 2010 - 10:47 AM

It has been few days since you post it, do you still need help with this topic ?
Was This Post Helpful? 0
  • +
  • -

#3 machoolah   User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 87
  • Joined: 17-May 09

Re: Something faster than bitmap.getpixel?

Posted 06 May 2010 - 06:22 PM

View Postadamarek, on 27 April 2010 - 09:47 AM, said:

It has been few days since you post it, do you still need help with this topic ?



Hey Adam, could you share your response if you have something in mind? I am having a somehow similar problem. My original post is here:
http://www.dreaminco...lor-of-a-pixel/

Thanks
Was This Post Helpful? 0
  • +
  • -

#4 Guest_adamarek*


Reputation:

Re: Something faster than bitmap.getpixel?

Posted 08 May 2010 - 05:44 AM

GetPixel, SetPixel could be OK for one or few pixels, but if you working with whole image I would use the Lock/Unlock method.
Problem with the previous code is that ' Locking and Unlocking was placed inside X and Y loops. That will slow the process a lot. You do it only one time.
Try the code and see if it works for you...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim bmp As New System.Drawing.Bitmap(picViewer1.Image) 'IMAGE FROM PICTURE BOX...

        Call LockBitmap(bmp)      'LOCKING BITMAP  
      
        Dim RedValue As Int32
        Dim GreenValue As Int32
        Dim BlueValue As Int32
        Dim l As Integer = 0       ' Pixel position                 
        For x = 0 To bmp.Width - 1 'Pixels start with 0 so we need Height - 1  
            For y = 0 To bmp.Height - 1 

                l = ((bmp.Width * 3 * y) + (x * 3)) ' pixel is made of 3 parts (RGB colors)
                RedValue = rgbValues(l)
                GreenValue = rgbValues(l + 1)
                BlueValue = rgbValues(l + 2)
                If RedValue > 250 AndAlso GreenValue > 250 AndAlso BlueValue > 250 Then
                Msgbox("Pixel is of a white color...")
                ElseIf RedValue < 5 AndAlso GreenValue < 5 AndAlso BlueValue < 5 Then
                 Msgbox("Pixel is of a black color...")
                 Else
                Msgbox("Pixel is not white or black ...")
                End If
            Next
        Next

        Call UnlockBitmap(bmp)      'UNLOCKING BITMAP

        picViewer1.Image = (bmp)
        picViewer1.Refresh()

End Sub

Private Sub LockBitmap(ByVal bmp As Bitmap)
        Dim bounds As Rectangle = New Rectangle(0, 0, bmp.Width, bmp.Height)
        bmpdata = bmp.LockBits(bounds, Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format24bppRgb)
        g_RowSizeBytes = bmpdata.Stride
        Dim total_size As Integer = bmpdata.Stride * bmpdata.Height
        ReDim rgbValues(total_size)
        Marshal.Copy(bmpdata.Scan0, rgbValues, 0, total_size)
    End Sub

    Private Sub UnlockBitmap(ByVal bmp As Bitmap)
        Dim total_size As Integer = bmpdata.Stride * bmpdata.Height
        Marshal.Copy(rgbValues, 0, bmpdata.Scan0, total_size)
        bmp.UnlockBits(bmpdata)
        rgbValues = Nothing
        bmpdata = Nothing
    End Sub

Was This Post Helpful? 0

Page 1 of 1