How to display an EnMAP RGB image within Python
@kleffy wrote:
Hi,
I hope this meets you well.
Please, how can I process and display this images in a python only dev environment.
Thank you.
The image above is what I get when I tried to display the RGB image of the EnMAP test product.
I a kinda a beginner in this sought of task.
Please what am I doing wrong or not doing at all?
See sample code below
# -*- coding: utf-8 -*-
"""
@author: Daniel (but not Scheffler)
"""
import os
import rasterio as rio
from rasterio.enums import Resampling
from rasterio.plot import show
import matplotlib.pyplot as plt
import numpy as np
l2a_path = r'datasets/L2A_Arcachon_3_combined/ENMAP01-____L2A-DT000400126_20170218T110119Z_003_V000204_20200512T142942Z-SPECTRAL_IMAGE.TIF'
class EnmapBook:
def __init__(self) -> None:
pass
def read_file(self, file_path) -> np.ndarray:
img = rio.open(file_path)
data = img.read()
return np.moveaxis(data, 0, -1)
def extract_rgb(self, data:np.ndarray, r_range:tuple, g_range:tuple, b_range:tuple) -> np.ndarray:
r_mean = np.mean(data[:,:,r_range[0]:r_range[-1]], axis=2)
g_mean = np.mean(data[:,:,g_range[0]:g_range[-1]], axis=2)
b_mean = np.mean(data[:,:,b_range[0]:b_range[-1]], axis=2)
rgb_img = np.zeros((data.shape[0], data.shape[1], 3))
r_mean = (r_mean - np.min(r_mean))/np.ptp(r_mean)
g_mean = (g_mean - np.min(g_mean))/np.ptp(g_mean)
b_mean = (b_mean - np.min(b_mean))/np.ptp(b_mean)
rgb_img[:,:,0]=r_mean
rgb_img[:,:,1]=g_mean
rgb_img[:,:,2]=b_mean
return rgb_img
def create_pan(self, data, pan_range:tuple) -> np.ndarray:
p_mean = np.mean(data[:,:,pan_range[0]:pan_range[-1]], axis=2)
pan = (p_mean - np.min(p_mean))/np.ptp(p_mean)
return pan
def create_grayscale(self, rgb_image:np.ndarray) -> np.ndarray:
gray = 0.216 * rgb_image[:,:,0] + 0.715 * rgb_image[:,:,1] + 0.0722 * rgb_image[:,:,2]
return gray
def display_image(self, image, save_image=False, path=None, fname='rgb_color') -> None:
plt.figure(figsize=(22, 18))
plt.axis('off')
plt.imshow(image)
plt.show()
if save_image:
if path:
fname = os.path.join(path, fname)
plt.savefig(f'{fname}.png')
enmap = EnmapBook()
img_data = enmap.read_file(l2a_path)
rgb_image = enmap.extract_rgb(data=img_data, r_range=(46, 48), g_range=(23, 25), b_range=(8, 10))
enmap.display_image(rgb_image)