[VB.Net] GetPixelColourCount

Decided to turn my solution to the question here into an easy to use function.
It uses the goto function, which I feel is frowned upon, but go ahead and complain ^_^

    ''' <summary >
    ''' Function to count the number of pixels of the same colour
    ''' </summary >
    ''' <param name="bmpImage" > The image as a Bitmap that you wish to count the pixels of</param >
    ''' <param name="Bounds" > The rectangle for the area that you wish to calculate</param >
    ''' <returns > A list of strings containing the pixels colour and the number of pixels of that colour</returns >
    ''' <remarks > sim0n</remarks >
    Private Function GetPixelColourCount(ByVal bmpImage As Bitmap, ByVal Bounds As RectangleF) As List(Of String())
        Dim Pixels As New List(Of String())
        For x As Integer = Bounds.Left To Bounds.Right - 1
            For y As Integer = Bounds.Top To Bounds.Bottom - 1
                Dim Found As Boolean = False
                For Each str As String() In Pixels
                    If str(0) = CStr(bmpImage.GetPixel(x, y).ToArgb) Then
                        str(1) = CStr(CInt(str(1)) + 1)
                        Found = True
                        GoTo pixelFound
                    End If
                Pixels.Add(New String() {CStr(bmpImage.GetPixel(x, y).ToArgb), 1})
        Return Pixels
    End Function

Using image: http://privatewww.essex.ac.uk/~sjs/research/iso_colour_blocks.png

        ''Get our image from PictureBox1
        Dim btmp As New Bitmap(PictureBox1.Image)
        ''Set the cursor to wait
        Me.Cursor = Cursors.WaitCursor
        ''Loop through the results - Rectangle is the entire image
        For Each str As String() In GetPixelColourCount(btmp, New RectangleF(0, 0, btmp.PhysicalDimension.Width, btmp.PhysicalDimension.Height))
            Debug.WriteLine(Color.FromArgb(str(0)).ToString & " | Count: " & str(1))
        ''Reset Cursor
        Me.Cursor = Cursors.Default

Would output:
Color [A=255, R=85, G=85, B=85] | Count: 65536
Color [A=255, R=0, G=255, B=0] | Count: 32768
Color [A=255, R=127, G=0, B=128] | Count: 32768
Color [A=255, R=0, G=0, B=255] | Count: 32768
Color [A=255, R=127, G=128, B=0] | Count: 32768
Color [A=255, R=255, G=0, B=0] | Count: 32768
Color [A=255, R=0, G=127, B=128] | Count: 32768

You can also just select regions of the image to calculate using the Bounds parameter, for example to get the colour of just one square, you could do:

  ''64 Pixels along
  ''0 Pixels down
  ''Width 64 pixels
  ''Heigh 64 Pixels
  For Each str As String() In GetPixelColourCount(btmp, New RectangleF(64, 0, 64, 64))
      Debug.WriteLine(Color.FromArgb(str(0)).ToString & " | Count: " & str(1))

Would output:
Color [A=255, R=127, G=128, B=0] | Count: 4096


0 Responses to “[VB.Net] GetPixelColourCount”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: