Skip to content

Commit a7a1366

Browse files
committed
fixed dimension error in cropping, added error message if OOB
1 parent af0b133 commit a7a1366

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/napari_cellseg3d/plugin_crop.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,27 @@ def add_crop_sliders(
275275
self._z = 0
276276

277277
# define crop sizes and boundaries for the image
278-
crop_sizes = (self._crop_size_x, self._crop_size_y, self._crop_size_z)
278+
crop_sizes = [self._crop_size_x, self._crop_size_y, self._crop_size_z]
279+
for i in range(len(crop_sizes)):
280+
if crop_sizes[i] > image_stack.shape[i]:
281+
crop_sizes[i] = image_stack.shape[i]
282+
warnings.warn(f"WARNING : Crop dimension in axis {i} was too large at {crop_sizes[i]}, it was set to {image_stack.shape[i]}")
279283
cropx, cropy, cropz = crop_sizes
280284
# shapez, shapey, shapex = image_stack.shape
281285
ends = np.asarray(image_stack.shape) - np.asarray(crop_sizes) + 1
286+
287+
288+
282289
stepsizes = ends // 100
283290

291+
print(f"Crop variables")
292+
print(crop_sizes)
293+
print(image_stack.shape)
294+
print(ends)
295+
print(stepsizes)
296+
284297
highres_crop_layer = vw.add_image(
285-
image_stack[:cropz, :cropy, :cropx],
298+
image_stack[:cropx, :cropy, :cropz],
286299
name="cropped",
287300
blending="additive",
288301
colormap="twilight_shifted",
@@ -292,7 +305,7 @@ def add_crop_sliders(
292305
if self.crop_labels:
293306
label_stack = self.label
294307
labels_crop_layer = vw.add_labels(
295-
self.label[:cropz, :cropy, :cropx],
308+
self.label[:cropx, :cropy, :cropz],
296309
name="cropped_labels",
297310
scale=self.label_layer.scale,
298311
)
@@ -312,14 +325,14 @@ def set_slice(axis, value, crp_lbl):
312325
cropz = self._crop_size_z
313326

314327
highres_crop_layer.data = image_stack[
315-
i : i + cropz, j : j + cropy, k : k + cropx
328+
i : i + cropx, j : j + cropy, k : k + cropz
316329
]
317330
highres_crop_layer.translate = scale * izyx
318331
highres_crop_layer.refresh()
319332

320333
if crp_lbl:
321334
labels_crop_layer.data = label_stack[
322-
i : i + cropz, j : j + cropy, k : k + cropx
335+
i : i + cropx, j : j + cropy, k : k + cropz
323336
]
324337
labels_crop_layer.translate = scale * izyx
325338
labels_crop_layer.refresh()

0 commit comments

Comments
 (0)