python - 本地机器上的图像在图像查看器上正确显示,但在使用 cv2.imread() 时无法正确读取?

标签 python image opencv base64

我正在将 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)

预期结果必须与图像查看器显示的结果相同。但它显示的是纯黑色图像。

来自图像查看器的实际图像

Actual image from image viewer

cv2.imshow()显示的图像

image displayed by 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/

相关文章:

java - 在 Google 的 Protocol Buffers 中,什么是适合异常的协议(protocol)文件/模型?

ios - 从 NSData 读取 XMP 元数据

c++ - 将 cv::Mat 转换为 std::vector<double> 并发症

python - 如何在Python中使用OpenCV或PIL在不更改图像分辨率的情况下放大或缩小形状

python - 如何过滤 pandas DataFrame 并保留特定元素?

python PIL : IOError: cannot identify image file

python - 如何高效地在一个数字中插入大量数字?

Javascript 阻止 css?

image - 静态图像的 OpenCV haar 训练

opencv - 带有Visual Studio的openCV,链接错误