Skip to content

Subtracting a tuple from an image gives error: (-215:Assertion failed) type2 == CV_64F && (sz2.height == 1 || sz2.height == 4) in function 'cv::arithm_op' #24057

@ky28059

Description

@ky28059

System Information

OpenCV python version: 4.8.0.74
Operating system: Windows 11 22H2
Python version: 3.10.11

Detailed description

From what I understand of the docs, cv2.subtract() should be able to subtract a tuple from an array if it has the same width as the array has channels:

Difference between an array and a scalar, when src2 is constructed from Scalar or has the same number of elements as src1.channels():

However, loading an image with imread and attempting to subtract a tuple gives

>>> cv2.subtract(mat, (10, 10, 10))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\core\src\arithm.cpp:652: error: (-215:Assertion failed) type2 == CV_64F && (sz2.height == 1 || sz2.height == 4) in function 'cv::arithm_op'

The same error occurs when trying to use a tuple of floats

>>> cv2.subtract(mat, (10.0, 10.0, 10.0))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\core\src\arithm.cpp:652: error: (-215:Assertion failed) type2 == CV_64F && (sz2.height == 1 || sz2.height == 4) in function 'cv::arithm_op'

and numpy arrays of various types

>>> cv2.subtract(mat, np.uint8([10, 10, 10]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\core\src\arithm.cpp:652: error: (-215:Assertion failed) type2 == CV_64F && (sz2.height == 1 || sz2.height == 4) in function 'cv::arithm_op'

>>> cv2.subtract(mat, np.array([10, 10, 10]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\core\src\arithm.cpp:652: error: (-215:Assertion failed) type2 == CV_64F && (sz2.height == 1 || sz2.height == 4) in function 'cv::arithm_op'

>>> cv2.subtract(mat, np.float64([10, 10, 10]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\core\src\arithm.cpp:652: error: (-215:Assertion failed) type2 == CV_64F && (sz2.height == 1 || sz2.height == 4) in function 'cv::arithm_op'

Subtracting a number works,

>>> cv2.subtract(mat, 5)
array([[[103, 122, 116],
        [102, 122, 114],
        [106, 122, 114],
        ...,

as does subtracting the tuple using numpy (but that won't work in my case because numpy doesn't saturate the subtraction like OpenCV does).

>>> mat - (10, 20, 30)
array([[[ 98, 102,  86],
        [ 97, 102,  84],
        [101, 102,  84],
        ...,

The error message is also correct if I remove a channel from the tuple:

>>> cv2.subtract(mat, (10, 10))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\core\src\arithm.cpp:650: error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function 'cv::arithm_op'

Steps to reproduce

import cv2

mat = cv2.imread("C:\\[...].png")
print(cv2.subtract(mat, (10, 10, 10)))

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions