How to convert a 2D image into a 3D graphics?



I have an image I would like to make into a .dxf and to do this I need a Graphics3D object. Is there anyway I can do this?

Can mathematica somehow vectorize a 2D image? I tried saving the image as a PDF then Export[path.dxf,Import[path.pdf]] after saving EdgeDetect[Image] as a PDF but AutoCad said it was a bad DXF and did not accept it.

Any solutions?

Edit: Clarification: I have a 2d image that i want to put into the .dxf format which I believe is 3d. In order to do that Mathematica wants a Export[path.dxf,3DGraphics object]. It's a 2d image though. This is for importation into AutoCAD. It says malformed shape when i do Export[path.dxf,Image] since I assume Image is 2DGraphics object

Eiyrioü von Kauyf

Posted 2012-04-13T01:41:17.030

Reputation: 1 705

Can this discussion help you? -

– Vitaliy Kaurov – 2012-04-13T02:30:26.610

Does the answer here help?

– rm -rf – 2012-04-13T02:44:42.333

The dxf standard can be used to describe 2D objects. Therefore, no specific need for Graphics3D. – Sjoerd C. de Vries – 2012-04-13T05:37:59.610

3PDFs don't necessarily contain vector graphics. The standard also includes bitmaps. So if you Export an image as PDF you won't automagically create vector graphics. – Sjoerd C. de Vries – 2012-04-13T05:44:06.310

1Mathematica DXF does export to some kind of 3D non-AutoCAD spec. Many programs accept that (e.g. Rhino 3D) but others do not. If you want to export 2D DXF let me know. I may have some awful legacy code that does work for some programs (not at hand though). – Yves Klett – 2012-04-13T08:17:11.670

For clarification, could you please post the actual (or simplified but essentially equivalent) code for the Graphics object you want to export? – Yves Klett – 2012-04-16T09:20:24.560

Well, basically I wanted/want to do edge recognition and then convert it into a 3d object in autocad though I haven't tried the Raster Design solution yet. Export[path.dxf,EdgeDetect[Import[path.jpeg]]] is what I'm looking at. – Eiyrioü von Kauyf – 2012-04-16T13:00:24.573



This maybe helpful if you want to convert image structure into 2D/3D line primitives, MorphologicalGraph does some astonishing things out of the box:

img = Import[

Mathematica graphics

g = MorphologicalGraph[img // MorphologicalBinarize, 
   VertexCoordinates -> Automatic, EdgeWeight -> Automatic];

edges = EdgeList[g];

extracting the actual connections can be done like this (although more streamlined solutions would be welcome):

vertices = 
  Thread[Rule[VertexList[g], PropertyValue[g, VertexCoordinates]]];

lines = ((edges /. vertices) /. 
    UndirectedEdge[a_, b_] :> Line[{a, b}]);


Mathematica graphics

or with a 3D touch:

 Tube[#] & /@ (lines /. {x_?NumericQ, y_?NumericQ} :> {x, 0, y})]

Mathematica graphics

After that, you can choose your preferred 2D/3D vector format for Export.

Yves Klett

Posted 2012-04-13T01:41:17.030

Reputation: 14 743


If you don't have any specific needs to do it with mathematica... why don't you use autocad raster design. It makes the most of raster images, maps, aerial photos, satellite imagery, and digital elevation models. With powerful raster editing and raster-to-vector conversion tools, AutoCAD Raster Design software helps you to easily clean up, edit, enhance, and maintain scanned drawings and plans in a familiar AutoCAD software environment. There are also many free software does that for you even online.


Posted 2012-04-13T01:41:17.030

Reputation: 4 429


If I understand your question correctly, you can include your 2D image in a 3D scene by texturing it on a square (or rectangle):

img = ExampleData[{"TestImage", "Lena"}];
g = Graphics3D[{Texture[img], 
  Polygon[{{0, 0, 0}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0}}, 
   VertexTextureCoordinates -> {{0, 0}, {1, 0}, {1, 1}, {0, 1}}]}]
Export["toto.dxf", g]

This does not give error messages, but I cannot tell you for sure that AutoCAD will open the resulting file.


Posted 2012-04-13T01:41:17.030

Reputation: 10 517

This does not seem to work with 8.04 on Win 7 (nor do I think with other versions so far). All I get is a polygon net with two triangular faces. If you look into the file, there is no trace of any texture. – Yves Klett – 2012-04-16T09:18:05.610