Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions TPTBox/core/nii_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,23 @@ def normalize_to_range_(self, min_value: int = 0, max_value: int = 1500, verbose
self.set_dtype_(self_dtype)
log.print(f"Shifted from range {mi, ma} to range {self.min(), self.max()}", verbose=verbose)

def get_histogram(self, bins=256, hrange=None, density=False, c_val:float|None=None):
"""Returns the histogram of the image array.

Args:
bins (int, optional): Number of bins for the histogram. Defaults to 256.
range (tuple, optional): Range of values to consider for the histogram. Defaults to None.
density (bool, optional): If True, the result is the probability density function at the bin, normalized such that the integral over the range is 1. Defaults to False.
c_val (float|None, optional): The value below which all values are set to c_val. Defaults to None.

Returns:
tuple: A tuple containing the histogram values and the bin edges.
"""
arr = self.get_array()
if c_val is not None:
arr[arr <= c_val] = c_val
return np.histogram(arr, bins=bins, range=hrange, density=density)

def match_histograms(self, reference:Image_Reference,c_val = 0,inplace=False):
assert not self.seg
ref_nii = to_nii(reference)
Expand Down Expand Up @@ -1268,6 +1285,7 @@ def filter_connected_components(self, labels: int |list[int]|None=None,min_volum
"""
assert self.seg, "This only works on segmentations"
arr = np_filter_connected_components(self.get_seg_array(), largest_k_components=max_count_component,label_ref=labels,connectivity=connectivity,return_original_labels=keep_label,min_volume=min_volume,max_volume=max_volume,removed_to_label=removed_to_label,)
assert arr.shape == self.shape, f"Shape mismatch: {arr.shape} != {self.shape}"
if keep_label and labels is not None:
if isinstance(labels,int):
labels = [labels]
Expand Down
2 changes: 1 addition & 1 deletion TPTBox/core/np_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def np_dilate_msk(
data = out.copy()
data[i != data] = 0
if use_crop:
lcrop = np_bbox_binary(data, px_dist=2, raise_error=False)
lcrop = np_bbox_binary(data, px_dist=2 + n_pixel, raise_error=False)
data = data[lcrop]
msk_ibe_data = _binary_dilation(data, struct=struct)

Expand Down
3 changes: 2 additions & 1 deletion TPTBox/core/poi.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ def apply_crop(self: Self, o_shift: tuple[slice, slice, slice] | Sequence[slice]
"""
origin: COORDINATE = None # type: ignore
shape = None # type: ignore
o_shift = tuple(o if o.start is not None else slice(0, None) for o in o_shift)
try:

def shift(x, y, z):
Expand Down Expand Up @@ -1066,7 +1067,7 @@ def calc_centroids_from_two_masks(
org_shape = subreg_msk.shape
# crop to mask to speed up the segmentation
crop = vert_msk.compute_crop()
crop = subreg_msk.compute_crop(maximum_size=crop)
# crop = subreg_msk.compute_crop(maximum_size=crop)
# crop = (slice(0, subreg_msk.shape[0]), slice(0, subreg_msk.shape[1]), slice(0, subreg_msk.shape[2]))

vert_msk = vert_msk.apply_crop(crop)
Expand Down