【问题标题】:Load RGB image in python from ESA Sentinel-2 product and save with openCV从 ESA Sentinel-2 产品在 python 中加载 RGB 图像并使用 openCV 保存
【发布时间】:2018-12-25 06:41:04
【问题描述】:

根据 ESA 快照,对于 RGB 图像,我们应该将波段 4 放入红色通道,将波段 3 放入绿色通道,将波段 2 放入蓝色通道。我们如何使用 python 将这些波段读取到 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 图像保存到磁盘?

【问题讨论】:

    标签: python opencv snap-python sentinel2


    【解决方案1】:
    #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 数组中有一个带有浮点值的 rgb 图像。为了以jpg图像的形式写入磁盘,我们首先裁剪较大的值使图像更亮,然后将图像转换为0-255的整数值。

    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])
    

    【讨论】:

    • 我正在编写相同的代码,但出现以下错误: aux_band.shape = height, width AttributeError: 'org.esa.snap.core.datamodel.Band' object has no attribute “形状”发生了什么?
    猜你喜欢
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多