Closed RasmusGrift96 closed 3 years ago
It depends on what you want the scope of the problem to be. A bitmap image consists of pixels not geometric objects such as lines. The main challenge will be extracting the geometry from the image. At that point, constructing a dxf file from the geometry is almost trivial in comparison. I don't want to discourage you but this is a very challenging problem.
You asked for where to start so I will give you an overview of where you might want to look:
If you choose to constrain the problem to just look for straight lines in an image and generate a dxf file containing those lines then I think that should be achievable. I would suggest using a probabilistic Hough transform to detect the line segments. OpenCV has a function which you can use for this: HoughLinesP. There is also LSD although from memory I think this detector is intended for real world images but either should be fine. Either way you will probably get a lot of outliers that you want to filter out.
Thanks for your prompt reply. I want to use this to convert images of black lines on White background to DXF sketches I can use in solid works. (The images are not that complex, Just black and white pixels)
I'm well aware of the various computer vision techniques. I already have my images ready in Python and I can detect the line segments. I'm just not sure how I should input these images to this library. Does it have something to do with msp.add_text?
Sorry for the questions. Kind regards R
I'm confused. some of your statements seem contradictory:
I can detect the line segments
: So you want to add lines to a dxf file? see herehow I should input these images to this library
: so do you want to create a dxf file with the images embedded inside the file? then see here (note that the file doesn't actually include the image content, but links to it and the CAD application will load in the image separately), but then why are you detecting line segments at all?Does it have something to do with msp.add_text
: what does converting images of black lines
have anything to do with text?I think we are confusing each other haha. So If I understand correctly this will probably be the process:
Not sure about the last two steps
Thanks in advance
note that a standard Hough transform detects lines not line segments, but that's a technicality.
I'm surprised that you can handle performing a Hough transform but not reading the ezdxf documentation but regardless, would something like this be what you are looking for:
from typing import Tuple, List
import ezdxf
from ezdxf.document import Drawing
from ezdxf.addons.drawing.matplotlib import qsave
Point = Tuple[float, float]
def create_dxf_from_lines(lines: List[Tuple[Point, Point]]) -> Drawing:
doc = ezdxf.new()
layout = doc.modelspace()
for a, b in lines:
layout.add_line(a, b)
return doc
def main():
lines = [
((0, 0), (1, 1)),
((0, 1), (0, 2))
]
doc = create_dxf_from_lines(lines)
doc.saveas('out.dxf')
qsave(doc.modelspace(), 'out.png')
if __name__ == '__main__':
main()
the qsave is optional for if you want to also render your dxf back to an image. this might be useful for debugging.
Thanks for taking the time! That's very useful!
Kind regards
Hi,
I want to convert a bunch of images to DXF, just like it can be done at https://convertio.co/
It looks like ezdxf can be the solution but I'm not really sure where to start.
I upload an image -> do something with it -> than I save/write it as a DXF.
Thanks in advance! R