I’m trying to convert a stack of images to a 3D mesh.
First as test i’m trying to convert just 1 layer, i’m using Subdiv2D to get the triangles from the scanned contours, but it render the holes as positive areas in the mesh which i can understand why.
Source image:
Result:
My question: It is possible to subtract the holes to the mesh?
My code:
using var mat = CvInvoke.Imread(@"D:\layer0.png", ImreadModes.Grayscale);
var contours = mat.FindContours(out var hierarchy, RetrType.Tree, ChainApproxMethod.ChainApproxTc89Kcos);
using var mesh = new STLMeshFile(@"D:\test.stl", FileMode.Create);
mesh.BeginWrite();
var groups = EmguContours.GetPositiveContoursInGroups(contours, hierarchy);
foreach (var group in groups)
{
for (int i = 0; i < group.Size; i++)
{
var list = new List<PointF>();
for (int x = 0; x < contours[i].Size; x++)
{
list.Add(contours[i][x]);
}
using var sub = new Subdiv2D(list.ToArray());
var triangles = sub.GetDelaunayTriangles();
foreach (var triangle2Df in triangles)
{
mesh.WriteTriangle(
new Vector3(triangle2Df.V0.X, triangle2Df.V0.Y, 1f),
new Vector3(triangle2Df.V1.X, triangle2Df.V1.Y, 1f),
new Vector3(triangle2Df.V2.X, triangle2Df.V2.Y, 1f),
Vector3.One
);
}
}
break;
}
mesh.EndWrite();
EmguContours.GetPositiveContoursInGroups: Groups contours into separate objects, eg, index 0 contains all the contours to correctly draw 1 cog (including the holes)