JoseA4B8
JoseA4B8

Reputation: 43

cv2.resize Error "(-215:Assertion failed) !dsize.empty()" for 16-bit images but not for 8-bit

I am trying to resize an image with the cv2.resize function and I get the following error:

error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-oduouqig\opencv\modules\imgproc\src\resize.cpp:3688: error: (-215:Assertion failed) !dsize.empty() in function 'cv::hal::resize'

My images are uint16 arrays:

img_ms.shape
(4, 57, 62)

img_pan.shape
(1, 1140, 1240)

The sample function I am using inside an image pansharpening script is:

downsampled_img_pan = cv2.resize(img_pan, (img_ms.shape[2], img_ms.shape[1]), 
                                 interpolation = cv2.INTER_AREA)[:, :, np.newaxis]

With an 8-bit image I don't get the error. What happens with 16-bit images?

Upvotes: 4

Views: 7730

Answers (1)

Josh
Josh

Reputation: 2869

You need to transpose your data. Typically EO images are (C,H,W) (if you're using something like rasterio) whereas cv2.resize expects (H,W,C).

downsampled_img_pan = cv2.resize(img_pan.transpose(1,2,0),
                                 (img_ms.shape[2], img_ms.shape[1]),
                                 interpolation = cv2.INTER_AREA).transpose(2,0,1)

Note you may also need to transpose back to channel-first.

OpenCV will happily resize images of any depth - the following should all work:

im = (np.random.random((640,640,3))*65535).astype(np.float32)
cv2.resize(im, None, fx=0.5, fy=0.5)

im = (np.random.random((640,640,3))*65535).astype(np.uint16)
cv2.resize(im, None, fx=0.5, fy=0.5)

im = (np.random.random((640,640,3))*255).astype(np.uint8)
cv2.resize(im, None, fx=0.5, fy=0.5)

Upvotes: 5

Related Questions