1# Calculates comparison output values for DistortionMapperTest.cpp:CompareToOpenCV
2#
3# Assumes a python that has numpy and cv2 (OpenCV) available
4
5import numpy as np
6import cv2
7
8Fx = 1000
9Fy = 1000
10Cx = 500
11Cy = 500
12# s = 0 - not supported by OpenCV
13
14K = np.array([[Fx, 0, Cx],[0, Fy, Cy],[0, 0, 1]])
15
16# Order is k1, k2, t1, t2, k3
17dist = np.array([0.1, -0.003, 0.02, 0.01, 0.004])
18
19np.random.seed(1234)
20
21activeArray = np.array([[1000, 750]])
22
23rawCoords = np.floor(np.random.rand(1000,2) * activeArray)
24
25# OpenCV needs either row count or col count = 1 for some reason
26rawCoords2 = rawCoords.reshape(-1, 1, 2)
27
28# P is the output camera matrix, K is the input; use the same for both
29expCoords = cv2.undistortPoints(rawCoords2, K, dist, P = K)
30
31with open('DistortionMapperTest_OpenCvData.h','w') as f:
32  f.write('// Generated by DistortionMapperComp.py\n');
33  f.write('// for use by DistortionMapperTest.cpp\n\n');
34
35  f.write('namespace openCvData {\n')
36  f.write('std::array<int32_t, %d> rawCoords = {\n' % (rawCoords.shape[0] * rawCoords.shape[1]))
37  for i in range(rawCoords.shape[0]):
38    f.write('  %d, %d,\n' % (rawCoords[i][0], rawCoords[i][1]))
39  f.write('};\n')
40
41  f.write('std::array<int32_t, %d> expCoords = {\n' % (expCoords.shape[0] * expCoords.shape[2]))
42  for i in range(expCoords.shape[0]):
43    f.write('  %d, %d,\n' % (expCoords[i][0][0], expCoords[i][0][1]))
44  f.write('};\n')
45  f.write('} // namespace openCvData\n')
46
47print "DistortionMapperTest_OpenCvData.h generated"
48