我想在 TIFF 文件中操作RGB波段并在 matplotlib 上输出灰度图。到目前为止,我有这段代码,但我无法在灰度上得到它:
import scipy as N
import gdal
import sys
import matplotlib.pyplot as pyplot
tif = gdal.Open('filename.tif')
band1 = tif.GetRasterBand(1)
band2 = tif.GetRasterBand(2)
band3 = tif.GetRasterBand(3)
red = band1.ReadAsArray()
green = band2.ReadAsArray()
blue = band3.ReadAsArray()
gray = (0.299*red + 0.587*green + 0.114*blue)
pyplot.figure()
pyplot.imshow(gray)
pylab.show()
这些是数组:
[[255 255 255 ..., 237 237 251]
[255 255 255 ..., 237 237 251]
[255 255 255 ..., 237 237 251]
...,
[237 237 237 ..., 237 237 251]
[237 237 237 ..., 237 237 251]
[242 242 242 ..., 242 242 252]]
[[255 255 255 ..., 239 239 251]
[255 255 255 ..., 239 239 251]
[255 255 255 ..., 239 239 251]
...,
[239 239 239 ..., 239 239 251]
[239 239 239 ..., 239 239 251]
[243 243 243 ..., 243 243 252]]
[[255 255 255 ..., 234 234 250]
[255 255 255 ..., 234 234 250]
[255 255 255 ..., 234 234 250]
...,
[234 234 234 ..., 234 234 250]
[234 234 234 ..., 234 234 250]
[239 239 239 ..., 239 239 251]]
知道如何解决这个问题吗?