Load RGB image in python from ESA Sentinel-2 product and save with openCV

2.5k views Asked by At

As from ESA snap, for a RGB image we should put Band 4 into Red Channel, Band 3 into Green Channel and Band 2 into Blue Channel. How can we read those bands with python into a numpy array so we could do whatever image processing we want and then save a RGB image on disk?

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

For example here is the type of one of the above variables (same for the others):

type(red)
# org.esa.snap.core.datamodel.Band

How can I get numpy arrays from these data, and subsequently save them to disk as jpg images?

1

There are 1 answers

1
Ioannis Nasios On BEST ANSWER
#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

So far we have a rgb image in a numpy array with float values. In order to write to disk as a jpg image, we first clip large values to make image brighter, and then convert the image to 0-255 integer values.

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