File size: 1,625 Bytes
594e028
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os

import Imath
import matplotlib.pyplot as plt
import numpy as np
import OpenEXR

script_dir = os.path.dirname(os.path.abspath(__file__))
root = os.path.dirname(script_dir)

depth_data_dir = os.path.join(root, "depth_data", "scene1", "depth_z.exr")
clib_dir = os.path.join(root, "depth_data", "scene1", "calib.json")


def load_depth_exr(path):
    f = OpenEXR.InputFile(path)
    dw = f.header()["dataWindow"]
    w = dw.max.x - dw.min.x + 1
    h = dw.max.y - dw.min.y + 1
    ch = "R" if "R" in f.header()["channels"] else next(iter(f.header()["channels"]))
    raw = f.channel(ch, Imath.PixelType(Imath.PixelType.FLOAT))
    return np.frombuffer(raw, dtype=np.float32).reshape(h, w)


def main():
    '''visualize the depth data'''
    with open(clib_dir, encoding="utf-8") as f:
        calib = json.load(f)
    unit = calib.get("unit", "?")
    res = tuple(calib["camera"]["resolution"])  # [w, h]

    z = load_depth_exr(depth_data_dir)
    assert z.shape == (res[1], res[0]), f"depth {z.shape} != calib {res[1]}x{res[0]}"

    lo, hi = np.nanpercentile(z, [2, 98])
    print("z clim", lo, hi, "valid", np.isfinite(z).sum(), f"unit={unit}")

    fig, ax = plt.subplots(figsize=(10, 5))
    im = ax.imshow(z, cmap="turbo", vmin=lo, vmax=hi)
    ax.set_title(f"Z = depth (camera) [{unit}]")
    ax.axis("off")
    plt.colorbar(im, ax=ax)
    plt.tight_layout()

    out = os.path.join(os.path.dirname(depth_data_dir), "depth_vis.png")
    plt.savefig(out, dpi=120)
    print("saved", out)
    plt.show()


if __name__ == "__main__":
    main()