Posts Tagged ‘colour

30
Mar
09

[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
                Next
                Pixels.Add(New String() {CStr(bmpImage.GetPixel(x, y).ToArgb), 1})
pixelFound:
            Next
        Next
        Return Pixels
    End Function

Usage:
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))
        Next
        ''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))
  Next

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

30
Mar
09

[VB.Net] Q: Get the amount and colour of pixels in an Image

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

27
Mar
09

[VBA] Q: How to get pixel colour

The question asked “On the image if i click anywhere, im supposed to tell the user what colour the pixel they clicked on was”

The solution:

Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (ByVal lpPoint As POINT) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long

Private Type POINT
    x As Long
    y As Long
End Type

Private Sub Image1_Click()
    Dim pLocation As POINT
    Dim lColour, lDC As Long
    lDC = GetWindowDC(0)
    Call GetCursorPos(pLocation)
    lColour = GetPixel(lDC, pLocation.x, pLocation.y)
    Me.BackColor = lColour
End Sub

27
Mar
09

[VB.Net] Q: How to get pixel colour

The question asked “On the image if i click anywhere, im supposed to tell the user what colour the pixel they clicked on was”

The solution:

Imports System.Runtime.InteropServices
Public Class Form1

    ''Required API DLL Imports
    <DllImport("gdi32.dll") >  Private Shared Function GetPixel(ByVal hdc As IntPtr, ByVal nXPos As Integer, ByVal nYPos As Integer) As Integer
    End Function
    <DllImport("gdi32.dll") >  Private Shared Function CreateDC(ByVal lpszDriver As String, ByVal lpszDevice As String, ByVal lpszOutput As String, ByVal lpInitData As IntPtr) As IntPtr
    End Function
    <DllImport("gdi32.dll") >  Private Shared Function DeleteDC(ByVal hdc As IntPtr) As Boolean
    End Function
    ''GetPixelColour Function
    Public Shared Function GetPixelColour(ByVal x As Integer, ByVal y As Integer) As Color
        Dim hdcScreen As IntPtr = _
            CreateDC("Display", Nothing, Nothing, IntPtr.Zero)
        Dim colorRef As Integer = GetPixel(hdcScreen, x, y)
        DeleteDC(hdcScreen)
        Return Color.FromArgb(colorRef And &HFF, (colorRef And &HFF00)  >  >  8, (colorRef And &HFF0000)  >  >  16)
    End Function

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        Debug.WriteLine(GetPixelColour(Cursor.Position.X, Cursor.Position.Y))
    End Sub
End Class

Just add a picture box to the form, when you click on the picturebox it will write the colour to the debug output