none
Detecting specific color in image

    Question

  • Hello All,

     

    I am wondering if there is a way to run through an image and check to see if the image contains a red pixel.

    Monday, September 05, 2011 11:29 PM

Answers

  • You can use code like this to find a pixel of a particur color:

            Dim BMP As Bitmap = Image.FromFile("C:\users\Public\Sample.jpg")
            
    For X As Integer = 0 To BMP.Width - 1
                
    For Y As Integer = 0 To BMP.Height - 1
                    
    If BMP.GetPixel(X, Y).ToArgb = Color.Red.ToArgb Then
                        MsgBox(" Found at " & X.ToString & "," & Y.ToString)
                        
    Exit Sub
                    End If
                Next
            Next


    • Edited by Acamar Tuesday, September 06, 2011 12:08 AM
    • Marked as answer by Jebtrillion 137 Tuesday, September 06, 2011 12:13 AM
    Tuesday, September 06, 2011 12:06 AM

All replies

  • If the image is of type Bitmap, call it's GetPixel method. Otherwise you can create a new Bitmap from the original before:

       Dim bmp as new bitmap(OriginalImage)

     

    The return value of GetPixel is of type Drawing.Color. Compare it to Drawing.Color.Red.


    Armin
    Tuesday, September 06, 2011 12:04 AM
  • Define "red"
    in RGB terms - red is 255 0 0
    what about 254 0 0, 255 2 2, etc
    something like this should work, might be slow on a huge pic - obviously the path is to a pic on my computer........
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim pixelcolor As Color
            Dim Rd, Gr, Bl As Integer
            Dim MyBitmap As New Bitmap("D:\DLoads\Pics\Chile Mine - Jeff Hart(driller) with Elizabeth Segovia(sister of a trapped miner).bmp")
            For y = 0 To MyBitmap.Height - 1
                For x = 0 To MyBitmap.Width - 1
                    pixelcolor = MyBitmap.GetPixel(x, y)
                    Rd = pixelcolor.R
                    Gr = pixelcolor.G
                    Bl = pixelcolor.B
                    If Rd > 250 And Gr < 128 And Bl < 128 Then
                        MsgBox("Found Red")
                    End If
                Next x
                TextBox1.Text = "Processing Row # " & y.ToString & " of " & MyBitmap.Height - 1
                TextBox1.Refresh()
            Next y
        End Sub
    

     
    Tuesday, September 06, 2011 12:05 AM
  • You can use code like this to find a pixel of a particur color:

            Dim BMP As Bitmap = Image.FromFile("C:\users\Public\Sample.jpg")
            
    For X As Integer = 0 To BMP.Width - 1
                
    For Y As Integer = 0 To BMP.Height - 1
                    
    If BMP.GetPixel(X, Y).ToArgb = Color.Red.ToArgb Then
                        MsgBox(" Found at " & X.ToString & "," & Y.ToString)
                        
    Exit Sub
                    End If
                Next
            Next


    • Edited by Acamar Tuesday, September 06, 2011 12:08 AM
    • Marked as answer by Jebtrillion 137 Tuesday, September 06, 2011 12:13 AM
    Tuesday, September 06, 2011 12:06 AM