20

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

VividD
  • 3,660
  • 4
  • 26
  • 42
Eiyrioü von Kauyf
  • 1,735
  • 1
  • 16
  • 20
  • Can this discussion help you? - http://mathematica.stackexchange.com/questions/637/character-edge-finding/638#638 – Vitaliy Kaurov Apr 13 '12 at 02:30
  • Does the answer here help? http://mathematica.stackexchange.com/q/1003/5 – rm -rf Apr 13 '12 at 02:44
  • The dxf standard can be used to describe 2D objects. Therefore, no specific need for Graphics3D. – Sjoerd C. de Vries Apr 13 '12 at 05:37
  • 3
    PDFs 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 Apr 13 '12 at 05:44
  • 1
    Mathematica 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 Apr 13 '12 at 08:17
  • For clarification, could you please post the actual (or simplified but essentially equivalent) code for the Graphics object you want to export? – Yves Klett Apr 16 '12 at 09:20
  • 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 Apr 16 '12 at 13:00

3 Answers3

28

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[
   "https://upload.wikimedia.org/wikipedia/commons/c/ce/Spinnennetz_\
im_Gegenlicht.jpg"];

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}]);

Graphics[lines]

Mathematica graphics

or with a 3D touch:

Graphics3D[
 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
  • 15,383
  • 5
  • 57
  • 124
2

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.

s.s.o
  • 4,559
  • 2
  • 27
  • 42
1

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.

F'x
  • 10,817
  • 3
  • 52
  • 92
  • 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 Apr 16 '12 at 09:18