简体   繁体   中英

error: (-215:Assertion failed) _step >= minstep in function 'cv::Mat::Mat'

when i use opencv in python,look like this:

LST_Day_1km = j.select('LST_Day_1km')[:]
if np.max(LST_Day_1km) != np.min(LST_Day_1km):
    d=np.array([LST_Day_1km,LST_Day_1km,LST_Day_1km])
    cv2.imwrite(savePath + i[:-4] + '.LST_Day_1km.tif', d)

LST_Day_1km has many zero,and its shape is (1200,1200) what can i do????!!! it is error: imwrite_('E:/pyCharm/ESRGAN/hdf_h5_datasPro/jpgGray/MOD11A1.A2000056.h23v04.061.2020043120949.LST_Day_1km.tif'): can't write data: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-kh7iq4w7\opencv\modules\core\src\matrix.cpp:466: error: (-215:Assertion failed) _step >= minstep in function 'cv::Mat::Mat'

Instead of d = np.array([LST_Day_1km, LST_Day_1km, LST_Day_1km]) , use d = np.array([LST_Day_1km, LST_Day_1km, LST_Day_1km]) .

The following code reproduces the error:

import numpy as np
import cv2

LST_Day_1km = np.zeros((1200, 1200), np.uint8)  # 1200x1200 zeros (used for testing).
d = np.array([LST_Day_1km, LST_Day_1km, LST_Day_1km])
print(d.shape)  # d.shape = (3, 1200, 1200)
cv2.imwrite('LST_Day_1km.tif', d)

d.shape is (3, 1200, 1200)
The shape does not apply a valid OpenCV image.

A shape of a valid OpenCV image in BGR format is (1200, 1200, 3) .
This is 1200 rows by 1200 columns by 3 color channels ( the 3 comes last ).


The following code works:

LST_Day_1km = np.zeros((1200, 1200), np.uint8)
d = np.dstack((LST_Day_1km, LST_Day_1km, LST_Day_1km))
print(d.shape)  # d.shape = (1200, 1200, 3)
cv2.imwrite('LST_Day_1km.tif', d)

You may also use OpenCV for converting LST_Day_1km from Grayscale to BGR:

d = cv2.cvtColor(LST_Day_1km, cv2.COLOR_GRAY2BGR)  # Same result as `np.dstack`.

You may also save LST_Day_1km as Grayscale.
Here is your modified code - saving LST_Day_1km as Grayscale:

LST_Day_1km = j.select('LST_Day_1km')[:]
if np.max(LST_Day_1km) != np.min(LST_Day_1km):
    cv2.imwrite(savePath + i[:-4] + '.LST_Day_1km.tif', LST_Day_1km)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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