lhao0301
lhao0301

Reputation: 2011

Can't draw box when call cv2.rectangle

Can't draw box on image after flipping it in opencv.

>>im = cv2.imread('demo.jpg')
>>im = im[:,::-1]
>>drawed_im = cv2.rectangle(im, (10,10), (50,50), (255, 0, 0), thickness=2)
>>TypeError: Layout of the output array img is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels)

Upvotes: 1

Views: 2123

Answers (1)

Jello
Jello

Reputation: 420

This an odd one that I haven't been able to determine the cause of. I've seen it before with other drawing functions and the solution I have used is to re-cast the image as a numpy array.

import cv2
import numpy as np

im = cv2.imread('demo.jpg')
im = np.array(im[:,::-1]) # cast as array
drawed_im = cv2.rectangle(im, (10,10), (50,50), (255, 0, 0), thickness=2)

Upvotes: 2

Related Questions