image - Python OpenCV drawing errors after manipulating array with numpy -


i'm reading in image opencv, , trying in numpy (rotate 90deg). viewing result imshow matplotlib, seems working fine - image rotated. can't use drawing methods opencv on new image, however. in following code (i'm running in sagemath cloud worksheet):

%python import cv2 import matplotlib.pyplot plt import numpy np import os, sys image = np.array( cv2.imread('imagename.png') ) plt.imshow(image,cmap='gray') image = np.array(np.rot90(image,3) ) # put right side plt.imshow(image,cmap='gray') cv2.rectangle(image,(0,0),(100,100),(255,0,0),2) plt.imshow(image,cmap='gray') 

i following error on cv2.rectangle() command:

typeerror: layout of output array img incompatible cv::mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels) 

the error goes away if use np.array(np.rot90(image,4) ) instead (i.e. rotate 360). appears change in dimensions messing up. opencv store dimensions somewhere internally need update or something?

edit: adding image = image.copy() after rot90() solved problem. see rayryeng's answer below.

this apparently bug in python opencv wrapper. if @ question here: np.rot90() corrupts opencv image, apparently doing rotation doesn't result in original dimensions corrupts image , op in post experiences same error having. fwiw, experienced same bug.... no idea why.

a way around make copy of image after rotate, , show image. can't explain, seems work. also, make sure call plt.show() @ end of code show image:

import cv2 import matplotlib.pyplot plt import numpy np import os, sys image = np.array( cv2.imread('imagename.png') ) plt.imshow(image,cmap='gray') image = np.array(np.rot90(image,3) ) # put right side  image = image.copy() # change  plt.imshow(image,cmap='gray') cv2.rectangle(image,(0,0),(100,100),(255,0,0),2) plt.imshow(image,cmap='gray')  plt.show() # show image 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -