rgb2gray 不工作,创建彩虹图像
rgb2gray not working, creating rainbow image
我正在尝试对一系列照片进行数据分析,当所有照片都从 RGB "changed" 到灰度时,它们的结果如下:
The standard Astronaut image as my system says it's grayscale
这是我使用的代码:
import matplotlib.pyplot as plt
from skimage.color import rgb2gray
from skimage import data
img = data.astronaut()
img_gray = rgb2gray(img)
plt.imshow(img_gray)
plt.show()
我也尝试过将 hsv 转换为 rgb,然后再转换为灰度,但它仍然会生成类似的非灰度图像。
问题是 matplotlib
使用默认颜色图显示 2D 图像。将您的代码更改为
import matplotlib.pyplot as plt
from skimage.color import rgb2gray
from skimage import data
img = data.astronaut()
img_gray = rgb2gray(img)
plt.imshow(img_gray, cmap='gray')
plt.show()
您也可以使用
from skimage import io
io.imshow(img_gray)
这将自动处理灰度图像
我正在尝试对一系列照片进行数据分析,当所有照片都从 RGB "changed" 到灰度时,它们的结果如下:
The standard Astronaut image as my system says it's grayscale
这是我使用的代码:
import matplotlib.pyplot as plt
from skimage.color import rgb2gray
from skimage import data
img = data.astronaut()
img_gray = rgb2gray(img)
plt.imshow(img_gray)
plt.show()
我也尝试过将 hsv 转换为 rgb,然后再转换为灰度,但它仍然会生成类似的非灰度图像。
问题是 matplotlib
使用默认颜色图显示 2D 图像。将您的代码更改为
import matplotlib.pyplot as plt
from skimage.color import rgb2gray
from skimage import data
img = data.astronaut()
img_gray = rgb2gray(img)
plt.imshow(img_gray, cmap='gray')
plt.show()
您也可以使用
from skimage import io
io.imshow(img_gray)
这将自动处理灰度图像