Np.rot90 () distorts opencv image

When I try to rotate the landscape image into the portrait after applying the rotation, I cannot draw the image.

img1 = cv2.imread('a.jpg') cv2.circle(img1, tuple([10,10]),radius = 3, color = (255,0,0)) 

works great.

Then I try:

 img2 = np.rot90(img1,3) cv2.circle(img2, tuple([10,10]),radius = 3, color = (255,0,0)) 

and I get the error:

 TypeError: Layout of the output array img is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels) 

Looking at type(img2) and img2.dtype , it seems identical to img1. the dimensions also seem beautiful (the first two dimensions are inverted, the third remains “3”)

By the way: it seems to work. (Why?):

 img2 = np.rot90(img1,3) img3 = img2.copy() cv2.circle(img3, tuple([10,10]),radius = 3, color = (255,0,0)) 
+6
source share
1 answer

I had the same problem, and I did not get to it. The workaround I used was to use image rotation / switching using OpenCV instead, for example:

 # flip image vertically img = cv2.flip(img, 0) # flip image horizontally img = cv2.flip(img, 1) # transpose image img = cv2.transpose(img) 

Note that rotation is equivalent to performing transpose and flip.

+2
source

All Articles