The question asked “How do i get the amount and colour of pixels in an Image?”
The Solution
I had to think a bit about this one, dealing with image processing needs to be as efficent as possible, however I couldnt see a better way than just looping through the pixels and adding them to a list.
Here is the solution that I posted to the users project:
Dim PixelList As New List(Of String()) Public Sub ColourInList(ByVal Colour As Color) Dim Found As Boolean = False For Each str As String() In PixelList If str(0) = CStr(Colour.ToArgb) Then str(1) = CStr(CInt(str(1)) + 1) Found = True Exit For End If Next If Found = False Then PixelList.Add(New String() {CStr(Colour.ToArgb), 1}) End Sub Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click Dim btmp As New Bitmap(PictureBox1.Image) For x As Integer = 0 To btmp.Width - 1 For y As Integer = 0 To btmp.Height - 1 ColourInList(btmp.GetPixel(x, y)) Next Next For Each str As String() In PixelList Debug.WriteLine(Color.FromArgb(str(0)).ToString & " | Count: " & str(1)) Next End Sub
Would Output:
Using this Image:
http://privatewww.essex.ac.uk/~sjs/research/iso_colour_blocks.png
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
1 Response to “[VB.Net] Q: Get the amount and colour of pixels in an Image”