Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange behaviour of cv2.add operation #25165

Open
4 tasks done
on2k23nm opened this issue Mar 6, 2024 · 6 comments
Open
4 tasks done

Strange behaviour of cv2.add operation #25165

on2k23nm opened this issue Mar 6, 2024 · 6 comments

Comments

@on2k23nm
Copy link

on2k23nm commented Mar 6, 2024

System Information

OpenCV python version: 4.9.0.80
Operating System / Platform: Ubuntu 22.04.4 LTS (WSL)
Python version: 3.10.12

Detailed description

I am expecting that cv2.add will truncate the results of addition between [0, 255], but that's not happening in cv2 4.9.0.80 as can be seen below :

(py2k23) onkar@DESKTOP-MT8AVE7:~/$ python
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> x = np.uint8([250])
>>> y = np.uint8([10])
>>> import cv2
>>> print(cv2.add(x, y))
[[260.]
 [  0.]
 [  0.]
 [  0.]]
>>> print(x+y)
[4]
>>> print(cv2.__version__)
4.9.0
>>> 

I tested the same behavior in other environment which is having cv2 3.4.18.65 and there it is working as expected.

(myPyTestEnv) onkar@DESKTOP-MT8AVE7:~$ python
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> import cv2
>>> x = np.uint8([250])
>>> y = np.uint8([10])
>>> z = cv2.add(x, y)
>>> print(z)
[[255]]
>>> print(cv2.__version__)
3.4.18
>>>

Steps to reproduce

To repro the problem, just follow the OpenCV tutorials here with cv2 version 4.9.0.80

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)
@on2k23nm on2k23nm added the bug label Mar 6, 2024
@Garvanand
Copy link

Check if there are any known compatibility issues with OpenCV version 4.9.0.80 on Ubuntu 22.04.4 LTS (WSL). The issue may be platform-specific.(Try this first)

If the issue is critical for your work, you may consider downgrading OpenCV to version 3.4.18.65 as a temporary solution until the problem is resolved in a future release.

If the issue is Open, kindly assign the issue to me so that I can work upon it and dig deeper to find the exact issue and the root cause.

@on2k23nm
Copy link
Author

on2k23nm commented Mar 6, 2024

Check if there are any known compatibility issues with OpenCV version 4.9.0.80 on Ubuntu 22.04.4 LTS (WSL). The issue may be platform-specific.(Try this first)

If the issue is critical for your work, you may consider downgrading OpenCV to version 3.4.18.65 as a temporary solution until the problem is resolved in a future release.

If the issue is Open, kindly assign the issue to me so that I can work upon it and dig deeper to find the exact issue and the root cause.

  1. I have already moved on with 3.4.18.65 cv2 release
  2. What compability issues are you referring to. I'm running in venv and not directly in WSL. You can see it in the output in the description. So I don't think this is a compatibility issue.

Anyways, someone with more experience needs to look into this and ascertain if this is really a issue or not. Currently I don't have time to look into this otherwise I myself would have digged in deeper into this.

@LaurentBerger
Copy link
Contributor

LaurentBerger commented Mar 6, 2024

As x and y are not 2 or 3 dimensional array opencv python binding make casting

y = 10*np.ones((1,1,1), np.uint8)
x = 250*np.ones((1,1,1), np.uint8)
cv.add(x, y)

result array([[255]], dtype=uint8)

result for cv.add(10,250)
array([[260.],
[260.],
[260.],
[260.]])

and

x = 250*np.ones((2,1), np.uint8)
y = 10*np.ones((2,1), np.uint8)
cv.add(x,y)
array([[255],
       [255]], dtype=uint8)

@Kumataro
Copy link
Contributor

Kumataro commented Mar 6, 2024

I'm sorry, this bug is occured by #24074 patch.

Following code is temporary disable this patch for add function.

kmtr@kmtr-None:~/work/opencv$ git diff
diff --git a/modules/python/src2/hdr_parser.py b/modules/python/src2/hdr_parser.py
index d4a5b26a9f..d52f5fdfbf 100755
--- a/modules/python/src2/hdr_parser.py
+++ b/modules/python/src2/hdr_parser.py
@@ -539,12 +539,11 @@ class CppHeaderParser(object):
         funcname = self.get_dotted_name(funcname)

         # see https://github.com/opencv/opencv/issues/24057
-        is_arithm_op_func = funcname in {"cv.add",
+        is_arithm_op_func = funcname in {"Xcv.add",
                                          "cv.subtract",
                                          "cv.absdiff",
                                          "cv.multiply",
                                          "cv.divide"}
-
         if not self.wrap_mode:
             decl = self.parse_func_decl_no_wrap(decl_str, static_method, docstring)
             decl[0] = funcname
kmtr@kmtr-None:~/work/build4-main_python3.12.0$ python issue25165.py
[[255]]
kmtr@kmtr-None:~/work/build4-main_python3.12.0$ cd ../opencv

I think it would be difficult to fix the previous problem ( #24057 ) and this problem at the same time.
Umm...

@crackwitz
Copy link
Contributor

crackwitz commented Mar 6, 2024

the Stack Overflow questions that those people came from:

I'd say it's not a bug but an unexpected feature that trips newbies up because they aren't aware of how numpy array shapes are translated into cv:: data types.

@Kumataro
Copy link
Contributor

I challenged to make patch to avoid convertion single long/double argument to cv::Mat for add/subtract/...

Currently python binding doesn't support changing argument-types.
add() function can be used for src1/src2 with only Mat , not Scalar, int and double.
It cause some strange behavious with arithm functions for emulation other types.

This patch is able to use manual some implementation functions instead of auto-generation functions.
So we can modify binding functions as we want to.

However, this brute-force fix may not be suitable to maintainance for us.
If there is a better way to fix this, I would like to use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants