Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions changelogs/master/fixed/20250321_fix_cv_shape_error.md
Original file line number Diff line number Diff line change
@@ -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`.
19 changes: 6 additions & 13 deletions imgaug/augmenters/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading