OpenCV中cv2.resize对于16位图像出现错误"(-215:Assertion failed) !dsize.empty()",但是对于8位图像却没有出错

4
我正在尝试使用cv2.resize函数调整图像的大小,但是我遇到了以下错误:

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'

我的图像是uint16数组:
img_ms.shape
(4, 57, 62)

img_pan.shape
(1, 1140, 1240)

我正在使用一种图像融合脚本中的样例函数,该函数为:

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

在8位图像上,我没有出现错误。16位图像会发生什么?

1个回答

3

您需要转置您的数据。通常,如果您使用类似于rasterio的工具,遥感图像的形状为(C,H,W) ,但是 cv2.resize函数期望的输入形状为(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)

请注意,您可能还需要将图像转换回通道优先格式。
OpenCV可以轻松地调整任何深度的图像大小 - 以下内容都应该有效:
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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接