您好,我想在阈值后保存图像:
binary=cv2.threshold(fark, 30, 255, cv2.THRESH_BINARY)[1]
binary.astype(np.uint8)
print binary.shape
cv2.imwrite("path"+str(counter)+".png",binary)
Run Code Online (Sandbox Code Playgroud)
binary.shape 输出是:(320,240) ---> 这就是我想要的
但是当我阅读图像时:
image=cv2.imread(path)
print image.shape
Run Code Online (Sandbox Code Playgroud)
输出是 (320,240,3) ,当我查看数组时,它的值类似于 254,253
我可以为这种情况做些什么,保存二进制图像的最佳文件格式是什么?
在threshed已经np.uint8,不需要改变。
th, threshed = cv2.threshold(fark, 30, 255, cv2.THRESH_BINARY)
print(threshed.dtype, threshed.shape)
Run Code Online (Sandbox Code Playgroud)
但使用时cv2.imread,默认转换为BGR通道。要保持原始形状和通道(对于grayscale或png with alpha channel):
img = cv2.imread("test.png", cv2.IMREAD_UNCHANGED)
Run Code Online (Sandbox Code Playgroud)
或者只是灰色:
img = cv2.imread("test.png", cv2.IMREAD_GRAYSCALE)
Run Code Online (Sandbox Code Playgroud)