Skip to content

Commit af608cb

Browse files
committed
Fixes #7: ROI rectangles with negative coords
1 parent b069915 commit af608cb

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ for future and past milestones.
1313
The result was an image positioned at the origin (0, 0) instead of the expected
1414
position (x0, y0) and the ROI rectangle itself was not removed as expected.
1515
This is now fixed (see [Issue #6](https://github.com/Codra-Ingenierie-Informatique/DataLab/issues/6) - 'Extract multiple ROI' feature: unexpected result for a single ROI)
16+
* ROI rectangles with negative coordinates were not properly handled:
17+
ROI extraction was raising a `ValueError` exception, and the image mask was not
18+
displayed properly.
19+
This is now fixed (see [Issue #7](https://github.com/Codra-Ingenierie-Informatique/DataLab/issues/7) - Image ROI extraction: `ValueError: zero-size array to reduction operation minimum which has no identity`)
1620
* ROI extraction was not taking into account the pixel size (dx, dy) and the origin
1721
(x0, y0) of the image.
1822
This is now fixed (see [Issue #8](https://github.com/Codra-Ingenierie-Informatique/DataLab/issues/8) - Image ROI extraction: take into account pixel size)

cdl/core/computation/image/__init__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,8 @@ def extract_multiple_roi(src: ImageObj, group: gds.DataSetGroup) -> ImageObj:
527527
Returns:
528528
ImageObj: output image object
529529
"""
530-
x0 = min(p.x0 for p in group.datasets)
531-
y0 = min(p.y0 for p in group.datasets)
530+
x0 = max(min(p.x0 for p in group.datasets), 0)
531+
y0 = max(min(p.y0 for p in group.datasets), 0)
532532
x1 = max(p.x1 for p in group.datasets)
533533
y1 = max(p.y1 for p in group.datasets)
534534
suffix = None
@@ -540,12 +540,11 @@ def extract_multiple_roi(src: ImageObj, group: gds.DataSetGroup) -> ImageObj:
540540
dst.y0 += y0 * src.dy
541541
dst.roi = None
542542
if len(group.datasets) == 1:
543-
p = group.datasets[0]
544-
dst.data = src.data.copy()[p.y0 : p.y1, p.x0 : p.x1]
543+
dst.data = src.data.copy()[y0:y1, x0:x1]
545544
return dst
546545
out = np.zeros_like(src.data)
547546
for p in group.datasets:
548-
slice1, slice2 = slice(p.y0, p.y1 + 1), slice(p.x0, p.x1 + 1)
547+
slice1, slice2 = slice(max(p.y0, 0), p.y1 + 1), slice(max(p.x0, 0), p.x1 + 1)
549548
out[slice1, slice2] = src.data[slice1, slice2]
550549
dst.data = out[y0:y1, x0:x1]
551550
return dst
@@ -560,9 +559,10 @@ def extract_single_roi(src: ImageObj, p: gds.DataSet) -> ImageObj:
560559
ImageObj: output image object
561560
"""
562561
dst = dst_11(src, "extract_single_roi", p.get_suffix())
563-
dst.data = src.data.copy()[p.y0 : p.y1, p.x0 : p.x1]
564-
dst.x0 += p.x0 * src.dx
565-
dst.y0 += p.y0 * src.dy
562+
x0, y0, x1, y1 = max(p.x0, 0), max(p.y0, 0), p.x1, p.y1
563+
dst.data = src.data.copy()[y0:y1, x0:x1]
564+
dst.x0 += x0 * src.dx
565+
dst.y0 += y0 * src.dy
566566
dst.roi = None
567567
if p.geometry is RoiDataGeometries.CIRCLE:
568568
# Circular ROI

cdl/core/model/image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def apply_mask(self, data: np.ndarray, yxratio: float) -> np.ndarray:
166166
roi_mask = np.ones_like(data, dtype=bool)
167167
x0, y0, x1, y1 = self.get_rect()
168168
if self.geometry is RoiDataGeometries.RECTANGLE:
169-
roi_mask[y0:y1, x0:x1] = False
169+
roi_mask[max(y0, 0) : y1, max(x0, 0) : x1] = False
170170
else:
171171
xc, yc = 0.5 * (x0 + x1), 0.5 * (y0 + y1)
172172
radius = 0.5 * (x1 - x0)

0 commit comments

Comments
 (0)