diff --git a/changelogs/master/fixed/20250321_fix_cv_shape_error.md b/changelogs/master/fixed/20250321_fix_cv_shape_error.md new file mode 100644 index 000000000..f01a4d237 --- /dev/null +++ b/changelogs/master/fixed/20250321_fix_cv_shape_error.md @@ -0,0 +1,12 @@ +# Fix error in `arithmetic._add_scalar_to_uint8_` on Windows + +On Python 3.8 tests for `_add_scalar_to_uint8_` fail for +windows due to dimension and dtype-specific behavior +in `cv2.add`. + +The fix the errors by directly adding scalar values +if the input image is 2D or 3D with a singular channel +dimension. + +If channel-dependent is required, broadcast the values +to full image shape and add it via `cv2.add`. \ No newline at end of file diff --git a/imgaug/augmenters/arithmetic.py b/imgaug/augmenters/arithmetic.py index 84f39bf9f..bbaee9d88 100644 --- a/imgaug/augmenters/arithmetic.py +++ b/imgaug/augmenters/arithmetic.py @@ -166,25 +166,18 @@ def _add_scalar_to_uint8_(image, value): value = round(value) elif ia.is_np_scalar(value) or ia.is_np_array(value): is_single_value = (value.size == 1) - value = np.round(value) if value.dtype.kind == "f" else value + value = np.round(value).astype(int) if value.dtype.kind == "f" else value else: is_single_value = False - is_channelwise = not is_single_value - - if image.ndim == 2 and is_single_value: - return cv2.add(image, value, dst=image, dtype=cv2.CV_8U) input_shape = image.shape - image = image.reshape(-1, 1) - values = np.array(value) - if not is_channelwise: - values = np.broadcast_to(values, image.shape) - else: - values = np.tile(values, image.size // len(values)) - image_add = cv2.add(image, values, dst=image, dtype=cv2.CV_8U) + if is_single_value and (image.ndim == 2 or (image.ndim == 3 and image.shape[-1] == 1)): + values = value + else: + values = np.broadcast_to(value, input_shape) - return image_add.reshape(input_shape) + return cv2.add(image, values, dtype=cv2.CV_8U).reshape(input_shape) def _add_scalar_to_non_uint8(image, value):