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: