保存和阅读二进制图像

时间:2017-12-29 13:08:48

标签: 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

我可以为此版本做些什么以及保存二进制图像的最佳文件格式是什么?

1 个答案:

答案 0 :(得分:3)

threshed已经np.uint8,无需进行任何更改。

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

但是在使用cv2.imread时,默认会转换为BGR个频道。保持原始形状和频道(grayscalepng with alpha channel):

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

或只是灰色:

img = cv2.imread("test.png", cv2.IMREAD_GRAYSCALE)
相关问题