0

I want to find the coordinates of black colored dots from a Colored Image. If i put three dots on a white background, my code detects it and shows the coordinates in textboxes. But my code doesn't work for the image given in the following link. what should I do??

https://drive.google.com/file/d/0B6ynC-W5aF41cWxtU3BVN3g3U00/view?usp=sharing

My Code is :

for (int i = 0; i < PatientImage.Image.Height; i++)
 {
    for (int j = 0; j < PatientImage.Image.Width; j++)
      {
          //Get the color at each pixel
          Color now_color = bmp.GetPixel(j, i);

//Compare Pixel's Color ARGB property with the picked color's ARGB Property 
           if (now_color.ToArgb() ==  Color.Black.ToArgb())
            {
                 // MessageBox.Show("Color Found!");
                 // MessageBox.Show("X = " + j + " , " + "Y =" + i);
                    bool flag = false;

                 if (String.IsNullOrEmpty(COPX.Text))
                   {
                      COPX.Text = Convert.ToString(j);
                      COPY.Text = Convert.ToString(i);
                      flag = true;
                    }

            }
      }
 }
Yawar Younus
  • 125
  • 1
  • 13
  • You should post some code for us to see. But first, consider debugging it to narrow it down to the *exact* problem; comparison doesn't work, improper format, etc. – GEEF Jul 31 '15 at 00:54
  • there is no black or white in that image – RadioSpace Jul 31 '15 at 02:10

1 Answers1

0

As has been mentioned the main problem is that the 'black dots' are anything but black. They are a darkish grey at best.

The real solution will be to not compare the Color for equality but the brightness to be lower than some threshold:

float threshold = 0.15f;
if (bmp.GetPixel(j, i).GetBrightness() < threshold )..

Obviously you will have to play with the value of threshold.

It may also be helpful to apply some gamma to the image, best by using a Color Matrix..

Looking closer at the example image, I have to assume that some other parts are also rather dark; to exclude them you may need to add a check for the saturation as well..:

Color now_color = bmp.GetPixel(j, i);
if (now_color.GetBrightness() < threshold1  && now_color.GetSaturation < threshold2)..
Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111