1 # Calculates comparison output values for DistortionMapperTest.cpp:CompareToOpenCV 2 # 3 # Assumes a python that has numpy and cv2 (OpenCV) available 4 5 import numpy as np 6 import cv2 7 8 Fx = 1000 9 Fy = 1000 10 Cx = 500 11 Cy = 500 12 # s = 0 - not supported by OpenCV 13 14 K = np.array([[Fx, 0, Cx],[0, Fy, Cy],[0, 0, 1]]) 15 16 # Order is k1, k2, t1, t2, k3 17 dist = np.array([0.1, -0.003, 0.02, 0.01, 0.004]) 18 19 np.random.seed(1234) 20 21 activeArray = np.array([[1000, 750]]) 22 23 rawCoords = np.floor(np.random.rand(1000,2) * activeArray) 24 25 # OpenCV needs either row count or col count = 1 for some reason 26 rawCoords2 = rawCoords.reshape(-1, 1, 2) 27 28 # P is the output camera matrix, K is the input; use the same for both 29 expCoords = cv2.undistortPoints(rawCoords2, K, dist, P = K) 30 31 with 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 47 print "DistortionMapperTest_OpenCvData.h generated" 48