How to get the number of closed contours? #477
-
Hi, first of all thanks for the great library! I'm trying to get the closed contours in dxf files with simple lines, arcs and circles. I attached a test.dxf (compressed to test.zip): test.zip So it has six closed contours. I started to get the start and end points, now I should connect the lines and arcs, but I don't know how to continue or how to use this information to continue. I want to connect them with something like `
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
This is not a DXF or ezdxf related question, but a question about a solution algorithm for a very special problem. You'd better post this question on StackOverflow, where it's much more likely to get an answer. |
Beta Was this translation helpful? Give feedback.
-
You can use OpenCV for finding closed contours. First draw the image and then apply CV algorithms on the output. Here, you can use: param = {"show_hatch": 0, "hatch_pattern": 0}
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1])
ctx = RenderContext(doc)
ctx.current_layout.set_colors(bg="#FFFFFFFF")
out = MatplotlibBackend(ax, params=param)
Frontend(ctx, out).draw_layout(doc.modelspace())
canvas = FigureCanvas(fig)
canvas.draw()
graph_image = np.array(fig.canvas.get_renderer()._renderer)
# convert to gray
imgGray = cv2.cvtColor(graph_image, cv2.COLOR_BGR2GRAY)
# binary threshold
img = cv2.threshold(imgGray, 127, 255, cv2.THRESH_BINARY_INV)[1] # ensure binary
# find contours
contours, hierarchy = cv2.findContours(fig, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours_poly = [None] * len(contours)
print("Number of contours: " + str(len(contours)))` For more information about algorithm, see: https://docs.opencv.org/master/d9/d8b/tutorial_py_contours_hierarchy.html |
Beta Was this translation helpful? Give feedback.
You can use OpenCV for finding closed contours. First draw the image and then apply CV algorithms on the output. Here, you can use: