从ESA Sentinel2产品中加载python中的RGB图像并使用openCV保存

2024-05-16 00:26:35 发布

您现在位置:Python中文网/ 问答频道 /正文

从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')

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

^{pr2}$

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


Tags: from图像importnumpy波段rgb数组product
1条回答
网友
1楼 · 发布于 2024-05-16 00:26:35
#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个整数值。在

^{pr2}$

相关问题 更多 >