繁体   English   中英

cv2.resize Error "(-215:Assertion failed).dsize.empty()" 对于 16 位图像但不是 8 位图像

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

我正在尝试使用cv2.resize function 调整图像大小,但出现以下错误:

错误:OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-oduouqig\opencv\modules\imgproc\src\resize.cpp:3688: 错误: (-215 :断言失败).dsize:empty() 在 function 'cv::hal::resize'

我的图像是 uint16 arrays:

img_ms.shape
(4, 57, 62)

img_pan.shape
(1, 1140, 1240)

我在图像全色锐化脚本中使用的示例 function 是:

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

对于 8 位图像,我没有得到错误。 16 位图像会怎样?

您需要转置数据。 通常 EO 图像是(C,H,W) (如果您使用的是rasterio类的东西),而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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM