File size: 2,971 Bytes
4eecd03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
"""Line a re-scan photo up with the photo a job was planned on, so a new camera read lands on the same wall pixels.

A drone or a person comes back to the wall and takes another photo: it is never from exactly the same place. Features
(AKAZE) matched between the two photos, a RANSAC homography, and the wall is planar, so one homography maps the whole
wall. The new photo's segmentation is warped into the job photo's frame; pixels the new photo does not cover are
"not observed" (never "bare"). Refuses, with the reason, when the photos do not match well enough to trust the warp.
"""
from __future__ import annotations

import cv2
import numpy as np

MIN_MATCHES = 30
MIN_INLIER_SHARE = 0.4


def _gray(rgb, side=1400):
    g = cv2.cvtColor(np.asarray(rgb), cv2.COLOR_RGB2GRAY)
    k = min(1.0, side / max(g.shape))
    return (cv2.resize(g, (round(g.shape[1] * k), round(g.shape[0] * k)), interpolation=cv2.INTER_AREA) if k < 1 else g), k


def homography(job_rgb, new_rgb):
    """3x3 H mapping new-photo pixels -> job-photo pixels, plus quality. Raises ValueError when it cannot be trusted."""
    a, ka = _gray(job_rgb)
    b, kb = _gray(new_rgb)
    ak = cv2.AKAZE_create()
    k0, d0 = ak.detectAndCompute(a, None)
    k1, d1 = ak.detectAndCompute(b, None)
    if d0 is None or d1 is None or len(k0) < MIN_MATCHES or len(k1) < MIN_MATCHES:
        raise ValueError("too little texture to line the photos up (plain wall, blur or darkness): re-shoot with the wall edges in view")
    pairs = cv2.BFMatcher(cv2.NORM_HAMMING).knnMatch(d1, d0, k=2)
    good = [m for m, n in (p for p in pairs if len(p) == 2) if m.distance < 0.8 * n.distance]
    if len(good) < MIN_MATCHES:
        raise ValueError(f"only {len(good)} matching features between the photos: is it the same wall?")
    src = np.float32([k1[m.queryIdx].pt for m in good]) / kb
    dst = np.float32([k0[m.trainIdx].pt for m in good]) / ka
    H, inl = cv2.findHomography(src, dst, cv2.RANSAC, 4.0)
    n = 0 if inl is None else int(inl.sum())
    if H is None or n < MIN_MATCHES or n < MIN_INLIER_SHARE * len(good):
        raise ValueError(f"the photos do not agree on one wall plane ({n} of {len(good)} matches): re-shoot square-on")
    det = float(np.linalg.det(H[:2, :2]))
    if not 0.2 < det < 5.0:
        raise ValueError("the alignment would stretch the photo implausibly: re-shoot from closer to where the first was taken")
    return H, {"matches": len(good), "inliers": n, "inlier_share": round(n / len(good), 3), "scale": round(det ** 0.5, 3)}


def warp_classes(classes, H, shape):
    """Warp a class map from the new photo into the job frame. Returns (warped classes, covered mask)."""
    h, w = shape
    warped = cv2.warpPerspective(np.asarray(classes, np.uint8), H, (w, h), flags=cv2.INTER_NEAREST, borderValue=255)
    covered = cv2.warpPerspective(np.full(np.asarray(classes).shape, 255, np.uint8), H, (w, h), flags=cv2.INTER_NEAREST, borderValue=0) > 0
    return warped, covered