GFZ / arosics

AROSICS - Automated and Robust Open-Source Image Co-Registration Software
https://git.gfz-potsdam.de/danschef/arosics
Apache License 2.0
146 stars 26 forks source link

Problem with bad data mask after shift corrwction #48

Closed danieliman closed 3 months ago

danieliman commented 3 months ago

arosics version: v1.11.0 Python version: 3.11.9 Operating System: windows 11

Description

Hello again. I'm trying to add bad data mask of the target image for coregistration. But i'm experiencing some problems after the shift correction.

I have checked and confirmed that the target image and the bad data mask have same geotransformation before coregistration. Thank you in advance :)

What I Did

from arosics import COREG_LOCAL

# Install and set the matplotlib backend to Qt6Agg
import matplotlib
matplotlib.use('Qt5Agg')

# Define input images
im_reference = 'D:/Daniel/S2A_OPER_MSI_L1C_TL_MPC__20160810T071128_A005920_T52SGC_B04.jp2'
im_target = 'D:/Daniel/GRUS1C_20231111013317/GRUS1C_20231111_red.tif'

kwargs = {
    'grid_res'       : 50,
    'window_size'    : (75,75),
    'path_out'       : '20231111_red_withmask',
    'projectDir'     : 'D:/Daniel/GRUS1C_20231111013317/red/withmask',
    'min_reliability': 30,
    'mask_baddata_tgt': 'D:/Daniel/GRUS1C_20231111013317/GRUS1C_20231111_cloud.tif',
    'q'              : False
}

# Initialize COREG_LOCAL object
CRL = COREG_LOCAL(im_reference, im_target, **kwargs)

# Correct shifts
CRL.correct_shifts()

# Visualize tie point grid with initial shifts
CRL.view_CoRegPoints(figsize=(15,15), backgroundIm='ref')
#Note: array has been downsampled to 1000 x 1000 for faster visualization.

# Visualize tie point grid AFTER shift correction
CRL_after_corr = COREG_LOCAL(im_reference, CRL.path_out, **kwargs)
CRL_after_corr.view_CoRegPoints(figsize=(15,15),backgroundIm='ref')

# Show the points table of the calculated tie point grid
CRL.CoRegPoints_table

This is the output

Bounding box of calculated footprint for image to be shifted:
        (712305.0, 3759405.0, 717580.0, 3764755.0)
Matching window position (X,Y): 714939.4719440191/3762076.535272802
Traceback (most recent call last):
  File "c:\Users\danie\AROSICS\original.py", line 32, in <module>
    CRL_after_corr = COREG_LOCAL(im_reference, CRL.path_out, **kwargs)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\danie\anaconda3\Lib\site-packages\arosics\CoReg_local.py", line 360, in __init__
    self.COREG_obj.shift.mask_baddata = mask_baddata_tgt
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\danie\anaconda3\Lib\site-packages\geoarray\baseclasses.py", line 502, in mask_baddata
    assert geoArr_mask.shape[:2] == self.shape[:2], 'The provided bad data mask must have the same number of ' \
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: The provided bad data mask must have the same number of rows and columns as the the GeoArray_CoReg '20231111_red_withmask' itself.
danschef commented 3 months ago

Did you check the image dimensions (number of rows and columns) of D:/Daniel/GRUS1C_20231111013317/GRUS1C_20231111_red.tif and D:/Daniel/GRUS1C_20231111013317/GRUS1C_20231111_cloud.tif? They need to be equal.

danieliman commented 3 months ago

Yes I did check them both beforehand

This is the target image D:/Daniel/GRUS1C_20231111013317/GRUS1C_20231111_red.tif Dimensions X: 1057 Y: 1072 Bands: 1 Origin 712301.4191808401374146,3764755.5040725860744715 Pixel Size 5,-5

This is the bad data mask D:/Daniel/GRUS1C_20231111013317/GRUS1C_20231111_cloud.tif Dimensions X: 1057 Y: 1072 Bands: 1 Origin 712301.4191808401374146,3764755.5040725860744715 Pixel Size 5,-5

For reference, this is the target image after shift correction 20231111_red_withmask Dimensions X: 1056 Y: 1071 Bands: 1 Origin 712305.0000000000000000,3764755.0000000000000000 Pixel Size 5,-5

I think the issue is because the different image dimensions after shift correction, but i am not sure

danschef commented 3 months ago

I think the issue is because the different image dimensions after shift correction, but i am not sure

Not sure what you mean with this. So far as I understand, the co-registration fails, so you also don´t have an "image after shift correction".

However, looks like I need the input data to reproduce this. Would you mind uploading it somewhere?

danieliman commented 3 months ago

Thanks! Here's the link (https://drive.google.com/drive/folders/1NuT-w-ztQo6UjMEsqFSrpKGqCjyUf4PW?usp=sharing)

I don't know why but I also got an output (shifted image) afterwards, which I also include in the link

danschef commented 3 months ago

Ok, I reproduced it and noticed that I misunderstood your question a bit. I overlooked that you are calling COREG_LOCAL twice:

  1. with the target image and the mask in the same dimension which works fine and produces you the output 20231111_red_withmask as an ENVI BSQ file
  2. with the co-registered (and thus resampled) output from the first run + the original (and not resampled) mask

