简体   繁体   English

如何从 python 中的二进制图像生成二维网格?

[英]How to generate a 2d mesh from a binary image in python?

I'm digging in scikits image toolbox and similars in order to manipulate image data in python.我正在挖掘 scikits 图像工具箱和类似工具,以便在 python 中操作图像数据。

When we have a binary (x,y) image how could we use it as mask to generate a mesh inside the image limits?当我们有一个二进制 (x,y) 图像时,我们如何将它用作掩码以在图像范围内生成网格? I want to export this mesh to a CAE program.我想将此网格导出到 CAE 程序。 So, I need to collect the mesh coordinates and also the element list所以,我需要收集网格坐标和元素列表

I found out some tools such as meshpy, but I didn't figure it out how can I solve this.我找到了一些工具,例如 meshpy,但我没有弄清楚如何解决这个问题。

Thank you谢谢

The following solution is based on MeshLib python package.以下解决方案基于MeshLib python package。

Let us have a binary image with 3 coins:让我们有一个包含 3 个硬币的二值图像: 3个硬币

Then one can convert it in triangular mesh as follows:然后可以将其转换为三角网格,如下所示:

import meshlib.mrmeshpy as mr
# load raster image:
dm = mr.loadDistanceMapFromImage(mr.Path("Binary_coins.png"), 0)
# find the boundary contour between black and white:
polyline2 = mr.distanceMapTo2DIsoPolyline(dm, isoValue=127)
# compute the triangulation inside the contour
mesh = mr.triangulateContours(polyline2.contours2())
# save 2D triangulation in a textual OBJ file:
mr.saveMesh(mesh, mr.Path("Binary_coins.obj"))

At this moment we got这时我们得到了

二维三角剖分

To construct a mesh with not that much degenerate triangles, long edges can be subdivided:要构建没有那么多退化三角形的网格,可以细分长边:

# by default split 1000 edges:
mr.subdivideMesh(mesh)
# save 2D mesh in a textual OBJ file:
mr.saveMesh(mesh, mr.Path("Binary_coins1.obj"))

Our final result:我们的最终结果:

二维网格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM