Skip to content

Commit c278ce5

Browse files
authored
Merge pull request #104 from zhujun98/reset_unmatched_empty_image_mask
Reset the image mask in case of different shapes if it is empty
2 parents d17ea18 + 63d05f6 commit c278ce5

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

extra_foam/gui/image_tool/tests/test_image_tool.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,15 @@ def testDrawMask(self):
330330
np.testing.assert_array_equal(mask_gt, proc._image_mask)
331331

332332
# test set a mask which has a different shape from the image
333-
mask_gt = np.zeros((2, 2), dtype=np.bool)
333+
mask_gt = np.ones((2, 2), dtype=np.bool)
334334
pub.set(mask_gt)
335335
with self.assertRaises(ImageProcessingError):
336336
proc.process(data)
337+
# an empty image mask with a different shape will be automatically reset
338+
mask_gt = np.zeros((2, 2), dtype=np.bool)
339+
pub.set(mask_gt)
340+
proc.process(data)
341+
np.testing.assert_array_equal(np.zeros((10, 10), dtype=np.bool), proc._image_mask)
337342

338343
def testBulletinView(self):
339344
processed = ProcessedData(1357)

extra_foam/pipeline/processors/image_processor.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,20 @@ def _record_dark(self, assembled):
193193
def _update_image_mask(self, image_shape):
194194
image_mask = self._mask_sub.update(self._image_mask, image_shape)
195195
if image_mask is not None and image_mask.shape != image_shape:
196-
# This could only happen when the mask is loaded from the files
197-
# and the image shapes in the ImageTool is different from the
198-
# shape of the live images.
199-
# The original image mask remains the same.
200-
raise ImageProcessingError(
201-
f"[Image processor] The shape of the image mask "
202-
f"{image_mask.shape} is different from the shape of the image "
203-
f"{image_shape}!")
196+
if np.sum(image_mask) == 0:
197+
# reset the empty image mask automatically
198+
image_mask = None
199+
else:
200+
# This could only happen when the mask is loaded from the files
201+
# and the image shapes in the ImageTool is different from the
202+
# shape of the live images.
203+
# The original image mask remains the same.
204+
raise ImageProcessingError(
205+
f"[Image processor] The shape of the image mask "
206+
f"{image_mask.shape} is different from the shape of the image "
207+
f"{image_shape}!")
204208

205-
elif image_mask is None:
209+
if image_mask is None:
206210
image_mask = np.zeros(image_shape, dtype=np.bool)
207211

208212
self._image_mask = image_mask

0 commit comments

Comments
 (0)