|
| 1 | +import vtk |
| 2 | +import numpy as np |
| 3 | +from vtk.util.numpy_support import vtk_to_numpy, numpy_to_vtk |
| 4 | +from save_vti import save_vti |
| 5 | +from scipy.ndimage import label |
| 6 | +from load_vti import load_vti |
| 7 | + |
| 8 | +from scipy.ndimage import zoom |
| 9 | +from scipy.spatial import KDTree |
| 10 | + |
| 11 | + |
| 12 | +def upscale_source_to_target(source_array, target_shape): |
| 13 | + """ |
| 14 | + Upscale the source_array to the given target_shape using interpolation. |
| 15 | +
|
| 16 | + Parameters: |
| 17 | + - source_array (np.ndarray): The 3D array you want to upscale. |
| 18 | + - target_shape (tuple): The desired shape (z, y, x) to upscale to. |
| 19 | +
|
| 20 | + Returns: |
| 21 | + - np.ndarray: The upscaled 3D array. |
| 22 | + """ |
| 23 | + |
| 24 | + # Calculate the scaling factors for each dimension |
| 25 | + z_scale = target_shape[0] / source_array.shape[0] |
| 26 | + y_scale = target_shape[1] / source_array.shape[1] |
| 27 | + x_scale = target_shape[2] / source_array.shape[2] |
| 28 | + |
| 29 | + # Use zoom to upscale |
| 30 | + upscaled_array = zoom(source_array, (z_scale, y_scale, x_scale)) |
| 31 | + |
| 32 | + return upscaled_array |
| 33 | + |
| 34 | + |
| 35 | +def map_source_to_target_using_kdtree(source_array, target_array): |
| 36 | + # Convert voxel data to points (non-zero voxels can be considered as points). |
| 37 | + source_points = np.argwhere(source_array) |
| 38 | + target_points = np.argwhere(target_array) |
| 39 | + |
| 40 | + # Create a KDTree from target points. |
| 41 | + tree = KDTree(target_points) |
| 42 | + |
| 43 | + # For each point in the source, find its nearest neighbor in the target. |
| 44 | + distances, indices = tree.query(source_points) |
| 45 | + |
| 46 | + # The above gives the nearest target point for each source point. |
| 47 | + # Now you can map or manipulate the source based on this information. |
| 48 | + |
| 49 | + return distances, indices |
| 50 | + |
| 51 | + |
| 52 | +def main(): |
| 53 | + # Sample usage: |
| 54 | + source_filename = "hackathon-dataset\seed-001-potts_3d.50.vti" |
| 55 | + target_filename = "voxelized_stl\cube.vti" |
| 56 | + |
| 57 | + source_vti = load_vti(source_filename) |
| 58 | + target_vti = load_vti(target_filename) |
| 59 | + |
| 60 | + dims = target_vti.GetDimensions() |
| 61 | + |
| 62 | + print(target_vti) |
| 63 | + |
| 64 | + source_array = vtk_to_numpy(source_vti.GetCellData().GetScalars()) # Your 3D source array |
| 65 | + source_array = source_array.reshape(100, 100, 100) |
| 66 | + target_shape = (dims[2], dims[1], dims[0]) # Replace with the shape of your target VTK |
| 67 | + target_array = vtk_to_numpy(target_vti.GetPointData().GetScalars()) |
| 68 | + |
| 69 | + |
| 70 | + upscaled_source = upscale_source_to_target(source_array, target_shape) |
| 71 | + distances, indices = map_source_to_target_using_kdtree(upscaled_source, target_array) |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + main() |
0 commit comments