从ESA Sentinel-2产品中加载python格式的RGB图像,并使用openCV保存
根据ESA snap,对于RGB图像,我们应该将波段4放入红色通道,波段3放入绿色通道,波段2放入蓝色通道。我们如何用python将这些带读取到从ESA Sentinel-2产品中加载python格式的RGB图像,并使用openCV保存,python,opencv,snap-python,sentinel2,Python,Opencv,Snap Python,Sentinel2,根据ESA snap,对于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
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')
例如,以下是上述变量之一的类型(其他变量相同):
如何从这些数据中获取numpy阵列,然后将它们作为jpg图像保存到磁盘
#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'对象没有属性'shape'发生了什么?
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])