Results 1 to 3 of 3

Thread: creating an look up array of pixel locations for specific colours

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    2

    Question creating an look up array of pixel locations for specific colours

    Hi everyone
    I have a JPEG image. And from this image that i need to create arrays of pixel coordinates for specific colours,such as blue,red,yellow etc, and then assign a fixed value for each colour in the array. But am a complete novice to programming and VB. Any help and guidance on how to make this happen will be gratefully recieved.

    Thanks

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,697

    Re: creating an look up array of pixel locations for specific colours

    What you need to do is first create a function with either a string parameter which will represent the image's location or an image parameter which will represent the actual image. The function will return a bitmap:
    Code:
    Private Function ConvertPixels(ByVal path As String) As Bitmap
    
    End Function
    Or
    Code:
    Private Function ConvertPixels(ByVal img As Image) As Bitmap
    
    End Function
    I personally prefer the path parameter, so that will be the example that I use.

    Inside of the function, create a new bitmap based on the parameter that's passed:
    Code:
    Private Function ConvertPixels(ByVal path As String) As Bitmap
        Dim b As Bitmap = New Bitmap(path)
    
    
    End Function
    Next, loop through each pixel of the bitmap. This will be done by using a nested loop:
    Code:
    Private Function ConvertPixels(ByVal path As String) As Bitmap
        Dim b As Bitmap = New Bitmap(path)
    
        For x As Integer = 0 To b.Width - 1 'Left > Right
            For y As Integer = 0 To b.Height - 1 'Top > Bottom
    
            Next
        Next
    End Function
    Next check if the color of the current pixel matches one of your colors. To get the current pixel, use the GetPixel method:
    Code:
    Private Function ConvertPixels(ByVal path As String) As Bitmap
        Dim b As Bitmap = New Bitmap(path)
    
        For x As Integer = 0 To b.Width - 1 'Left > Right
            For y As Integer = 0 To b.Height - 1 'Top > Bottom
                Dim currentPixel As Color = b.GetPixel(x, y)
    
                If currentPixel = Color.Blue Then
    
                ElseIf currentPixel = Color.Red Then
    
                ElseIf currentPixel = Color.Yellow Then
    
                    'etc..
                End If
            Next
        Next
    
    End Function
    Next set the current pixel to whatever value that you wish based on what the pixel currently is. To set the pixel, use the SetPixel method:
    Code:
    Private Function ConvertPixels(ByVal path As String) As Bitmap
        Dim b As Bitmap = New Bitmap(path)
    
        For x As Integer = 0 To b.Width - 1 'Left > Right
            For y As Integer = 0 To b.Height - 1 'Top > Bottom
                Dim currentPixel As Color = b.GetPixel(x, y)
    
                If currentPixel = Color.Blue Then
                    b.SetPixel(x, y, Color.Red)
                ElseIf currentPixel = Color.Red Then
                    b.SetPixel(x, y, Color.Yellow)
                ElseIf currentPixel = Color.Yellow Then
                    b.SetPixel(x, y, Color.Blue)
                End If
            Next
        Next
    
    End Function
    Finally return the bitmap at the end of the nested loop:
    Code:
    Private Function ConvertPixels(ByVal path As String) As Bitmap
        Dim b As Bitmap = New Bitmap(path)
    
        For x As Integer = 0 To b.Width - 1 'Left > Right
            For y As Integer = 0 To b.Height - 1 'Top > Bottom
                Dim currentPixel As Color = b.GetPixel(x, y)
    
                If currentPixel = Color.Blue Then
                    b.SetPixel(x, y, Color.Red)
                ElseIf currentPixel = Color.Red Then
                    b.SetPixel(x, y, Color.Yellow)
                ElseIf currentPixel = Color.Yellow Then
                    b.SetPixel(x, y, Color.Blue)
                End If
            Next
        Next
    
        Return b
    End Function
    In that example, I replace:
    1. Blue Pixels to Red Pixels
    2. Red Pixels to Yellow Pixels
    3. Yellow Pixels to Blue Pixels

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2014
    Posts
    2

    Re: creating an look up array of pixel locations for specific colours

    Thanks dday9 appreciate the help and guidance will have play

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width