Returns the scale and shift between two images More precisely, we find (b1, b2) such that img1 = b1 + b2*img2 We model this problem as a least-squares problem: (b1*, b2*) = argmin_(b1, b2) ||img1 - b1 + b2*img2||_(L2)
(img1, img2, mask=None)
| 10 | |
| 11 | |
| 12 | def get_scale_and_shift(img1, img2, mask=None): |
| 13 | """ |
| 14 | Returns the scale and shift between two images |
| 15 | More precisely, we find (b1, b2) such that img1 = b1 + b2*img2 |
| 16 | |
| 17 | We model this problem as a least-squares problem: |
| 18 | (b1*, b2*) = argmin_(b1, b2) ||img1 - b1 + b2*img2||_(L2) |
| 19 | |
| 20 | """ |
| 21 | img1_flat = img1.flatten() |
| 22 | img2_flat = img2.flatten() |
| 23 | |
| 24 | # we might get some nans and infs here, exclude them first |
| 25 | valid1 = torch.logical_and(~torch.isinf(img1_flat), ~torch.isinf(img2_flat)) |
| 26 | valid2 = torch.logical_and(~torch.isnan(img1_flat), ~torch.isnan(img2_flat)) |
| 27 | valid = torch.logical_and(valid1, valid2) |
| 28 | |
| 29 | # apply mask |
| 30 | if mask is not None: |
| 31 | mask_flat = mask.flatten() |
| 32 | valid = torch.logical_and(valid, mask_flat) |
| 33 | |
| 34 | img1_flat = img1_flat[valid] |
| 35 | img2_flat = img2_flat[valid] |
| 36 | |
| 37 | ones = torch.ones_like(img1_flat) |
| 38 | X = torch.cat((ones[None, ...], img2_flat[None, ...]), dim=0).T |
| 39 | |
| 40 | # compute analytical solution |
| 41 | b_opt = (X.T@X).float().inverse()@X.T@img1_flat # works with bf16-mixed training |
| 42 | shift, scale = b_opt |
| 43 | return shift, scale |
| 44 | |
| 45 | |
| 46 | def get_batch_scale_and_shift(img1, img2, mask=None): |
no outgoing calls
no test coverage detected