I noticed, transposing an image seemingly corrupts the image layout
. While functions such as cv2.imshow()
seem to work normally, it causes other functions such as cv2.drawContours
to fail with an error like:
error: OpenCV(4.9.0) :-1: error: (-5:Bad argument) in function 'drawContours'
> Overload resolution failed:
> - Layout of the output array image is incompatible with cv::Mat
> - Expected Ptr<cv::UMat> for argument 'image'
In order to fix the issue, I noticed doing a simple resizing fixes the issue:
h,w,c = img.shape
np.transpose(img, axes=(1,0,2)).reshape((w,h,c))
# as you can see, the image size stays the same!
img = cv2.resize(img, dsize=None, fx=1, fy=1)
But I’m not sure why my accidental work-around! works or how to prevent this in first place!
Can someone please explain whats going wrong here and how it can be avoided for fixed properly?