-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcamera_pixel_calibration.py
executable file
·133 lines (96 loc) · 4.33 KB
/
camera_pixel_calibration.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from skimage.feature import match_template
from random import random, choice
from camera import camera
from goniometer import goniometer
import pickle
import numpy as np
import pylab
import time
g = goniometer()
cam = camera()
def main():
import optparse
parser = optparse.OptionParser()
parser.add_option('-z', '--zoom', default=10, type=int, help='Zoom (default=%default)')
parser.add_option('-N', '--number_of_points', default=40, type=int, help='Number of points')
parser.add_option('-c', '--crop', default=32, type=int, help='crop size')
parser.add_option('-n', '--name_pattern', default='date', type='str', help='distinguishing name for the result files')
options, args = parser.parse_args()
zoom = options.zoom
number_of_points = options.number_of_points
if options.name_pattern == 'date':
name_pattern = time.strftime('%Y-%M-%d')
else:
name_pattern = options.name_pattern
cam.set_zoom(zoom)
init_image = cam.get_image(color=False)
shape = np.array(init_image.shape[:2])
center = shape/2
cam.set_gain(3)
template = init_image[center[0]-options.crop: center[0]+options.crop, center[1]-options.crop: center[1]+options.crop]
available_range = shape * cam.get_calibration() * 0.15
reference_position = g.get_aligned_position()
v = reference_position['AlignmentZ']
h = reference_position['AlignmentY']
mt = match_template(init_image, template)
mt = mt.reshape(mt.shape[:2])
position_max = np.unravel_index(mt.argmax(), mt.shape)
results = [[0., 0., position_max[0], position_max[1]]]
for k in range(number_of_points):
print('point %d,' % k)
v = random() * available_range[0] * choice([-1, 1])
h = random() * available_range[1] * choice([-1, 1])
new_position = dict([(key, reference_position[key]) for key in reference_position])
new_position['AlignmentZ'] += v
new_position['AlignmentY'] += h
g.set_position(new_position, wait=True)
mt = match_template(cam.get_image(color=False), template)
mt = mt.reshape(mt.shape[:2])
position_max = np.unravel_index(mt.argmax(), mt.shape)
result = [v, h, position_max[0], position_max[1]]
results.append(result)
results = np.array(results)
results_dictionary = {'results': results}
results_from_origin = results - results[0, :]
rfo = results_from_origin[1:, :]
results_dictionary['results_from_origin'] = results_from_origin
results_dictionary['rfo'] = rfo
horizontal_calibration = np.abs(rfo[:,1])/np.abs(rfo[:,3])
vertical_calibration = np.abs(rfo[:,0])/np.abs(rfo[:,2])
results_dictionary['horizontal_calibration'] = horizontal_calibration
results_dictionary['vertical_calibration'] = vertical_calibration
print('vertical_calibration')
print(vertical_calibration)
print('vertical mean %.10f, median %.10f' % (vertical_calibration.mean(), np.median(vertical_calibration)))
print('current vertical camera calibration', cam.get_calibration()[0])
print('horizontal_calibration')
print(horizontal_calibration)
print('horizontal mean %.10f, median %.10f' % (horizontal_calibration.mean(), np.median(horizontal_calibration)))
print('current horizontal camera calibration', cam.get_calibration()[1])
print()
g.set_position(reference_position)
f = open('camera_calibration_results_zoom_%d_%s.pickle' % (zoom, name_pattern), 'wb')
pickle.dump(results_dictionary, f)
f.close()
pylab.plot(vertical_calibration, label='vertical calibration')
pylab.plot(horizontal_calibration, label='horizontal_calibration')
pylab.grid(True)
pylab.xlabel('point')
pylab.ylabel('mm/pixel')
pylab.legend()
pylab.savefig('camera_calibration_results_zoom_%d_%s.png' % (zoom, name_pattern))
#pylab.show()
#old zoom 10 calibration X: 1.5746e-4, Y: 1.6108e-4
def get_current_calibrations(sleep_time=0.5):
calibrations = {}
for z in range(10, 0, -1):
cam.set_zoom(z, wait=True)
time.sleep(sleep_time)
calibrations[z] = np.array([g.md2.CoaxCamScaleY, g.md2.CoaxCamScaleX])
return calibrations
def test():
pass
if __name__ == '__main__':
main()