Skip to content

Commit

Permalink
made autocrop work if the image was cropped previously
Browse files Browse the repository at this point in the history
  • Loading branch information
MeyerBender committed Nov 21, 2024
1 parent c15571b commit 0574c3e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
8 changes: 0 additions & 8 deletions docs/notebooks/FAQ.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@
" gc.collect() # manually calling the garbage collector after each iteration\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d46c2043-de87-4b96-9db8-410b243aeef4",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
3 changes: 2 additions & 1 deletion spatialproteomics/pl/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1426,5 +1426,6 @@ def autocrop(
if channel is None:
channel = self._obj.coords[Dims.CHANNELS].values.tolist()[0]
img = self._obj.pp[channel].pp.downsample(downsample)[key].values.squeeze()
slices = _autocrop(img, downsample=downsample, padding=padding)
bounds = self._obj.pl._get_bounds()
slices = _autocrop(img, bounds=bounds, downsample=downsample, padding=padding)
return self._obj.pp[slices[0], slices[1]]
18 changes: 15 additions & 3 deletions spatialproteomics/pl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,19 +310,19 @@ def _set_up_subplots(num_plots: int = 1, ncols: int = 4, width: int = 4, height:
return fig, axes


def _autocrop(img: np.ndarray, padding: int = 50, downsample: int = 10):
def _autocrop(img: np.ndarray, bounds=List, padding: int = 50, downsample: int = 10):
"""
Crop an image based on the regions of interest so that the background around the tissue/TMA gets cropped away.
Parameters:
img (np.ndarray): The input image as a NumPy array.
bounds (list): The bounds for the cropping. Should be given as [xmin, xmax, ymin, ymax].
padding (int, optional): The padding to be added around the cropped image in pixels. Defaults to 50.
downsample (int, optional): The downsample factor to be used for the cropping. Defaults to 10.
Returns:
tuple: A tuple containing two slices representing the cropped image.
"""

bw = closing(img > np.quantile(img, 0.8), square(20))
label_image = label(bw)
props = regionprops(label_image)
Expand All @@ -336,10 +336,22 @@ def _autocrop(img: np.ndarray, padding: int = 50, downsample: int = 10):
region = props[max_idx]
minr, minc, maxr, maxc = region.bbox

return slice(downsample * minc - padding, downsample * maxc + padding), slice(
slices = slice(downsample * minc - padding, downsample * maxc + padding), slice(
downsample * minr - padding, downsample * maxr + padding
)

# getting the minimum x and y bounds
min_bounds = [bounds[0], bounds[2]]
# adding the offset from the bounds
slices = [slice(s.start + min_bounds[i], s.stop + min_bounds[i]) for i, s in enumerate(slices)]
# if the slices are outside of the bounds, we need to adjust them
slices = [
slice(max(bounds[0], slices[0].start), min(bounds[1], slices[0].stop)),
slice(max(bounds[2], slices[1].start), min(bounds[3], slices[1].stop)),
]

return slices


def _compute_erosion(segmentation: np.ndarray, erosion_strength: int = 5) -> np.ndarray:
# Binarize the segmentation array: values > 0 become 1, others become 0
Expand Down

0 comments on commit 0574c3e

Please sign in to comment.