Currently i have a black&white 3D model sliced in 2D images (+/- 500 to 1000 images), each layer is 0.05mm in Z, that aside i need to capture all closed black areas contours which i already have. I need to detect all empty spaces on the model looping all images and go up and down in Z at each countour area to find if is hollow space in a 3D prespective or if have any gap to outside the model and will not consider that area hollow.
Currently i'm doing the following:
- for first layer to last layer:
- Detect the required contours
- Foreach contour
- Create a empty image at same size of input
- FillConvexPoly at empty image
- Loop image pixels and find that previous draw pixels and save them in a array
That's the first part of the problem, what i'm doing is very inefficient/slow,... As can be observed each contour in layer image must loop all pixels in image, so if i have 5 countours on a image, it loop image 5 times to get pixels as a countour area.
Current code:
for (int i = 0; i < contours.Size; i++)
{
if ((int)arr.GetValue(0, i, 2) != -1 || (int)arr.GetValue(0, i, 3) == -1) continue;
grayscale.Dispose();
grayscale = new Image<Gray, byte>(image.Width, image.Height);
//grayscale = grayscale.CopyBlank();
grayscale.FillConvexPoly(contours[i].ToArray(), new Gray(125));
List<Point> points = new List<Point>();
byte[,,] data = grayscale.Data;
for (int y = grayscale.Rows - 1; y >= 0; y--)
{
for (int x = grayscale.Cols - 1; x >= 0; x--)
{
if(data[y, x, 0] != 125) continue;
points.Add(new Point(x, y)); // Gather pixels
}
}
}
The question: Is there any function or code i can use to skip 4 to 6 and get all pixels inside the matched countours?