0

I before computed the Normalized Difference Vegetation Index (NDVI) using two images from Sentine-2 (band 4 and band 8) this both images has a 10m resolution, however now I would like computed the Normalized Difference Red Edge Index (NDRE) but the problem for me is that this index, according to the formula:

NDRE = (NIR - RED EDGE) / (NIR + RED EDGE)

The problem is not the formula the problem is with the resolution of the images from the sentinel-2 in the band 5 (Red Edge) with has 20m of resolution and the band 8 (NIR) which has a 10m resolution:

enter image description here

is possible compute the NDRE index using two images with different resolution? which of the 3 red edge bands should I take?

I will appreciate any idea guys to achieve this computation by using sentinel-2 images. thanks

PD: To compute the ndvi I used rasterio library and gdal

Update:

I did this code from rasterio.readthedocs.io/en/latest/topics/resampling.html However I changed to a different scale factor and always I'm getting a 20m cell size or 20m pixel.

import rasterio
from rasterio.enums import Resampling

upscale_factor = 2

# load band 05 from a sentinel2 footprint
# this is 20m resolution I one an output raster of 10m resolution
with rasterio.open("images/B05.jp2", driver='JP2OpenJPEG') as dataset:

    # resample data to target shape
    data = dataset.read(
        out_shape=(
            dataset.count,
            int(dataset.height * upscale_factor),
            int(dataset.width * upscale_factor)
        ),
        resampling=Resampling.bilinear
    )

    # scale image transform
    transform = dataset.transform * dataset.transform.scale(
        (dataset.width / data.shape[-1]),
        (dataset.height / data.shape[-2])
    )

    
    #Finally I want export image.. save in the disk with crs geotiff
    #...

Should I use opencv or another libraries to achieve it?

  • so you resize them to be the same size, right? try that, then report back. pick whatever library you like for the resize operation. I'm sure OpenCV or skimage can do it, rasterio probably too. – Christoph Rackwitz Jun 20 '22 at 22:49
  • I will try resize but I'm not sure if it is good idea because are satellite images with different resolution by pixel. – FreddicMatters Jun 21 '22 at 00:17
  • sat images are getting aligned/registered and resampled all the time. if you're worried that a simple resize is introducing distortions, then you need to explore rasterio more and see what they offer in terms of image alignment/registration on a sphere/spheroid/whatever-earth-is. – Christoph Rackwitz Jun 21 '22 at 11:47
  • I found this https://rasterio.readthedocs.io/en/latest/topics/resampling.html according to it I should do Downsampling which means resampling to lower resolution/larger cellsizes. I don´t know what scale factor use, this is using 2 scale factor, what scale factor should I use to resize from 20m to 10m? a 10 scale factor? – FreddicMatters Jun 21 '22 at 15:07
  • that thing doesn't recommend anything. it merely demonstrates a particular scale factor. I recommend that you upsample so the smaller one becomes the size of the larger one. downsampling could be sensible, if you don't care about the loss of information. -- what makes you think 10 is a useful factor here? just write down the fraction of "20 m / 10 m" and you get 2. or say "10 m / 20 m", then you get *half* (0.5, or 1/2) – Christoph Rackwitz Jun 21 '22 at 15:09
  • I have updated my question and I have added the code, however when I changed the scale factor I'm getting the same pixel size as the original image (20m), could you help me with this task I could support you by paypal my email is creatorpart@gmail.com I need this urgently to compute the NDRE index. – FreddicMatters Jun 21 '22 at 17:05
  • I don't know that library... so that's code taken from https://rasterio.readthedocs.io/en/latest/topics/resampling.html so I would hope that it works. if it doesn't, you might want to consider submitting a bug report to their repository. – Christoph Rackwitz Jun 21 '22 at 18:32

1 Answers1

1

You may use another way. Firstly, you will extract the profile from the 10m-resolution image (band 08). Then you need to extract data from 20m-resolution image (band 06).

import numpy as np
import rasterio as rio
import os

# T34SFH_20230428T092031_B06.jp2  - 10m resolution
# T34SFH_20230428T092031_B08.jp2  - 20m resolution

# Extracting profile of 10m-resolution image
with rio.open('Input_images/T34SFH_20230428T092031_B08.jp2') as f:
    profile = f.profile
    
# Extracting data-table of 20m-resolution image
with rio.open('Input_images/T34SFH_20230428T092031_B06.jp2') as f:
    data = f.read()

The next step is to upsample the data using the same value with the help of np.repeat. You may use more advanced methods such nearest neighbor, bilinear, and cubic resampling.

resampling_factor = 2 # 20m/10m = 2

# upsampling the array using the same value
resampled_table = np.repeat(data[0], resampling_factor, axis=1).repeat(resampling_factor, axis=0)

The last step is to write the new image file using the profile of 10m-resolution image that you saved before and check the resolution.

# Writing new image
with rio.open('T34SFH_20230428T092031_B06_upsample_10m', 'w', **profile) as img:
    img.write(resampled_table, indexes=1)
    
# checking resolution
with rio.open('T34SFH_20230428T092031_B06_upsample_10m', 'r') as img:
    print(img.res)

The rsolution should be 10m as well. Then, you may use the formula mentioned above to calculate NDRE.

Ilias Machairas
  • 133
  • 2
  • 8