0

I have an image and I would like to create a GraphicsPath representing a selection on the image based on some sort of thresholding on the pixels making up the image. For instance a color range algorithm that takes into account pixels with color values close to a given color.

My problem at the moment is not on the algorithm to select pixels, but on the GraphicsPath which with my current code becomes far too complicated (too many points) and hence time-expensive to create it and especially to further manage it.

At the moment I'm simply using the following code (in this example I simply take opaque pixels) where I add a Rectangle for each taken pixel.

BitmapData bitmapData = sourceBitmap.LockBits(new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height), ImageLockMode.ReadOnly, sourceBitmap.PixelFormat);

GraphicsPath gp = new GraphicsPath();

unsafe
{
    byte* imagePointer = (byte*)bitmapData.Scan0;
    for (int x = 0; x < bitmapData.Width; x++)
    {
         for (int y = 0; y < bitmapData.Height; y++)
         {
              if (imagePointer[3] != 0)
                  gp.AddRectangle(new Rectangle(x, y, 1, 1));
              imagePointer += 4;
          }
     }
 }

How can I simplify this process or the resulting GraphicsPath to obtain a much simpler path, ideally with only contours?

EDIT I will then use to path to: display its contours on screen (running ants); use it as a mask for other images (cancel what is out from the path); do hit tests (whether a point is inside the path). As an example: enter image description here

Mauro Ganswer
  • 1,379
  • 1
  • 19
  • 33
  • Adding single pixels to a path indeed will not be efficient, infact it sounds rather crazy.. What exactly do you want to achieve with the path? Can you post an example image? You can use a Floodfill algorithm to find connected areas of same or similar pixels.. – TaW Oct 31 '16 at 19:24
  • I've added some uses I intend to do on the path. Could you please provide some more details on how to proceed with a floodfill? Once I have all the pixels I want to take, how do I generate the GraphicsPath? – Mauro Ganswer Oct 31 '16 at 19:48
  • What exactly do you want to achieve with the path? Can you post an example image? – TaW Oct 31 '16 at 19:50
  • I added a quick example of a selection by color range on a image. I would like to get that GraphicsPath comprising for instance all the orange colors – Mauro Ganswer Oct 31 '16 at 20:33
  • This is a really tough task and unless the images are very helpful you can't hope for any simple solution. If you take the example image you could easily __trace__ the selection by following pixels of the same hue, which would be the general approach but the general case will be rather difficult. I would try to find a free library that can help. – TaW Oct 31 '16 at 21:14

0 Answers0