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

Document OpenCV 4.9 Python changes behavior with readonly numpy arrays #24522

Closed
4 tasks done
vrabaud opened this issue Nov 10, 2023 · 3 comments · Fixed by #24764
Closed
4 tasks done

Document OpenCV 4.9 Python changes behavior with readonly numpy arrays #24522

vrabaud opened this issue Nov 10, 2023 · 3 comments · Fixed by #24764
Milestone

Comments

@vrabaud
Copy link
Contributor

vrabaud commented Nov 10, 2023

System Information

OpenCV 4.x HEAD, Linux, Python 3.11

Detailed description

When some functions (e.g. cv::rectangle, cv::circle) are called on a readonly argument in OpenCV 4.8.0, they do modify it. It is unclear with the doc if the input should also be modified or not as the function returns an image, cf https://docs.opencv.org/4.8.0/d6/d6e/group__imgproc__draw.html#ga07d2f74cadcf8e305e810ce8eed13bc9.

Still, In OpenCV HEAD in 4.x, we get the following error:

error: OpenCV(4.8.0-dev) :-1: error: (-5:Bad argument) in function 'rectangle'
> Overload resolution failed:
>  - img marked as output argument, but provided NumPy array marked as readonly
>  - Expected Ptr<cv::UMat> for argument 'img'
>  - img marked as output argument, but provided NumPy array marked as readonly
>  - Expected Ptr<cv::UMat> for argument 'img'

I guess the correct behavior is to always make a copy of the image before calling cv::rectangle and return it?

Steps to reproduce

import cv2
from PIL import Image
import numpy as np
img=np.asarray(Image.open("/tmp/img.png"))
print(img.flags)
cv2.rectangle(img,0,1,1,1)

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)
@asmorkalov
Copy link
Contributor

Related patch: #24026
Original issue: opencv/opencv-python#859

In case if the numpy array was created by external package write access to RO array may case segmentation fault. Please see details in related OpenCV-Python issue.

@vrabaud
Copy link
Contributor Author

vrabaud commented Nov 14, 2023

Thx for the pointers, I missed them. I am totally ok with the new behavior, it is a fix.

I had to go through https://docs.opencv.org/4.8.0/da/d49/tutorial_py_bindings_basics.html to get that Header parser can understand the input and output arguments from keywords like InputArray, OutputArray etc.. Maybe a little bit of text could be added for users (not sure if that is the right page, as it is a bit technical). Something like: The InputArray, OutputArray, InputOutputArray semantics are kept in Python: anything that is modified in C++ will be modified in Python. In addition, Python bindings also output anything that is an OutputArray, InputOutputArray or marked as CV_OUT.

@asmorkalov asmorkalov added category: documentation Documentation fix or update and removed bug labels Nov 14, 2023
@asmorkalov asmorkalov changed the title OpenCV 4.9 Python changes behavior with readonly numpy arrays Document OpenCV 4.9 Python changes behavior with readonly numpy arrays Nov 14, 2023
@asmorkalov asmorkalov added this to the 4.9.0 milestone Nov 14, 2023
@bitsdaddy
Copy link

.astype(np.uint8)

using this with the image solved the problem for me

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.

3 participants