【发布时间】:2021-05-24 06:35:50
【问题描述】:
我对 Python 完全陌生。我拍了一张彩色照片。然后将其转换为灰度图像。到目前为止还不错。但是当我尝试将此灰度转换为二值图像时,我得到的是黄色和紫色的图像,而不是白色和黑色。
import matplotlib.pyplot as plt
import numpy as np
painting=plt.imread("ff.jpg")
print(painting.shape)
print("The image consists of %i pixels" % (painting.shape[0] * painting.shape[1]))
plt.imshow(painting);
#This displayed by color image perfectly
from skimage import data, io, color
painting_gray = color.rgb2gray(painting)
print(painting_gray)
io.imshow(painting_gray)
print("This is the Gray scale version of the original image")
#This displayed my gray scale image perfectly
#Now comes the code for binary image:
num = np.array(painting_gray)
print(num)
bin= num >0.5
bin.astype(np.int)
plt.imshow(bin)
#The display showed an image with yellow and purple color instead of white and black
我得到的图像图是:
请帮我获取黑白二值图像。
【问题讨论】:
-
这是因为 imshow 使用默认颜色图。试试 plt.imshow(bin, cmap='gray', vmin=0, vmax=255) 或 plt.gray()
-
这非常有效。谢谢。
-
@Stefan 也许您应该提交此评论作为答案,以便 OP 可以接受它?
-
好的,我可以这样做,但我认为类似的问题在其他地方已经有了答案。
标签: python numpy image-processing grayscale binary-image