错误: OpenCV(4.5.1) ../modules/highgui/src/window.cpp:376:错误:(-215:断言失败)函数'imshow'中的size.width>0



我想在加载在ImageNet上预训练的VGG-16神经网络之前,将栅格图像转换为具有(224,224)维数的NumPy数组。我的代码引发error: OpenCV(4.5.1) ../modules/highgui/src/window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'错误,即使图像可以成功地从路径读取。

from osgeo import gdal
import numpy as np
import sys
from keras.preprocessing import image as image_utils
from keras.applications.imagenet_utils import decode_predictions
from keras.applications.imagenet_utils import preprocess_input
from keras.applications.vgg16 import VGG16
from keras.applications.vgg19 import VGG19
from keras.applications.resnet import ResNet50
from keras.applications.inception_v3 import InceptionV3
from keras.applications.densenet import DenseNet121
import cv2
reg001_path = "../sample_data/mosaic/sample IDs A015-C-202 (Reg1) A001-C-002  (Reg2)_reg001.tif"
reg001 = gdal.Open(reg001_path)
channel_reg001 = np.array(reg001.GetRasterBand(1).ReadAsArray())
# Resize NumPy array to 224x224, the required input dimensions for the neural network
image = channel_reg001.resize((224, 224, 3))
# Expand the dimensions so we can pass it through the network
image = np.expand_dims(channel_reg001, axis=0)
# preprocess the image by subtracting the mean RGB pixel intensity from the ImageNet dataset
image = preprocess_input(image)
# load the VGG16 network pre-trained on the ImageNet dataset
print("[INFO] loading network...")
model = VGG16(weights="imagenet")
# classify the image
print("[INFO] classifying image...")
preds = model.predict(image)
P = decode_predictions(preds)
# loop over the predictions and display the rank-5 predictions +
# probabilities to our terminal
for (i, (imagenetID, label, prob)) in enumerate(P[0]):
print("{}. {}: {:.2f}%".format(i + 1, label, prob * 100))
# load the image via OpenCV, draw the top prediction on the image,
# and display the image to our screen
orig = cv2.imread(r"reg001_path")
(imagenetID, label, prob) = P[0][0]
cv2.putText(orig, "Label: {}, {:.2f}%".format(label, prob * 100), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
cv2.imshow("Classification", orig)
cv2.waitKey(0)
cv2.destroyAllWindows()

回溯:

> --------------------------------------------------------------------------- error                                     Traceback (most recent call
> last) /tmp/ipykernel_7165/3707729371.py in <module>
>      15 (imagenetID, label, prob) = P[0][0]
>      16 cv2.putText(orig, "Label: {}, {:.2f}%".format(label, prob * 100), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
> ---> 17 cv2.imshow("Classification", orig)
>      18 cv2.waitKey(0)
>      19 cv2.destroyAllWindows()
> 
> error: OpenCV(4.5.1) ../modules/highgui/src/window.cpp:376: error:
> (-215:Assertion failed) size.width>0 && size.height>0 in function
> 'imshow'

可以成功读取图像:

img = cv2.imread("../sample_data/mosaic/sample IDs A015-C-202 (Reg1) A001-C-002  (Reg2)_reg001.tif" ,0)
print(img)
[[  0   1  24 ...   2  40  25]
[ 71   2  22 ...   3  23  58]
[ 82   3  38 ... 147 116   3]
...
[  0   0   0 ...  14   1   0]
[183  31   0 ...  50  34   0]
[119   3   8 ...   1  11   3]]

可以成功读取图像:

img = cv2.imread("../sample_data/mosaic/sample IDs A015-C-202 (Reg1) A001-C-002 (Reg2)_reg001.tif" ,0)

reg001_path = "../sample_data/mosaic/sample IDs A015-C-202 (Reg1) A001-C-002 (Reg2)_reg001.tif"

如果是,您可能希望像这样加载orig图像:

orig = cv2.imread(reg001_path) # loading the *value* of 'reg001_path'

不一样:

orig = cv2.imread(r"reg001_path") # loading the *name* of 'reg001_path'
然而,请养成习惯,总是检查imread()的结果在继续计算之前,因为它将在失败时静默返回None,而不是抛出异常

最后,我认为putText()默默地接受无效图像是一个opencv错误!

相关内容

  • 没有找到相关文章

最新更新