【问题标题】:How to plot geolocated RGB data faster using Python basemap如何使用 Python 底图更快地绘制地理定位的 RGB 数据
【发布时间】:2017-05-14 08:20:16
【问题描述】:

我在使用带有纬度和经度数据的 Python 底图模块绘制 RGB 图像时遇到问题。现在,我可以制作我想要的图了,但问题是它有多慢,因为它能够比 RGB 数据更快地绘制单通道数据,而且一般来说,自己绘制 RGB 图像也是快速地。由于我有纬度/经度数据,这就是事情变得复杂的地方。我已经检查了这个问题的解决方案:

How to plot an irregular spaced RGB image using python and basemap?

这就是我现在所处的位置。它基本上归结为以下问题。在底图中使用pcolormesh 方法时,要绘制 RGB 数据,您必须定义一个 colorTuple 参数,该参数将逐点映射 RGB 数据。由于数组大小约为 2000x1000,因此这需要一段时间。我正在谈论的 sn-p 如下所示(完整的工作代码进一步向下):

if one_channel:
    m.pcolormesh(lons, lats, img[:,:,0], latlon=True)
else:
    # This is the part that is slow, but I don't know how to
    # accurately plot the data otherwise.

    mesh_rgb = img[:, :-1, :]
    colorTuple = mesh_rgb.reshape((mesh_rgb.shape[0] * mesh_rgb.shape[1]), 3)

    # What you put in for the image doesn't matter because of the color mapping
    m.pcolormesh(lons, lats, img[:,:,0], latlon=True,color=colorTuple)

当只绘制一个通道时,它可以在大约 10 秒左右的时间内完成地图。绘制 RGB 数据时,可能需要 3-4 分钟。鉴于只有 3 倍的数据量,我觉得一定有更好的方法,尤其是当您制作矩形图像时,绘制 RGB 数据可以与单通道数据一样快。

所以,我的问题是:有没有什么方法可以加快计算速度,无论是使用其他绘图模块(例如 Bokeh)还是以任何方式更改颜色映射?我已经尝试使用 imshow 和精心选择的地图边界,但由于它只是将图像拉伸到地图的全部范围,这对于准确映射数据来说还不够好。

以下是我的代码的精简版本,适用于具有正确模块的示例:

from pyhdf.SD import SD,SDC
import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

def get_hdf_attr(infile,dataset,attr):

    f = SD(infile,SDC.READ)
    data = f.select(dataset)
    index = data.attr(attr).index()
    attr_out = data.attr(index).get()
    f.end()

    return attr_out

def get_hdf_dataset(infile,dataset):

    f = SD(infile,SDC.READ)
    data = f.select(dataset)[:]
    f.end()

    return data

