locked
color detection RRS feed

  • Question

  • iam trying to implement an color detection from image , in this case to detect red color and draw a circle at the pixel where it it red , but don't get the result there is no change in the image, please help here is my code

     for (int x = 0; x < pictureBox1.Width-1 ; x++)
                    {
                        for (int y = 0; y < pictureBox1.Height-1  ; y++)
                        {
                            Color now_color = img_bmp.GetPixel(x, y);
                            Color red = Color.FromArgb(255, 0, 0);
                            if (now_color ==red)
                            {
                                Graphics g = Graphics.FromImage(img_bmp);
                                g.DrawEllipse(Pens.Black, x, y, 200, 200); 
                            }
                          
    SK
    Thursday, December 3, 2009 7:12 AM

Answers

  • hi  karthik .s    if you  make a breakpoint  at  the  if  instruction  you  will  find that   the now_color have 4 color (A,R,G,B)   so the  right  way  to  make the comparison  
        

     
      private void button1_Click(object sender, EventArgs e)
            {
                Bitmap bmp = new Bitmap(pictureBox1.Image); 
                Graphics g = Graphics.FromImage(bmp);
                  
                for (int i = 0; i < pictureBox1.Image.Height; i++)
                {
                    for (int j = 0; j < pictureBox1.Image.Width; j++)
                    {
                        Color now_color = bmp.GetPixel(j, i);
                        
                            
                        Color red = Color.FromArgb(255, 255, 0, 0);
                                            
                        short now_RedColor = Convert.ToInt16(now_color.R)  ;  
                         short now_GreenColor = Convert.ToInt16(now_color.G)    ;  
                        short  now_BlueColor =  Convert.ToInt16(now_color.B)  ;  
                        short redColor = Convert.ToInt16(red.R)  ;  
                         short greenColor = Convert.ToInt16(red.G)    ;  
                        short  blueColor =  Convert.ToInt16(red.B)  ;
                        //here  you  will find more practical 
                        if ( (now_RedColor== redColor || ( now_RedColor <255 && now_RedColor>200)) && now_GreenColor < 70  && now_BlueColor <70 )
                        {
                            g.DrawEllipse(Pens.Black, j - 50, i - 50, 100, 100);
                            
                        }
    
                    }
                    
                }
                 
                pictureBox1.Image = bmp;  
            }
    Hope this help 
    A man's dreams are an index to his greatness
    • Proposed as answer by Chao Kuo Monday, December 7, 2009 9:55 AM
    • Marked as answer by Chao Kuo Tuesday, December 8, 2009 7:55 AM
    Thursday, December 3, 2009 9:01 AM

All replies

  • hi,

    are you sure that now_color in your picture is exactly the "red" that you defined in your code? maybe its A, R, G, B values are different that 255, 0, 0?

    on picturebox's mouseclick event you can get the alpha, red, green, blue values of your picture's red like this:

         private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
            {
                Color c = ((Bitmap)pictureBox1.Image).GetPixel(e.X, e.Y);
            }
    After you get the exact values, try changing 255, 0, 0 to these values.
    Thursday, December 3, 2009 8:29 AM
  • hi  karthik .s    if you  make a breakpoint  at  the  if  instruction  you  will  find that   the now_color have 4 color (A,R,G,B)   so the  right  way  to  make the comparison  
        

     
      private void button1_Click(object sender, EventArgs e)
            {
                Bitmap bmp = new Bitmap(pictureBox1.Image); 
                Graphics g = Graphics.FromImage(bmp);
                  
                for (int i = 0; i < pictureBox1.Image.Height; i++)
                {
                    for (int j = 0; j < pictureBox1.Image.Width; j++)
                    {
                        Color now_color = bmp.GetPixel(j, i);
                        
                            
                        Color red = Color.FromArgb(255, 255, 0, 0);
                                            
                        short now_RedColor = Convert.ToInt16(now_color.R)  ;  
                         short now_GreenColor = Convert.ToInt16(now_color.G)    ;  
                        short  now_BlueColor =  Convert.ToInt16(now_color.B)  ;  
                        short redColor = Convert.ToInt16(red.R)  ;  
                         short greenColor = Convert.ToInt16(red.G)    ;  
                        short  blueColor =  Convert.ToInt16(red.B)  ;
                        //here  you  will find more practical 
                        if ( (now_RedColor== redColor || ( now_RedColor <255 && now_RedColor>200)) && now_GreenColor < 70  && now_BlueColor <70 )
                        {
                            g.DrawEllipse(Pens.Black, j - 50, i - 50, 100, 100);
                            
                        }
    
                    }
                    
                }
                 
                pictureBox1.Image = bmp;  
            }
    Hope this help 
    A man's dreams are an index to his greatness
    • Proposed as answer by Chao Kuo Monday, December 7, 2009 9:55 AM
    • Marked as answer by Chao Kuo Tuesday, December 8, 2009 7:55 AM
    Thursday, December 3, 2009 9:01 AM