.NET: find coordinates of specified color

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
vgvallee
Posts: 3
Joined: 2015-12-05T12:15:28-07:00
Authentication code: 1151

.NET: find coordinates of specified color

Post 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.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: .NET: find coordinates of specified color

Post 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);
    }
  }
}
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
vgvallee
Posts: 3
Joined: 2015-12-05T12:15:28-07:00
Authentication code: 1151

Re: .NET: find coordinates of specified color

Post by vgvallee »

This works very well... Somehow, I had come up with something that took a very long time.
Post Reply