python - 保存和读取二进制图像

标签 python opencv

你好我想在阈值后保存图像:

binary=cv2.threshold(fark, 30, 255, cv2.THRESH_BINARY)[1]
binary.astype(np.uint8)
print binary.shape
cv2.imwrite("path"+str(counter)+".png",binary)

binary.shape 输出是:(320,240) ---> 这就是我想要的

但是当我阅读图像时:

image=cv2.imread(path)
print image.shape

输出是 (320,240,3) ,当我检查数组时,它的值类似于 254,253

我能为这种情况做什么?保存二进制图像的最佳文件格式是什么?

最佳答案

threshed 已经是np.uint8,不需要改变。

th, threshed = cv2.threshold(fark, 30, 255, cv2.THRESH_BINARY)
print(threshed.dtype, threshed.shape)

但是当使用cv2.imread时,默认转换为BGR channel 。要保持原始形状和 channel (对于 grayscalepng with alpha channel):

img = cv2.imread("test.png", cv2.IMREAD_UNCHANGED)

或者只是为了灰色:

img = cv2.imread("test.png", cv2.IMREAD_GRAYSCALE)

关于python - 保存和读取二进制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48023127/

相关文章:

image - 在 C++ 中使用 OpenCV 删除背景

python - 使用 Python 解析 JSON 文件中的字典

javascript - 1 << 32 的不同行为比较 javascript 和 python

python - 如何从 OpenCV fitLine 计算残差?

c++ - Opencv 3 支持 vector 机训练

python - 在openCV中删除没有运动的帧

python - python:pip install opencv-python给我错误

python - 检查 Pair Series 值以查看它们是否在列表中

python - 通过 sh 模块运行 python 脚本后访问 stderr

python-3.x - 根据颜色分割图像