File size: 943 Bytes
142281f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
"""Visualisation helpers shared by the demo app and the figure script."""
from __future__ import annotations

import cv2
import numpy as np


def heat_overlay(img: np.ndarray, amap: np.ndarray, thr: float, alpha: float = 0.45) -> np.ndarray:
    """Jet heat-map over an RGB uint8 image. Colour scale is anchored on the
    calibrated pixel threshold: blue = 0.5*thr, red >= 1.5*thr."""
    norm = np.clip((amap - 0.5 * thr) / thr, 0, 1)
    heat = cv2.applyColorMap((norm * 255).astype(np.uint8), cv2.COLORMAP_JET)
    heat = cv2.cvtColor(heat, cv2.COLOR_BGR2RGB)  # OpenCV colormaps are BGR
    return (alpha * heat + (1 - alpha) * img).astype(np.uint8)


def draw_contours(img: np.ndarray, mask: np.ndarray, color=(255, 40, 40), thickness=2) -> np.ndarray:
    out = img.copy()
    cs, _ = cv2.findContours(mask.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cv2.drawContours(out, cs, -1, color, thickness)
    return out