我正在尝试从分段神经网络中保存预测的蒙版。当我尝试保存结果时,保存的图像为黑色。但是当我使用cv2.imshow()
函数时,图像很好。
我的代码:
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import numpy as np
def load_image(img_path, show=False):
img = image.load_img(img_path, target_size=(256, 256))
img_tensor = image.img_to_array(img)
img_tensor = np.expand_dims(img_tensor, axis=0)
img_tensor /= 255.
return img_tensor
model = load_model('model.h5')
input_image = load_image('input.jpg')
mask = model.predict(input_image)
cv2.imshow("output", mask[0]) #cv2.imshow() is working alright, so mask is correctly printed
cv2.imwrite("output.jpg", mask[0]) #saved image is black
cv2.waitKey(0)
cv2.destroyAllWindows()
我该怎么办?