def get_contour_boundary_points(contour): """Get boundary coordinates from contour""" # First convert the contour image to array contour_boundary = get_contour_boundary(contour) contour_boundary_array = sitkh.GetArrayFromImage(contour_boundary) boundary_index = np.asarray(np.nonzero(contour_boundary_array)) # Convert index to actual coordinates boundary_points = list() if len(contour.GetSize()) == 3: # 3D for i in range(boundary_index.shape[1]): boundary_points.append( contour_boundary.TransformContinuousIndexToPhysicalPoint([ boundary_index[0, i], boundary_index[1, i], boundary_index[2, i] ])) else: # Assume 2D for i in range(boundary_index.shape[1]): boundary_points.append( contour_boundary.TransformContinuousIndexToPhysicalPoint( [boundary_index[0, i], boundary_index[1, i]])) boundary_points = np.asarray(boundary_points) boundary_points = sort_points(boundary_points) # Add last point to ensure fully connected contour boundary_points = np.append(boundary_points, boundary_points[0:1], 0) return boundary_points
def get_masked_slices_mask(mask_image): ''' Remove the axial slices in a 3-D mask array for which there are non-zero elements. Parameters ---------- mask_image: numpy array, mandatory Boolean 3D array. Returns ----------- mask_sliced: numpy array, mandatory Array containing only those slices of which there are non-zero elements in the mask. ''' mask_array = sitkh.GetArrayFromImage(mask_image) # Filter out slices where there is no mask (need actual index here) mask_slices = np.flatnonzero(np.any(mask_array, axis=(0, 1))) if len(mask_slices) == 1: mask_sliced = mask_image[:, :, mask_slices[0]:(mask_slices[0] + 1)] else: mask_sliced = mask_image[:, :, mask_slices[0]:mask_slices[-1]] return mask_sliced
def get_masked_slices_mask(mask_image): mask_array = sitkh.GetArrayFromImage(mask_image) # Filter out slices where there is no mask (need actual index here) mask_slices = np.flatnonzero(np.any(mask_array, axis=(0, 1))) mask_sliced = mask_image[:, :, mask_slices[0]:mask_slices[-1]] return mask_sliced
def get_not_voi_voxels(contour, image): """Sets all voxels within VOI to 0""" contour = sitk.Cast(sitk.Not(contour), image.GetPixelID()) voi_voxels = image * contour voi_array = sitkh.GetArrayFromImage(voi_voxels) voi_array_nz = voi_array[np.nonzero(voi_array)] return voi_array, voi_array_nz
def get_voi_voxels(contour, image): """Gives back VOI (non-zero slices), and indices of slices""" contour = sitk.Cast(contour, image.GetPixelID()) # VOI is only where contour is 1 voi_voxels = image * contour voi_array = sitkh.GetArrayFromImage(voi_voxels) voi_array_nz = voi_array[np.nonzero(voi_array)] new_voi = list() voi_slices = list() for i_slice in range(0, voi_array.shape[2]): if np.count_nonzero(voi_array[:, :, i_slice]) > 0: new_voi.append(voi_array[:, :, i_slice]) voi_slices.append(i_slice) new_voi = np.asarray(new_voi) # Transpose because new_voi was constructed as list of slices, want slice # index last new_voi = np.transpose(new_voi, [1, 2, 0]) voi_slices = np.asarray(voi_slices) return new_voi, voi_array_nz, voi_slices