class make_rgb:

    def __init__(self,file_name):

        sds_250 = get_hdf_dataset(file_name, 'EV_250_Aggr1km_RefSB')
        scales_250 = get_hdf_attr(file_name, 'EV_250_Aggr1km_RefSB', 'reflectance_scales')
        offsets_250 = get_hdf_attr(file_name, 'EV_250_Aggr1km_RefSB', 'reflectance_offsets')

        sds_500 = get_hdf_dataset(file_name, 'EV_500_Aggr1km_RefSB')
        scales_500 = get_hdf_attr(file_name, 'EV_500_Aggr1km_RefSB', 'reflectance_scales')
        offsets_500 = get_hdf_attr(file_name, 'EV_500_Aggr1km_RefSB', 'reflectance_offsets')

        data_shape = sds_250.shape

        along_track = data_shape[1]
        cross_track = data_shape[2]

        rgb = np.zeros((along_track, cross_track, 3))

        rgb[:, :, 0] = (sds_250[0, :, :] - offsets_250[0]) * scales_250[0]
        rgb[:, :, 1] = (sds_500[1, :, :] - offsets_500[1]) * scales_500[1]
        rgb[:, :, 2] = (sds_500[0, :, :] - offsets_500[0]) * scales_500[0]

        rgb[rgb > 1] = 1.0
        rgb[rgb < 0] = 0.0

        lin = np.array([0, 30, 60, 120, 190, 255]) / 255.0
        nonlin = np.array([0, 110, 160, 210, 240, 255]) / 255.0

        scale = interp1d(lin, nonlin, kind='quadratic')

        self.img = scale(rgb)

    def plot_image(self):

        fig = plt.figure(figsize=(10, 10))
        ax = fig.add_subplot(111)

        ax.set_yticks([])
        ax.set_xticks([])
        plt.imshow(self.img, interpolation='nearest')
        plt.show()

    def plot_geo(self,geo_file,one_channel=False):

        fig = plt.figure(figsize=(10, 10))
        ax = fig.add_subplot(111)

        lats = get_hdf_dataset(geo_file, 0)
        lons = get_hdf_dataset(geo_file, 1)

        lat_0 = np.mean(lats)
        lat_range = [np.min(lats), np.max(lats)]

        lon_0 = np.mean(lons)
        lon_range = [np.min(lons), np.max(lons)]

        map_kwargs = dict(projection='cass', resolution='l',
                          llcrnrlat=lat_range[0], urcrnrlat=lat_range[1],
                          llcrnrlon=lon_range[0], urcrnrlon=lon_range[1],
                          lat_0=lat_0, lon_0=lon_0)

        m = Basemap(**map_kwargs)

        if one_channel:
            m.pcolormesh(lons, lats, self.img[:,:,0], latlon=True)
        else:
            # This is the part that is slow, but I don't know how to
            # accurately plot the data otherwise.
            mesh_rgb = self.img[:, :-1, :]
            colorTuple = mesh_rgb.reshape((mesh_rgb.shape[0] * mesh_rgb.shape[1]), 3)
            m.pcolormesh(lons, lats, self.img[:,:,0], latlon=True,color=colorTuple)

        m.drawcoastlines()
        m.drawcountries()

        plt.show()

if __name__ == '__main__':

    # https://ladsweb.nascom.nasa.gov/archive/allData/6/MOD021KM/2015/183/
    data_file = 'MOD021KM.A2015183.1005.006.2015183195350.hdf'

    # https://ladsweb.nascom.nasa.gov/archive/allData/6/MOD03/2015/183/
    geo_file = 'MOD03.A2015183.1005.006.2015183192656.hdf'

    # Very Fast
    make_rgb(data_file).plot_image()

    # Also Fast, takes about 10 seconds
    make_rgb(data_file).plot_geo(geo_file,one_channel=True)

    # Much slower, takes several minutes
    make_rgb(data_file).plot_geo(geo_file)

【问题讨论】:

    标签: python geolocation rgb matplotlib-basemap


    【解决方案1】:

    我通过将 1.0 添加到 colorTuple 的每个部分的值以将其转换为 RGBA 数组来解决此问题。我浏览了pcolormesh 函数,发现它调用颜色转换器将 RGB 转换为 RGBA 数组 4 次不同的时间,每次大约需要 50 秒。如果你给它一个 RGBA 数组来启动它,它会绕过这个并在合理的时间范围内生成绘图。添加的额外代码行如下所示:

    if one_channel:
        m.pcolormesh(lons, lats, img[:,:,0], latlon=True)
    else:
        mesh_rgb = img[:, :-1, :]
        colorTuple = mesh_rgb.reshape((mesh_rgb.shape[0] * mesh_rgb.shape[1]), 3)
    
        # ADDED THIS LINE
        colorTuple = np.insert(colorTuple,3,1.0,axis=1)
    
        # What you put in for the image doesn't matter because of the color mapping
        m.pcolormesh(lons, lats, img[:,:,0], latlon=True,color=colorTuple)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-05
      • 2014-05-29
      • 2015-06-17
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      • 2018-11-05
      • 2011-12-23
      相关资源
      最近更新 更多