File size: 2,810 Bytes
c147de1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
56
# SPDX-License-Identifier: Apache-2.0
"""Host pre-processing of the camera frames, as VisionPilot does it (vision_pilot 87a82ec8):

- AutoSteer / AutoSpeed (``front_image``): top-crop to 2:1 (``compute_top_crop_2_1``) and ``cv::resize`` to
  1024 x 512 (INTER_LINEAR) of the raw frame (``modules/sensing/image_preprocessing``);
- AutoDrive (``warped_image``): ``cv::warpPerspective`` of the raw frame by the camera homography ``C`` to 1024 x 512
  (INTER_LINEAR, BORDER_REFLECT_101). ``center_crop_homography`` builds the ``C`` of AutoDrive's training crop
  (a 50 deg horizontal centre crop of a camera of known field of view, 2:1, ``load_data_auto_drive.py``); a frame
  that is already 1024 x 512 is taken as warped when no homography is given.

Every function takes and returns RGB uint8 HxWx3 (OpenCV's resize / warp treat channels independently, so the
channel order does not change the result).
"""
from __future__ import annotations

from typing import Optional, Tuple

import numpy as np

from ..models import NET_H, NET_W, top_crop_2_1


def front_image(rgb: np.ndarray) -> Tuple[np.ndarray, dict]:
    """Raw frame -> (1024 x 512 network image, geometry to map network pixels back to the frame)."""
    import cv2

    h, w = rgb.shape[:2]
    top = top_crop_2_1(h, w)
    img = rgb if (h, w) == (NET_H, NET_W) and top == 0 else cv2.resize(rgb[top:], (NET_W, NET_H),
                                                                        interpolation=cv2.INTER_LINEAR)
    geom = {"input_hw": [int(h), int(w)], "crop_top": int(top), "scale_x": w / NET_W, "scale_y": (h - top) / NET_H}
    return np.ascontiguousarray(img), geom


def center_crop_homography(width: int, height: int, hfov_deg: float, crop_fov_deg: float = 50.0) -> np.ndarray:
    """``C`` (raw pixel -> warped 1024 x 512) of AutoDrive's training crop: ``crop_w = round(W * 50 / hfov)``,
    ``crop_h = crop_w // 2``, centred both ways (``_center_crop_50deg_resize``)."""
    cw = int(round(width * crop_fov_deg / hfov_deg))
    ch = cw // 2
    x0 = (width - cw) // 2
    y0 = (height - ch) // 2
    sx, sy = NET_W / cw, NET_H / ch
    return np.array([[sx, 0.0, -x0 * sx], [0.0, sy, -y0 * sy], [0.0, 0.0, 1.0]], np.float64)


def warped_image(rgb: np.ndarray, homography: Optional[np.ndarray]) -> np.ndarray:
    import cv2

    if homography is None:
        if rgb.shape[:2] != (NET_H, NET_W):
            raise ValueError(f"a {rgb.shape[1]}x{rgb.shape[0]} frame needs the camera homography (3x3, raw pixel -> "
                             f"warped {NET_W}x{NET_H}); send a {NET_W}x{NET_H} warped frame otherwise")
        return np.ascontiguousarray(rgb)
    C = np.asarray(homography, np.float64).reshape(3, 3)
    return cv2.warpPerspective(rgb, C, (NET_W, NET_H), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REFLECT_101)