你好我想在阈值后保存图像:
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 (对于 grayscale
或 png 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/