Evoland-Land-Monitoring-Evolution / sentinel2_superresolution

Super-resolution of 10 Sentinel-2 bands to 5-meter resolution, starting from L1C or L2A (Theia format) products.
Apache License 2.0
56 stars 7 forks source link

Issue with computing the pixel based bounding box #10

Closed eomasters-repos closed 1 month ago

eomasters-repos commented 1 month ago

I think the computation of the bounfing box is not correct if the parameter region_of_interest_pixel is used. Currently the code is like: https://github.com/Evoland-Land-Monitoring-Evolution/sentinel2_superresolution/blob/83fa1ba00e3fdea82dca71bb3ba744013e98cfa3/src/sentinel2_superresolution/run.py#L295-L300

But this leads to an error when adding the folloing parameter --region_of_interest_pixel 0 500 500 0

Traceback (most recent call last):
  File "rasterio\\_io.pyx", line 1488, in rasterio._io.DatasetWriterBase.__init__
  File "rasterio\\_err.pyx", line 221, in rasterio._err.exc_wrap_pointer
rasterio._err.CPLE_AppDefinedError: Attempt to create 968x-1000 dataset is illegal,sizes must be larger than zero.

I think the correct code should be:

roi = rio.coords.BoundingBox(
    left=s2_ds.bounds.left + 10 * roi_pixel.left,
    bottom=s2_ds.bounds.top - 10 * roi_pixel.bottom,
    right=s2_ds.bounds.left + 10 * roi_pixel.right,
    top=s2_ds.bounds.top - 10 * roi_pixel.top,
)
jmichel-otb commented 1 month ago

I think the current version is correct, but maybe the parameter documentation is unclear. The --region_of_interest_pixel switch expects col_start line_start col_end line_end, so when using your example, --region_of_interest_pixel 0 500 500 0 requests -500 lines, which causes the error.

I pushed the following patch to disambiguate: https://github.com/Evoland-Land-Monitoring-Evolution/sentinel2_superresolution/commit/f92e00e6aaea8e5be369e653b583d40164de3507

I also pushed safeguards with meaningful error: https://github.com/Evoland-Land-Monitoring-Evolution/sentinel2_superresolution/commit/cac34248fbfe9e088430a55f3bb157092ed814ff

$ sentinel2_superesolution -i ~/tmp/validation_sr/SENTINEL2A_20230624-105731-594_L2A_T31UDQ_D_V3-1.zip -roip 0 500 500 0 -o ~/tmp/test_s2sr/
[2024-08-19 11:34:24] ERROR:root:Inconsistent coordinates for region_of_interest_pixel parameter: expected line_start col_start line_end col_end, with line_end > line_start and col_end > col_start

Let me know if it works for you, so that I can close this issue.

eomasters-repos commented 1 month ago

Thanks for your update:

I've changed the parameter to this --region_of_interest_pixel 0 0 500 500 to get the upper left 500² pixels.

And now I'm getting this error:

python.exe sentinel2_superresolution\run.py -v -i D:\EOData\S2\S2A_MSIL1C_20240205T193611_N0510_R142_T09UXR_20240205T212046.SAFE -o D:\EOData\_temp\s2_supres --l1c --number_of_threads 16 --region_of_interest_pixel 0 0 500 500 
[2024-08-19 11:52:45] INFO:__main__:Will process SENTINEL2A, 2024-02-05 19:36:11, 09UXR
[2024-08-19 11:52:45] INFO:__main__:Bounds: BoundingBox(left=600000.0, bottom=5490240.0, right=709800.0, top=5600040.0), EPSG:32609
[2024-08-19 11:52:45] INFO:__main__:Will use model C:\Users\marco\PycharmProjects\sentinel2_superresolution\src\sentinel2_superresolution\models/carn_3x3x64g4sw_bootstrap.onnx
[2024-08-19 11:52:45] INFO:root:Pixel ROI set, will use it to define target ROI
[2024-08-19 11:52:45] ERROR:root:Inconsistent coordinates for region_of_interest_pixel parameter: expected line_start col_start line_end col_end, with line_end > line_start and col_end > col_start

Now, the region_of_interest_pixel parameter is converted to roi_pixel in Line#305 https://github.com/Evoland-Land-Monitoring-Evolution/sentinel2_superresolution/blob/cac34248fbfe9e088430a55f3bb157092ed814ff/src/sentinel2_superresolution/run.py#L305

col_start -> roi_pixel.left,
line_start -> roi_pixel.bottom,
col_end -> roi_pixel.right,
line_end -> roi_pixel.top,

and I think top and bottom should be swapped.

jmichel-otb commented 1 month ago

My apologies. I should have checked it better.

Here is another fix: https://github.com/Evoland-Land-Monitoring-Evolution/sentinel2_superresolution/commit/7e486735a4ef48b10dc880684d7a974ddc589a52

$ sentinel2_superesolution -i ~/tmp/validation_sr/SENTINEL2A_20230624-105731-594_L2A_T31UDQ_D_V3-1.zip -roip 0 0 500 500 -o ~/tmp/test_s2sr/
Super-resolution in progress ...: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:17<00:00, 17.57s/it]

Yields the following (red patch is the produced SISR image): pixel_coordinates_bug

eomasters-repos commented 1 month ago

Great! It works now. Thanks for the effort.

jmichel-otb commented 1 month ago

Thanks for reporting !