The second call cannot work because the requirement is that the target image and the mask is provided in the same shape. Since you are not using the original target image but the already co-registered one (CRL.path_out), they don´t have the same dimension. You would have to crop the co-registered result to the extent of the mask or vice versa before calling COREG_LOCAL the second time. However, as you are just validating the shifts after correction, you can also leave the mask out.

Please also note that reducing the window size to 75x75 pixels will decrease the accuracy of the derived shifts. I suggest to just not touch the defaults here (i.e., use 256x256). Same applies to min_reliability. Including tie points with less than 60% reliability leads to less accurate results.

So I would just go for something like this:

from arosics import COREG_LOCAL

# Define input images
im_reference = 'D:/Daniel/S2A_OPER_MSI_L1C_TL_MPC__20160810T071128_A005920_T52SGC_B04.jp2'
im_target = 'D:/Daniel/GRUS1C_20231111013317/GRUS1C_20231111_red.tif'
im_mask = 'D:/Daniel/GRUS1C_20231111013317/mask.tif'

kwargs = {
    'grid_res'       : 25,
    'path_out'       : '20231111_red_withmask',
    'projectDir'     : 'D:/Daniel/GRUS1C_20231111013317/red/withmask',
    'mask_baddata_tgt': 'D:/Daniel/GRUS1C_20231111013317/GRUS1C_20231111_cloud.tif',
}

# Initialize COREG_LOCAL object
CRL = COREG_LOCAL(im_reference, im_target, **kwargs)

# Correct shifts
CRL.correct_shifts()

# Visualize tie point grid with initial shifts
CRL.view_CoRegPoints(figsize=(15,15), backgroundIm='tgt', shapes2plot='vectors', vector_scale=10)

del kwargs['mask_baddata_tgt']

# Visualize tie point grid AFTER shift correction
CRL_after_corr = COREG_LOCAL(im_reference, CRL.path_out, **kwargs)
CRL_after_corr.view_CoRegPoints(figsize=(15,15), backgroundIm='tgt', shapes2plot='vectors', vector_scale=10)

# Show the points table of the calculated tie point grid
CRL.CoRegPoints_table

This gives me the following output:

Polygonize progress     |=====================-----------------------------| 41.1% Complete  => 0:00:00
Calculating footprint polygon and actual data corner coordinates for reference image...
Bounding box of calculated footprint for reference image:
    (699960.0, 3690240.0, 809760.0, 3800040.0)
Calculating footprint polygon and actual data corner coordinates for image to be shifted...
Polygonize progress     |==================================================| 100.0% Complete  => 0:00:00
Warping progress     |======================================------------| 75.0% Complete  => 0:00:00
Bounding box of calculated footprint for image to be shifted:
    (712301.4191808401, 3759395.504072586, 717586.4191808401, 3764755.504072586)
Matching window position (X,Y): 714942.2650936018/3762075.9513885323
Initializing tie points grid...
Equalizing pixel grids and projections of reference and target image...
Warping progress     |==================================================| 100.0% Complete  => 0:00:00
Warping progress     |==================================================| 100.0% Complete  => 0:00:00
Calculating tie point grid (1735 points) using 32 CPU cores...
    progress: |==================================================| 100.0% Complete  => 0:00:16
Found 1470 matches.
Performing validity checks...
493 tie points flagged by level 1 filtering (reliability).
459 tie points flagged by level 2 filtering (SSIM).
25 tie points flagged by level 3 filtering (RANSAC)
651 valid tie points remain after filtering.
Correcting geometric shifts...
Translating progress |==================================================| 100.0% Complete  => 0:00:00
Warping progress     |==================================================| 100.0% Complete  => 0:00:00
Writing GeoArray of size (1071, 1056) to /home/gfz-fe/scheffler/temp/coreg_issue_daniel_iman/20231111_red_withmask.
Note: array has been downsampled to 986 x 999 for faster visualization.

grafik

Polygonize progress     |=================---------------------------------| 33.0% Complete  => 0:00:00
Calculating footprint polygon and actual data corner coordinates for reference image...
Bounding box of calculated footprint for reference image:
    (699960.0, 3690240.0, 809760.0, 3800040.0)
Calculating footprint polygon and actual data corner coordinates for image to be shifted...
Polygonize progress     |==================================================| 100.0% Complete  => 0:00:00
Bounding box of calculated footprint for image to be shifted:
    (712305.0, 3759400.0, 717585.0, 3764755.0)
Matching window position (X,Y): 714943.1388322699/3762074.049905645
Note: array has been downsampled to 985 x 1000 for faster visualization.
Warping progress     |==========================================--------| 84.3% Complete  => 0:00:00
Initializing tie points grid...
Equalizing pixel grids and projections of reference and target image...
Warping progress     |==================================================| 100.0% Complete  => 0:00:00
Warping progress     |==================================================| 100.0% Complete  => 0:00:00
Calculating tie point grid (1743 points) using 32 CPU cores...
    progress: |==================================================| 100.0% Complete  => 0:00:17
Found 1490 matches.
Performing validity checks...
590 tie points flagged by level 1 filtering (reliability).
686 tie points flagged by level 2 filtering (SSIM).
14 tie points flagged by level 3 filtering (RANSAC)
522 valid tie points remain after filtering.

grafik

danieliman commented 3 months ago

Okay thanks! Much appreciated !