File size: 2,856 Bytes
1d21e88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f65c627
 
 
 
1d21e88
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""Native-resolution flow candidates composed into source coordinate maps.

Photometric agreement is only a gate, never an anatomical accuracy measurement.
No normalized or previously warped color image is used for the delivered output.
"""
import cv2
import numpy as np
from .preprocessing import normalize, glare_mask


def refine_native(reference, moving, mapping, support):
    h, w = reference.shape[:2]
    aligned = cv2.remap(moving, mapping[..., 0], mapping[..., 1], cv2.INTER_LINEAR)
    a, b = normalize(reference), normalize(aligned)
    def flow(left, right):
        return cv2.calcOpticalFlowFarneback(left, right, None, .5, 4, 21, 5, 7, 1.5, 0)
    forward = cv2.GaussianBlur(flow(a, b), (0, 0), 1.2)
    reverse = flow(b, a)
    y, x = np.indices((h, w), dtype=np.float32)
    mx, my = x+forward[..., 0], y+forward[..., 1]
    fb = np.linalg.norm(forward+cv2.remap(reverse, mx, my, cv2.INTER_LINEAR), axis=2)
    before = cv2.GaussianBlur(cv2.absdiff(a, b).astype(np.float32), (0, 0), 2)
    after = cv2.GaussianBlur(cv2.absdiff(a, cv2.remap(b, mx, my, cv2.INTER_LINEAR)).astype(np.float32), (0, 0), 2)
    valid = support & ~glare_mask(reference) & ~glare_mask(aligned)
    valid &= cv2.remap(valid.astype(np.uint8), mx, my, cv2.INTER_NEAREST).astype(bool)
    trusted = valid & (fb < .75) & (np.linalg.norm(forward, axis=2) < 6) & (after < before) & (after < 18)
    trusted = cv2.erode(trusted.astype(np.uint8), np.ones((7, 7), np.uint8)).astype(bool)
    # Smoothly bring residual motion to zero at unsupported boundaries.
    weight = np.minimum(cv2.distanceTransform(trusted.astype(np.uint8), cv2.DIST_L2, 3)/12, 1)
    candidate = cv2.remap(mapping, x+forward[..., 0]*weight, y+forward[..., 1]*weight, cv2.INTER_LINEAR)
    delta = candidate-mapping
    uy, ux = np.gradient(delta[..., 0]); vy, vx = np.gradient(delta[..., 1])
    cy, cx = np.gradient(candidate[..., 0]); dy, dx = np.gradient(candidate[..., 1])
    determinant = cx*dy-cy*dx
    by, bx = np.gradient(mapping[..., 0]); ey, ex = np.gradient(mapping[..., 1])
    original_determinant = bx*ey-by*ex
    ratio = determinant/np.maximum(original_determinant, 1e-6)
    safe = np.isfinite(candidate).all(axis=2) & (original_determinant > 0) & (ratio > .8) & (ratio < 1.2) & (np.sqrt(ux**2+uy**2+vx**2+vy**2) < .35)
    safe &= (candidate[..., 0]>=0)&(candidate[..., 0]<moving.shape[1]-1)&(candidate[..., 1]>=0)&(candidate[..., 1]<moving.shape[0]-1)
    # Reject the candidate as a whole instead of introducing seams by patching bad pixels.
    applied = bool(trusted.any() and safe[support].all() and trusted.sum()/max(support.sum(), 1) > .25)
    return (candidate if applied else mapping.copy()), dict(applied=applied, trusted_fraction=float(trusted.sum()/max(support.sum(), 1)), max_motion_px=float(np.linalg.norm(delta, axis=2)[support].max()) if support.any() else 0.)