renatoGarcia / opencv-swig

SWIG interface files for OpenCV types.
Other
93 stars 23 forks source link

Weird behavior for Mat.from_array() #20

Open huckw opened 4 years ago

huckw commented 4 years ago

Attached is an image that is acting very strange...

I=cv2.imread("path/to/hbl.png")[:,:,0]
I1 = np.array(mylib.Mat.from_array(I))
I2 = np.array(mylib.Mat.from_array(I.astype(np.uint8)))

I1 is all messed up but I2 works as expected. I's dtype is already np.uint8 so I don't know what the magic is with the asytpe() converting it to the same datatype.

Any idea what I might be doing incorrectly?

hbl

renatoGarcia commented 4 years ago

Good catch! Thanks!

When you do A = cv2.imread("path/to/hbl.png") A will be a (200, 200, 3) image. when you do I=cv2.imread("path/to/hbl.png")[:,:,0] I will be a view of shape (200, 200) over the same underlaying memory as A. You can see that doing A.strides and I.strides, I1.strides, I2.strides.

What I3 = I.astype(np.uint8) is doing is creating a new (200, 200) buffer and copying the I data. Hence now the I3 memory is continuous and has the same strides as I2.

The bug lies at the function from_array doing the assumption that the array memory is continous.