首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从ESA Sentinel-2产品中加载python中的RGB图像并用openCV保存

从ESA Sentinel-2产品中加载python中的RGB图像并用openCV保存
EN

Stack Overflow用户
提问于 2018-07-17 14:05:12
回答 1查看 2.2K关注 0票数 3

从ESA snap的角度来看,对于RGB图像,我们应该把第4波段放到红色频道,第3波段放到绿色通道,第2波段放到蓝色通道中。我们如何将使用python的那些带读取到numpy数组中,以便我们可以进行任何我们想要的图像处理,然后将RGB映像保存到磁盘上?

代码语言:javascript
复制
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')

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

代码语言:javascript
复制
type(red)
# org.esa.snap.core.datamodel.Band

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-17 14:05:12

代码语言:javascript
复制
#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整数值。

代码语言:javascript
复制
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])
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51383462

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档