Exemplo n.º 1
0
        def exportMeshes(object_names, obj_filepaths):
            n_objects = len(object_names)
            progress_update = 100 / n_objects
            try:
                for obj, obj_path, obj_n in zip(object_names, obj_filepaths,
                                                range(n_objects)):
                    object_volume = get_label_volume_from_mst(mst, obj)
                    unique_ids = len(numpy.unique(object_volume))

                    if unique_ids <= 1:
                        logger.info(f"No voxels found for {obj}, skipping")
                        continue
                    elif unique_ids > 2:
                        logger.info(
                            f"Supervoxel segmentation not unique for {obj}, skipping, got {unique_ids}"
                        )
                        continue

                    logger.info(f"Generating mesh for {obj}")
                    _, mesh_data = list(labeling_to_mesh(object_volume,
                                                         [1]))[0]
                    self.parentApplet.progressSignal(
                        (obj_n + 0.5) * progress_update)
                    logger.info(f"Mesh generation for {obj} complete.")

                    logger.info(f"Saving mesh for {obj} to {obj_path}")
                    mesh_to_obj(mesh_data, obj_path, obj)
                    self.parentApplet.progressSignal(
                        (obj_n + 1) * progress_update)
            finally:
                self.parentApplet.busy = False
                self.parentApplet.progressSignal(100)
                self.parentApplet.appletStateUpdateRequested()
Exemplo n.º 2
0
        def onMeshesComplete(mesh):
            """
            Called when mesh extraction is complete.
            Writes the extracted mesh to an .obj file
            """
            logger.info( "Mesh generation complete." )

            # FIXME: the old comment: Mesh count can sometimes be 0 for the '<not saved yet>' object...
            # FIXME: is this still relevant???
            '''
            mesh_count = len( window.extractor.meshes )
            if mesh_count > 0:
                assert mesh_count == 1, \
                    "Found {} meshes processing object '{}',"\
                    "(only expected 1)".format( mesh_count, object_name )
                mesh = list(window.extractor.meshes.values())[0]
                logger.info( "Saving meshes to {}".format( obj_filepath ) )
    
                # Use VTK to write to a temporary .vtk file
                tmpdir = tempfile.mkdtemp()
                vtkpoly_path = os.path.join(tmpdir, 'meshes.vtk')
                w = vtkPolyDataWriter()
                w.SetFileTypeToASCII()
                w.SetInput(mesh)
                w.SetFileName(vtkpoly_path)
                w.Write()
                
                # Now convert the file to .obj format.
                convertVTPtoOBJ(vtkpoly_path, obj_filepath)
            '''
            logger.info("Saving meshes to {}".format(obj_filepath))
            mesh_to_obj(mesh, obj_filepath, object_name)
            # Cleanup: We don't need the window anymore.
            window.setParent(None)

            # If there are still objects left to process,
            #   start again with the remainder of the list.
            if object_names:
                self._exportMeshes(object_names, obj_filepaths)
Exemplo n.º 3
0
        def onMeshesComplete(mesh):
            """
            Called when mesh extraction is complete.
            Writes the extracted mesh to an .obj file
            """
            logger.info("Mesh generation complete.")

            # FIXME: the old comment: Mesh count can sometimes be 0 for the '<not saved yet>' object...
            # FIXME: is this still relevant???
            '''
            mesh_count = len( window.extractor.meshes )
            if mesh_count > 0:
                assert mesh_count == 1, \
                    "Found {} meshes processing object '{}',"\
                    "(only expected 1)".format( mesh_count, object_name )
                mesh = list(window.extractor.meshes.values())[0]
                logger.info( "Saving meshes to {}".format( obj_filepath ) )
    
                # Use VTK to write to a temporary .vtk file
                tmpdir = tempfile.mkdtemp()
                vtkpoly_path = os.path.join(tmpdir, 'meshes.vtk')
                w = vtkPolyDataWriter()
                w.SetFileTypeToASCII()
                w.SetInput(mesh)
                w.SetFileName(vtkpoly_path)
                w.Write()
                
                # Now convert the file to .obj format.
                convertVTPtoOBJ(vtkpoly_path, obj_filepath)
            '''
            logger.info("Saving meshes to {}".format(obj_filepath))
            mesh_to_obj(mesh, obj_filepath, object_name)
            # Cleanup: We don't need the window anymore.
            window.setParent(None)

            # If there are still objects left to process,
            #   start again with the remainder of the list.
            if object_names:
                self._exportMeshes(object_names, obj_filepaths)
Exemplo n.º 4
0
from h5py import File
from volumina.view3d.meshgenerator import labeling_to_mesh, mesh_to_obj

if __name__ == "__main__":
    parser = ArgumentParser()
    parser.add_argument("--label", type=int, required=False)
    parser.add_argument("input_hdf5_file")
    parsed_args = parser.parse_args()

    output_file = os.path.splitext(parsed_args.input_hdf5_file)[0] + ".obj"

    print("Loading volume...")
    with File(parsed_args.input_hdf5_file, "r") as f_input:
        dataset_names = []
        f_input.visit(dataset_names.append)
        if len(dataset_names) != 1:
            stderr.write("Input HDF5 file should have exactly 1 dataset.\n")
            sysexit(1)
        volume = f_input[dataset_names[0]][:].squeeze()

    if parsed_args.label:
        volume[:] = volume == parsed_args.label
        labels = [parsed_args.label]
    else:
        volume[:] = volume != 0
        labels = [label for label in unique(volume) if label != 0]

    for label, mesh in labeling_to_mesh(volume, labels):
        mesh_to_obj(mesh, "{}_{}.obj".format(output_file, label), label)