Skip to content

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

Closed
@vrabaud

Description

@vrabaud

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions