Page 1 of 1

.NET: find coordinates of specified color

Posted: 2015-12-05T18:04:25-07:00
by vgvallee
Using the C# .NET API, how to find the coordinates of pixels of a given color? Plenty of examples using cmd but I'm having a hard time figuring how to do it with C#. I have seen some cmd examples dealing with txt output to find specific lines but the images I have to deal with are 5000x3000. It came out to around 680MB so not quite manageable that way. I got my images mostly black alpha area with white islands. Maybe it would be useful to reduce each islands to 1 pixel each if it makes it easier to have a smaller list?

Thanks.

Re: .NET: find coordinates of specified color

Posted: 2015-12-06T12:49:49-07:00
by dlemstra
You can access the pixels with the GetReadOnlyPixels method. Below is an example of how you can check each pixel.

Code: Select all

MagickColor color = new MagickColor("white");

using (var image = new MagickImage("input.png"))
{
  using (var pixels = image.GetReadOnlyPixels())
  {
    foreach (var pixel in pixels)
    {
      /* Exact match */
      if (pixel.ToColor().Equals(color))
        DoSomething(pixel.X, pixel.Y);

      /* Fuzz match */
      if (pixel.ToColor().FuzzyEquals(color, new Percentage(1)))
        DoSomething(pixel.X, pixel.Y);
    }
  }
}

Re: .NET: find coordinates of specified color

Posted: 2015-12-06T14:01:53-07:00
by vgvallee
This works very well... Somehow, I had come up with something that took a very long time.