我正在将 base64 图像发送到 flask
服务器。然后使用文件处理将 base64 图像解码并保存在我的本地机器上。保存文件后,我使用 opencv 2.0
函数 imread
读取图像并使用 imshow
显示它。但它显示全黑图像(numpy 数组全为零)。使用图像查看器可以正确显示图像。
可能的解决方案是什么?
我尝试用 matplotlib
替换 OpenCV,但它也不起作用。甚至添加了 imread_unchanged 以便不裁剪返回的图像。
image_64_encoded="contains base64 encoded image"
fh = open("imageToSave.png", "w")
fh.write(image_64_encoded.decode('base64'))
fh.close()
script_dir = os.path.dirname(__file__)
rel_path = "../shape-detection/imageToSave.png"
abs_file_path = os.path.join(script_dir, rel_path)
print("\n",abs_file_path,"\n\n")
image = cv2.imread(abs_file_path)
print(image);
cv2.imshow('image',image)
预期结果必须与图像查看器显示的结果相同。但它显示的是纯黑色图像。
来自图像查看器的实际图像
cv2.imshow()显示的图像
最佳答案
信息全部在 alpha channel 中而不是 RGB channel 中。
你需要使用:
image = cv2.imread(path,cv2.IMREAD_UNCHANGED)
并查看第 4 个 channel (alpha):
usefulchannel = image[:,:,3]
关于python - 本地机器上的图像在图像查看器上正确显示,但在使用 cv2.imread() 时无法正确读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56869984/