diff --git a/TPTBox/core/nii_wrapper.py b/TPTBox/core/nii_wrapper.py index 81e8b70..3481259 100755 --- a/TPTBox/core/nii_wrapper.py +++ b/TPTBox/core/nii_wrapper.py @@ -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) @@ -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] diff --git a/TPTBox/core/np_utils.py b/TPTBox/core/np_utils.py index 51bd178..9816f98 100755 --- a/TPTBox/core/np_utils.py +++ b/TPTBox/core/np_utils.py @@ -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) diff --git a/TPTBox/core/poi.py b/TPTBox/core/poi.py index e321daf..71cf2b3 100755 --- a/TPTBox/core/poi.py +++ b/TPTBox/core/poi.py @@ -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): @@ -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)