Skip to content

np.rot90() and cv2.line() doesn't seem to work together #1057

Open
@FrickTobias

Description

@FrickTobias

Expected behaviour

cv2.line() should work on what np.rot90() returns (np arrays with np.uint8 dtype).

Actual behaviour

After rotating images with np.rot90() cv2.line() raises an error, claiming the array is not compatible.

Steps to reproduce

  • example code
import numpy as np
import cv2

img = np.zeros((10, 10), dtype=np.uint8)

img_rot = np.rot90(img)

img_line = cv2.line(img, (0, 0), (9, 9), 255, 2)

cv2.line(img_rot, (0, 0), (9, 9), 255, 2)
  • error
Traceback (most recent call last):
  File "cv2bug.py", line 10, in <module>
    cv2.line(img_rot, (0, 0), (9, 9), 255, 2)
cv2.error: OpenCV(4.10.0) :-1: error: (-5:Bad argument) in function 'line'
> Overload resolution failed:
>  - Layout of the output array img is incompatible with cv::Mat
>  - Expected Ptr<cv::UMat> for argument 'img'```
  • operating system
sw_vers
ProductName:		macOS
ProductVersion:		15.1
BuildVersion:		24B83
  • architecture (e.g. x86)
uname -m
x86_64
pip show opencv-python numpy

Name: opencv-python
Version: 4.10.0.84
Summary: Wrapper package for OpenCV python bindings.
Home-page: https://github.com/opencv/opencv-python
<...>

Name: numpy
Version: 2.0.2
Summary: Fundamental package for array computing in Python
Home-page: https://numpy.org
<...>
  • other info

I tested some other cv2 drawing functions and get the same result (for example cv2.circle())

I also tested numpy 1.26.1 and got the same result.

I have double checked cv2.rot90() doesn't change the dtype of the array

print(f"{img_rot.dtype = }") # output: img_rot.dtype = dtype('uint8')

Arguably this could be an issue with np.rot90(). However, since other cv2 functions works like normal my initial guess was to start here. For instance, image can be displayed, saved and loaded.

cv2.imshow("img_rot", img_rot)
cv2.imwrite("imgrot.png", img_rot)
img_rot = cv2.imread("imgrot.png")

Lastly, I've noted that if the image is saved and loaded, it works as expected.

import numpy as np
import cv2

img = np.zeros((10, 10), dtype=np.uint8)

img_rot = np.rot90(img)

img_line = cv2.line(img, (0, 0), (9, 9), 255, 2)

cv2.imwrite("imgrot.png", img_rot)
img_rot = cv2.imread("imgrot.png")

cv2.line(img_rot, (0, 0), (9, 9), 255, 2) # No error
Issue submission checklist
  • This is not a generic OpenCV usage question (looking for help for coding, other usage questions, homework etc.)
  • I have read the README of this repository and understand that this repository provides only an automated build toolchain for OpenCV Python packages (there is no actual OpenCV code here)
  • The issue is related to the build scripts in this repository, to the pre-built binaries or is a feature request (such as "please enable this additional dependency")
  • I'm using the latest version of opencv-python

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions