-
Notifications
You must be signed in to change notification settings - Fork 37
/
gen_graycode_imgs.py
executable file
·75 lines (61 loc) · 2.39 KB
/
gen_graycode_imgs.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#coding: UTF-8
import os
import os.path
import argparse
import cv2
import numpy as np
TARGETDIR = './graycode_pattern'
CAPTUREDDIR = './capture_*'
def main():
parser = argparse.ArgumentParser(
description='Generate graycode pattern images')
parser.add_argument('proj_height', type=int, help='projector pixel height')
parser.add_argument('proj_width', type=int, help='projector pixel width')
parser.add_argument('-graycode_step', type=int,
default=1, help='step size of graycode [default:1](increase if moire appears)')
args = parser.parse_args()
step = args.graycode_step
height = args.proj_height
width = args.proj_width
gc_height = int((height-1)/step)+1
gc_width = int((width-1)/step)+1
graycode = cv2.structured_light_GrayCodePattern.create(gc_width, gc_height)
patterns = graycode.generate()[1]
# expand image size
exp_patterns = []
for pat in patterns:
img = np.zeros((height, width), np.uint8)
for y in range(height):
for x in range(width):
img[y, x] = pat[int(y/step), int(x/step)]
exp_patterns.append(img)
exp_patterns.append(255*np.ones((height, width), np.uint8)) # white
exp_patterns.append(np.zeros((height, width), np.uint8)) # black
if not os.path.exists(TARGETDIR):
os.mkdir(TARGETDIR)
for i, pat in enumerate(exp_patterns):
cv2.imwrite(TARGETDIR + '/pattern_' + str(i).zfill(2) + '.png', pat)
print('=== Result ===')
print('\'' + TARGETDIR + '/pattern_00.png ~ pattern_' +
str(len(exp_patterns)-1) + '.png \' were generated')
print()
print('=== Next step ===')
print('Project patterns and save captured images as \'' +
CAPTUREDDIR + '/graycode_*.png\'')
print()
print(
' ./ --- capture_1/ --- graycode_00.png\n'
' | |- graycode_01.png\n'
' | | .\n'
' | | .\n'
' | |- graycode_' +
str(len(exp_patterns)-1) + '.png\n'
' |- capture_2/ --- graycode_00.png\n'
' | |- graycode_01.png\n'
' | . | .\n'
' | . | .\n')
print()
print('It is recommended to capture more than 5 times')
print()
if __name__ == '__main__':
main()