簡體   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