I'm trying to extrude a nice 3D form from the 2D binary image below using the code posted, but I haven't had any luck in figuring out the error that's keeping GraphicsComplex
from running. The end result should be the 3D points and a plot. Any help would certainly be appreciated!
pts= ImageData[testimage];
twoD = Rescale[Table[Thread[{pts[[i]], pts[[i]]}], {i, 1, Length[pts]}]];
extrude[pts_, h_] := Module[{vb, vt, len = Length[pts], nh, shape},
If[! NumericQ[h], nh = 0., nh = N@h];
vb = Table[{pts[[i, 1]], pts[[i, 2]], 0}, {i, len}];
vt = Table[{pts[[i, 1]], pts[[i, 2]], nh}, {i, len}];
shape =
GraphicsComplex[
Join[vb, vt], {Polygon[Range[len]],
Polygon[Append[
Table[{i, i + 1, len + i + 1, len + i}, {i, len - 1}], {len, 1,
len + 1, 2 len}]], Polygon[Range[len + 1, 2 len]]}]
]
Answer
One way to extrude a 3D object from a binary 2D image is to use RegionPlot3D
:
pts = ImageData[ColorNegate@Binarize@Import["http://i.stack.imgur.com/UWO6k.png"], "Bit"];
g = RegionPlot3D[pts[[Sequence @@ Round@{i, j}]] == 1, {i, 1, #1}, {j, 1, #2}, {z,
0, 1}, PlotPoints -> 100, Mesh -> False, Axes -> False, Boxed -> False] & @@ Dimensions[pts]
pts = Cases[g, x_GraphicsComplex :> First@x, Infinity]
Comments
Post a Comment