Python 保存灰度.png图像时cv2.imwrite()不起作用
我使用Python 保存灰度.png图像时cv2.imwrite()不起作用,python,numpy,opencv,Python,Numpy,Opencv,我使用tf.keras.preprocessing.image.ImageDataGenerator生成了图像,并绘制了使用matplotlib生成的图像,结果如下: 这实际上是我希望保存在文件系统中的内容,但出于某种原因,每当我尝试使用cv2.imwrite()函数写入图像时,我都会得到以下结果: 在某些情况下,保存的图像完全为空白(所有像素均为白色)。代码如下: cnt = 0 for image, label in zip(augmented_images, labels): cn
tf.keras.preprocessing.image.ImageDataGenerator
生成了图像,并绘制了使用matplotlib
生成的图像,结果如下:
这实际上是我希望保存在文件系统中的内容,但出于某种原因,每当我尝试使用cv2.imwrite()
函数写入图像时,我都会得到以下结果:
在某些情况下,保存的图像完全为空白(所有像素均为白色)。代码如下:
cnt = 0
for image, label in zip(augmented_images, labels):
cnt += 1
path = os.path.join(augmented_raw_images_train_dir,str(int(label[0])),"aug_"+str(cnt)+".png")
cv2.imwrite(path, image[0])
请记住,图像是一批大小为1的图像,因此
图像[0]
实际上是一个大小为(150,150,1)
的n维numpy数组,很可能数据中的像素值在[0.0,1.0]范围内。在通过opencv保存它们之前,应该将它们转换为[0255]范围
cnt = 0
for image, label in zip(augmented_images, labels):
cnt += 1
path = os.path.join(augmented_raw_images_train_dir,str(int(label[0])),"aug_"+str(cnt)+".png")
image_int = np.array(255*image[0], np.uint8)
cv2.imwrite(path, image_int)
如果值在[-1,1]范围内,您可以相应地进行调整