Coverage for src/abm_shape_collection/construct_mesh_from_array.py: 100%

13 statements  

« prev     ^ index     » next       coverage.py v7.1.0, created at 2024-09-25 19:34 +0000

1import numpy as np 

2from aicsshparam import shtools 

3from vtk import vtkPolyData # pylint: disable=no-name-in-module 

4from vtk.util import numpy_support # pylint: disable=no-name-in-module, import-error 

5 

6 

7def construct_mesh_from_array(array: np.ndarray, reference: np.ndarray) -> vtkPolyData: 

8 """ 

9 Construct a mesh from binary image array. 

10 

11 Parameters 

12 ---------- 

13 array 

14 Binary image array. 

15 reference 

16 Binary reference array that determines alignment angle and centroid. 

17 

18 Returns 

19 ------- 

20 : 

21 Mesh object. 

22 """ 

23 

24 _, angle = shtools.align_image_2d(image=reference) 

25 aligned_reference = shtools.apply_image_alignment_2d(reference, angle).squeeze() 

26 aligned_array = shtools.apply_image_alignment_2d(array, angle).squeeze() 

27 

28 mesh, _, _ = shtools.get_mesh_from_image( 

29 image=aligned_array, lcc=False, translate_to_origin=False 

30 ) 

31 

32 centroid = np.argwhere(aligned_reference == 1).mean(axis=0)[[2, 1, 0]] 

33 coords = numpy_support.vtk_to_numpy(mesh.GetPoints().GetData()) - centroid 

34 mesh.GetPoints().SetData(numpy_support.numpy_to_vtk(coords)) 

35 

36 return mesh