代码之家  ›  专栏  ›  技术社区  ›  Ioannis Nasios

从ESA Sentinel-2产品加载python中的rgb图像并使用opencv保存

  •  2
  • Ioannis Nasios  · 技术社区  · 7 年前

    从ESA快照来看,对于RGB图像,我们应该将波段4放入红色通道,将波段3放入绿色通道,将波段2放入蓝色通道。我们怎么能把这些带着巨蟒的带子 numpy 数组,这样我们就可以做我们想要的任何图像处理,然后在磁盘上保存一个RGB图像?

    from snappy import Product
    from snappy import ProductIO
    import numpy as np
    import cv2
    
    product = ProductIO.readProduct(path_to_product)
    
    width = product.getSceneRasterWidth()
    height = product.getSceneRasterHeight()
    
    # Natural colors 
    red = product.getBand('B4')
    green = product.getBand('B3')
    blue = product.getBand('B2')
    

    例如,以下是上述变量之一的类型(其他变量相同):

    type(red)
    # org.esa.snap.core.datamodel.Band
    

    如何从这些数据中获取numpy数组,然后将它们保存到磁盘上作为JPG图像?

    1 回复  |  直到 7 年前
        1
  •  2
  •   desertnaut SKZI    7 年前
    #Read in channel's pixels    
    red_pixels = np.zeros(width * height, np.float32)
    red.readPixels(0, 0, width, height, red_pixels)
    
    green_pixels = np.zeros(width * height, np.float32)
    green.readPixels(0, 0, width, height, green_pixels)
    
    blue_pixels = np.zeros(width * height, np.float32)
    blue.readPixels(0, 0, width, height, blue_pixels)
    
    #Reshape to image dimensions
    red_pixels.shape =  height, width
    green_pixels.shape =  height, width
    blue_pixels.shape =  height, width
    
    #Combine into a RGB image
    rgb=np.zeros((height,width,3))
    rgb[...,0] = red_pixels
    rgb[...,1] = green_pixels
    rgb[...,2] = blue_pixels
    

    numpy

    rgb2 = ((np.clip(rgb.copy(),0,1))*255).astype('uint8')
    #Reverse Red-Blue Channels as open cv will reverse again upon writing image on disk
    cv2.imwrite('image_name.jpg',rgb2[...,::-1])