从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)
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映像保存到磁盘中?
发布于 2018-07-17 14:05:12
#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])https://stackoverflow.com/questions/51383462
复制相似问题