-
Notifications
You must be signed in to change notification settings - Fork 6
/
tool.py
53 lines (41 loc) · 1.27 KB
/
tool.py
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
import numpy as np
import cv2
def depth2Gray(im_depth):
"""
将深度图转至8位灰度图
"""
# 16位转8位
x_max = np.max(im_depth)
x_min = np.min(im_depth)
if x_max == x_min:
print('图像渲染出错 ...')
raise EOFError
k = 255 / (x_max - x_min)
b = 255 - k * x_max
return (im_depth * k + b).astype(np.uint8)
def inpaint(img, missing_value=0):
"""
Inpaint missing values in depth image.
:param missing_value: Value to fill in teh depth image.
"""
img = cv2.copyMakeBorder(img, 1, 1, 1, 1, cv2.BORDER_DEFAULT)
mask = (img == missing_value).astype(np.uint8)
# Scale to keep as float, but has to be in bounds -1:1 to keep opencv happy.
scale = np.abs(img).max()
img = img.astype(np.float32) / scale # Has to be float32, 64 not supported.
img = cv2.inpaint(img, mask, 1, cv2.INPAINT_NS)
# Back to original size and value range.
img = img[1:-1, 1:-1]
img = img * scale
return img
def crop(img, crop_w, crop_h):
"""
裁剪深度图像
"""
img_1 = img.copy()
h, w = img.shape
l = int((w - crop_w) / 2)
r = l + crop_w
t = int((h - crop_h) / 2)
b = t + crop_h
return img_1[t:b, l:r]