This repository was archived by the owner on Jan 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmin_max_projector_calibration.py
More file actions
143 lines (112 loc) · 4.49 KB
/
min_max_projector_calibration.py
File metadata and controls
143 lines (112 loc) · 4.49 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import matplotlib
import json
matplotlib.use('TkAgg')
# from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
# Implement the default mpl key bindings
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import sys
if sys.version_info[0] < 3:
import Tkinter as Tk
else:
import tkinter as Tk
from tkinter import ttk
import numpy as np
import cv2
def MinMaxProjectorCalibration(patterns, cameras, projector):
end = False
pattern_num = 0
def quit(root):
nonlocal end
end = True
root.quit() # Stops mainloop
root.destroy() # This is necessary on Windows to prevent
# Fatal Python Error: PyEval_RestoreThread: NULL tstate
root = Tk.Tk()
root.wm_title("Embedding in TK")
# Callback function to show next pattern
def next_pattern(root):
nonlocal pattern_num
pattern_num = pattern_num + 1
if pattern_num == len(patterns):
pattern_num = 0
f = Figure(figsize=(5, 4), dpi=100)
# A tk.DrawingArea
canvas = FigureCanvasTkAgg(f, master=root)
canvas.draw()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
button = Tk.Button(master=root, text='Quit', command=lambda: quit(root))
button.pack(side=Tk.BOTTOM)
button = Tk.Button(master=root, text='Next', command=lambda: next_pattern(root))
button.pack(side=Tk.BOTTOM)
def slider_max_changed(event):
projector.max_image_brightness = current_max_brightness.get()
def slider_min_changed(event):
projector.min_image_brightness = current_min_brightness.get()
current_max_brightness = Tk.DoubleVar(value=projector.max_image_brightness)
current_min_brightness = Tk.DoubleVar(value=projector.min_image_brightness)
scale_max = Tk.Scale(root, orient="horizontal",
from_=0, to=1.0,
digits=4,
resolution=0.01,
variable=current_max_brightness,
command=slider_max_changed,
length=300)
scale_min = Tk.Scale(root, orient="horizontal",
from_=0, to=1.0,
digits=4,
resolution=0.01,
variable=current_min_brightness,
command=slider_min_changed,
length=300)
label_1 = Tk.Label(root, text="Max Brightness")
label_2 = Tk.Label(root, text="Min Brightness")
label_1.pack(side=Tk.TOP)
scale_max.pack(side=Tk.TOP)
label_2.pack(side=Tk.TOP)
scale_min.pack(side=Tk.TOP)
projector.set_up_window()
while True:
projector.project_pattern(patterns[pattern_num][0])
if cameras[0].type == 'web':
_1 = cameras[0].get_image()
if cameras[1].type == 'web':
_2 = cameras[1].get_image()
frame_1 = cameras[0].get_image()
frame_2 = cameras[1].get_image()
if cameras[0].type == 'web':
frame_1 = cv2.cvtColor(frame_1, cv2.COLOR_BGR2GRAY)
if cameras[1].type == 'web':
frame_2 = cv2.cvtColor(frame_2, cv2.COLOR_BGR2GRAY)
delta_height = 50
ROI = slice(int(frame_1.shape[0] / 2 - delta_height), int(frame_1.shape[0] / 2 + delta_height))
a1 = f.add_subplot(221)
a1.plot(np.mean(frame_1[ROI, :], axis=0))
# a1.set_ylim((0, 4096))
b1 = f.add_subplot(222)
b1.imshow(frame_1)
a2 = f.add_subplot(223)
a2.plot(np.mean(frame_2[ROI, :], axis=0))
# a2.set_ylim((0, 4096))
b2 = f.add_subplot(224)
b2.imshow(frame_2)
root.update()
if (end):
# Save results of calibration
with open('config.json') as f:
data = json.load(f)
data['projector']["min_brightness"] = projector.min_image_brightness
data['projector']["max_brightness"] = projector.max_image_brightness
with open('config.json', 'w') as f:
json.dump(data, f, ensure_ascii=False, indent=4)
projector.close_window()
break
canvas.draw()
f.delaxes(a1)
f.delaxes(b1)
f.delaxes(a2)
f.delaxes(b2)