Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mesh Segmentation #1627

Closed
lutfia95 opened this issue Sep 20, 2023 · 10 comments
Closed

Mesh Segmentation #1627

lutfia95 opened this issue Sep 20, 2023 · 10 comments

Comments

@lutfia95
Copy link

Hey,

I am trying to segment a mesh using MeshLib, I am not able to find the related source library or example. Do you an example for it?
Assuming this input mesh:

import meshlib.mrmeshpy as mrmeshpy
 
try:
    mesh = mrmeshpy.loadMesh(mrmeshpy.Path("mesh.stl"))
except ValueError as e:
    print(e)

Thanks!

Best,
Ahmad

@Grantim
Copy link
Contributor

Grantim commented Sep 20, 2023

Hello!
What kind of segmentation do you need? Can you please show visual example?

@lutfia95
Copy link
Author

Hey,

as I saw in the readme you mentioned two segmentation methods (Semi-auto voxel segmentation, Semi-auto mesh segmentation by curvature.) I am not sure how can I apply both of them to the mesh.

I got this example from "Link" so I want something like this.

image

@Grantim
Copy link
Contributor

Grantim commented Sep 21, 2023

Thanks for the image: you can have a look at https://github.com/MeshInspector/MeshLib/blob/master/test_python/test_segmentation.py for example of mesh segmentation, you need to specify source and sink faces for it,
also please you can try this functions:

from meshlib import mrmeshpy as mm
help(mm.surroundingContour)
help(mm.fillContourLeftByGraphCut)

the idea here is to pick 2 or 3 vertices (tincludeEdges, vertices in the origins are used) and create contour connecting them, and then separate inside and outside parts of the contour

@lutfia95
Copy link
Author

Hey @Grantim,

thank you very much! I was trying to test the segmentation but I think that I did something wrong or didn't understand it correctly.
I followed the same code snippets from your `test_segmentation.py'. And I used it as following:

from helper import *
import shutil

from meshlib import mrmeshpy as mrmesh

mesh = mrmesh.makeTorus()
mrmesh.saveMesh(mesh, mrmeshpy.Path("mesh_original.stl"))
metric = mrmesh.edgeLengthMetric(mesh)

source = mrmesh.FaceBitSet()
sink = mrmesh.FaceBitSet()

# For mesh segmentation, one has to specify source and sink faces for it.
source.resize(mesh.topology.getValidFaces().size(),False)
sink.resize(mesh.topology.getValidFaces().size(),False)

source.set(mrmesh.FaceId(0),True)
sink.set(mrmesh.FaceId(5),True)

res = mrmesh.segmentByGraphCut(mesh.topology,source,sink,metric)

The res object is an FaceBitSet, I am still not sure how can I save the segmented mesh.

@Grantim
Copy link
Contributor

Grantim commented Oct 20, 2023

Hello!
You can do so

from helper import *
import shutil

from meshlib import mrmeshpy as mrmesh

mesh = mrmesh.makeTorus()
mrmesh.saveMesh(mesh, mrmeshpy.Path("mesh_original.stl"))
metric = mrmesh.edgeLengthMetric(mesh)

source = mrmesh.FaceBitSet()
sink = mrmesh.FaceBitSet()

# For mesh segmentation, one has to specify source and sink faces for it.
source.resize(mesh.topology.getValidFaces().size(),False)
sink.resize(mesh.topology.getValidFaces().size(),False)

source.set(mrmesh.FaceId(0),True)
sink.set(mrmesh.FaceId(5),True)

res = mrmesh.segmentByGraphCut(mesh.topology,source,sink,metric)

bitSetToRemove = mesh.topology.getValidFaces() - res
mesh.topology.deleteFaces( bitSetToRemove )
mesh.pack()
mrmesh.saveMesh(mesh, mrmeshpy.Path("mesh_segmented.stl"))

@lutfia95
Copy link
Author

The idea is we segment out the faces and then remove them from the original mesh, so that we can store the segmented part only. Is there any way to save all segmented faces in other mesh file? And do you have examples for surroundingContour and fillContourLeftByGraphCut? I didn't find any documentation regarding segmentation.

Best,

@Grantim
Copy link
Contributor

Grantim commented Oct 20, 2023

I have edited code sample here hope it will help you. We have more options to copy segmented part in c++ code, but it is not exposed to python yet, we will add it later! About surroundingContour and fillContourLeftByGraphCut unfortunately we dont have examples.
You can find some description with help(mrmesh.surroundingContour) and help(mrmesh.fillContourLeftByGraphCut)
Basicaly:
surroundingContour finds best closed edge loop in terms of provided metric
fillContourLeftByGraphCut fills left part of the edge loop minimizing tonnels with provided metric.

@lutfia95
Copy link
Author

thank you very much! with C++ will work much better in my use case, as I'm trying to segment meshes completely using C++ in the first place. Is there any snippiest example for the C++ versions? basically my goal is to segment everything inside the mesh. same as here. Thank you very much for your support!

@Grantim
Copy link
Contributor

Grantim commented Oct 20, 2023

https://meshinspector.com/MeshLib/doc/group__MeshSegmentationGroup.html this link should be helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants