[英]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:我正在尝试使用
cv2.resize
function 调整图像大小,但出现以下错误:
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'
错误: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'
My images are uint16 arrays:我的图像是 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:我在图像全色锐化脚本中使用的示例 function 是:
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.对于 8 位图像,我没有得到错误。 What happens with 16-bit images?
16 位图像会怎样?
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)
.通常 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)
Note you may also need to transpose back to channel-first.请注意,您可能还需要移调回通道优先。
OpenCV will happily resize images of any depth - the following should all work: 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.