class Test_image_coordinates(unittest.TestCase): def setUp(self): self.control = ControlParams(4) self.calibration = Calibration() def test_img_coord_typecheck(self): with self.assertRaises(TypeError): list = [[0 for x in range(3)] for x in range(10)] # initialize a 10x3 list (but not numpy matrix) flat_image_coordinates(list, self.control, out=None) with self.assertRaises(TypeError): flat_image_coordinates(np.empty((10, 2)), self.calibration, self.control.get_multimedia_params(), output=None) with self.assertRaises(TypeError): image_coordinates(np.empty((10, 3)), self.calibration, self.control.get_multimedia_params(), output=np.zeros((10, 3))) with self.assertRaises(TypeError): image_coordinates(np.zeros((10, 2)), self.calibration, self.control.get_multimedia_params(), output=np.zeros((10, 2))) def test_image_coord_regress(self): self.calibration.set_pos(np.array([0, 0, 40])) self.calibration.set_angles(np.array([0, 0, 0])) self.calibration.set_primary_point(np.array([0, 0, 10])) self.calibration.set_glass_vec(np.array([0, 0, 20])) self.calibration.set_radial_distortion(np.array([0, 0, 0])) self.calibration.set_decentering(np.array([0, 0])) self.calibration.set_affine_trans(np.array([1, 0])) self.mult = MultimediaParams(n1=1, n2=np.array([1]), n3=1, d=np.array([1])) input = np.array([[10., 5., -20.], [10., 5., -20.]]) # vec3d output = np.zeros((2, 2)) x = 10. / 6. y = x / 2. correct_output = np.array([[x, y], [x, y]]) flat_image_coordinates(input=input, cal=self.calibration, mult_params=self.mult, output=output) np.testing.assert_array_equal(output, correct_output) output=np.full((2,2), 999.) image_coordinates(input=input, cal=self.calibration, mult_params=self.mult, output=output) np.testing.assert_array_equal(output, correct_output)
def test_full_corresp(self): """Full scene correspondences""" cpar = ControlParams(4) cpar.read_control_par(r"testing_fodder/corresp/control.par") vpar = VolumeParams() vpar.read_volume_par(r"testing_fodder/corresp/criteria.par") # Cameras are at so high angles that opposing cameras don't see each # other in the normal air-glass-water setting. cpar.get_multimedia_params().set_layers([1.0001], [1.]) cpar.get_multimedia_params().set_n3(1.0001) cals = [] img_pts = [] corrected = [] for c in xrange(4): cal = Calibration() cal.from_file( "testing_fodder/calibration/sym_cam%d.tif.ori" % (c + 1), "testing_fodder/calibration/cam1.tif.addpar") cals.append(cal) # Generate test targets. targs = TargetArray(16) for row, col in np.ndindex(4, 4): targ_ix = row * 4 + col # Avoid symmetric case: if (c % 2): targ_ix = 15 - targ_ix targ = targs[targ_ix] pos3d = 10 * np.array([[col, row, 0]], dtype=np.float64) pos2d = image_coordinates(pos3d, cal, cpar.get_multimedia_params()) targ.set_pos(convert_arr_metric_to_pixel(pos2d, cpar)[0]) targ.set_pnr(targ_ix) targ.set_pixel_counts(25, 5, 5) targ.set_sum_grey_value(10) img_pts.append(targs) mc = MatchedCoords(targs, cpar, cal) corrected.append(mc) sorted_pos, sorted_corresp, num_targs = correspondences( img_pts, corrected, cals, vpar, cpar) self.failUnlessEqual(num_targs, 16)
def test_full_corresp(self): """Full scene correspondences""" print "about to dump core" cpar = ControlParams(4) cpar.read_control_par("testing_fodder/corresp/control.par") vpar = VolumeParams() vpar.read_volume_par("testing_fodder/corresp/criteria.par") # Cameras are at so high angles that opposing cameras don't see each # other in the normal air-glass-water setting. cpar.get_multimedia_params().set_layers([1.0001], [1.]) cpar.get_multimedia_params().set_n3(1.0001) cals = [] img_pts = [] corrected = [] for c in xrange(4): cal = Calibration() cal.from_file( "testing_fodder/calibration/sym_cam%d.tif.ori" % (c + 1), "testing_fodder/calibration/cam1.tif.addpar") cals.append(cal) # Generate test targets. targs = TargetArray(16) for row, col in np.ndindex(4, 4): targ_ix = row*4 + col # Avoid symmetric case: if (c % 2): targ_ix = 15 - targ_ix targ = targs[targ_ix] pos3d = 10*np.array([[col, row, 0]], dtype=np.float64) pos2d = image_coordinates( pos3d, cal, cpar.get_multimedia_params()) targ.set_pos(convert_arr_metric_to_pixel(pos2d, cpar)[0]) targ.set_pnr(targ_ix) targ.set_pixel_counts(25, 5, 5) targ.set_sum_grey_value(10) img_pts.append(targs) mc = MatchedCoords(targs, cpar, cal) corrected.append(mc) sorted_pos, sorted_corresp, num_targs = correspondences( img_pts, corrected, cals, vpar, cpar) self.failUnlessEqual(num_targs, 16)
def test_single_cam_corresp(self): """Single camera correspondence""" cpar = ControlParams(1) cpar.read_control_par(b"testing_fodder/single_cam/parameters/ptv.par") vpar = VolumeParams() vpar.read_volume_par(b"testing_fodder/single_cam/parameters/criteria.par") # Cameras are at so high angles that opposing cameras don't see each # other in the normal air-glass-water setting. cpar.get_multimedia_params().set_layers([1.], [1.]) cpar.get_multimedia_params().set_n3(1.) cals = [] img_pts = [] corrected = [] cal = Calibration() cal.from_file( b"testing_fodder/single_cam/calibration/cam_1.tif.ori", b"testing_fodder/single_cam/calibration/cam_1.tif.addpar") cals.append(cal) # Generate test targets. targs = TargetArray(9) for row, col in np.ndindex(3, 3): targ_ix = row*3 + col targ = targs[targ_ix] pos3d = 10*np.array([[col, row, 0]], dtype=np.float64) pos2d = image_coordinates( pos3d, cal, cpar.get_multimedia_params()) targ.set_pos(convert_arr_metric_to_pixel(pos2d, cpar)[0]) targ.set_pnr(targ_ix) targ.set_pixel_counts(25, 5, 5) targ.set_sum_grey_value(10) img_pts.append(targs) mc = MatchedCoords(targs, cpar, cal) corrected.append(mc) _, _, num_targs = correspondences( img_pts, corrected, cals, vpar, cpar) self.failUnlessEqual(num_targs, 9)
def test_single_cam_corresp(self): """Single camera correspondence""" cpar = ControlParams(1) cpar.read_control_par("testing_fodder/single_cam/parameters/ptv.par") vpar = VolumeParams() vpar.read_volume_par( "testing_fodder/single_cam/parameters/criteria.par") # Cameras are at so high angles that opposing cameras don't see each # other in the normal air-glass-water setting. cpar.get_multimedia_params().set_layers([1.], [1.]) cpar.get_multimedia_params().set_n3(1.) cals = [] img_pts = [] corrected = [] cal = Calibration() cal.from_file( "testing_fodder/single_cam/calibration/cam_1.tif.ori", "testing_fodder/single_cam/calibration/cam_1.tif.addpar") cals.append(cal) # Generate test targets. targs = TargetArray(9) for row, col in np.ndindex(3, 3): targ_ix = row * 3 + col targ = targs[targ_ix] pos3d = 10 * np.array([[col, row, 0]], dtype=np.float64) pos2d = image_coordinates(pos3d, cal, cpar.get_multimedia_params()) targ.set_pos(convert_arr_metric_to_pixel(pos2d, cpar)[0]) targ.set_pnr(targ_ix) targ.set_pixel_counts(25, 5, 5) targ.set_sum_grey_value(10) img_pts.append(targs) mc = MatchedCoords(targs, cpar, cal) corrected.append(mc) sorted_pos, sorted_corresp, num_targs = correspondences( img_pts, corrected, cals, vpar, cpar) self.failUnlessEqual(num_targs, 9)
def test_single_camera_point_positions(self): """Point positions for a single camera case""" num_cams = 1 # prepare MultimediaParams cpar_file = b'testing_fodder/single_cam/parameters/ptv.par' vpar_file = b'testing_fodder/single_cam/parameters/criteria.par' cpar = ControlParams(num_cams) cpar.read_control_par(cpar_file) mult_params = cpar.get_multimedia_params() vpar = VolumeParams() vpar.read_volume_par(vpar_file) ori_name = b'testing_fodder/single_cam/calibration/cam_1.tif.ori' add_name = b'testing_fodder/single_cam/calibration/cam_1.tif.addpar' calibs = [] # read calibration for each camera from files new_cal = Calibration() new_cal.from_file(ori_file=ori_name, add_file=add_name) calibs.append(new_cal) # 3d point points = np.array([[1, 1, 0], [-1, -1, 0]], dtype=float) targs_plain = [] targs_jigged = [] jigg_amp = 0.4 new_plain_targ = image_coordinates( points, calibs[0], mult_params) targs_plain.append(new_plain_targ) jigged_points = points - np.r_[0, jigg_amp, 0] new_jigged_targs = image_coordinates( jigged_points, calibs[0], mult_params) targs_jigged.append(new_jigged_targs) targs_plain = np.array(targs_plain).transpose(1,0,2) targs_jigged = np.array(targs_jigged).transpose(1,0,2) skew_dist_plain = point_positions(targs_plain, cpar, calibs, vpar) skew_dist_jigged = point_positions(targs_jigged, cpar, calibs, vpar) if np.any(np.linalg.norm(points - skew_dist_plain[0], axis=1) > 1e-6): self.fail('Rays converge on wrong position.') if np.any(np.linalg.norm(jigged_points - skew_dist_jigged[0], axis=1) > 1e-6): self.fail('Rays converge on wrong position after jigging.')
def test_single_camera_point_positions(self): """Point positions for a single camera case""" num_cams = 1 # prepare MultimediaParams cpar_file = b'testing_fodder/single_cam/parameters/ptv.par' vpar_file = b'testing_fodder/single_cam/parameters/criteria.par' cpar = ControlParams(num_cams) cpar.read_control_par(cpar_file) mult_params = cpar.get_multimedia_params() vpar = VolumeParams() vpar.read_volume_par(vpar_file) ori_name = b'testing_fodder/single_cam/calibration/cam_1.tif.ori' add_name = b'testing_fodder/single_cam/calibration/cam_1.tif.addpar' calibs = [] # read calibration for each camera from files new_cal = Calibration() new_cal.from_file(ori_file=ori_name, add_file=add_name) calibs.append(new_cal) # 3d point points = np.array([[1, 1, 0], [-1, -1, 0]], dtype=float) targs_plain = [] targs_jigged = [] jigg_amp = 0.4 new_plain_targ = image_coordinates(points, calibs[0], mult_params) targs_plain.append(new_plain_targ) jigged_points = points - np.r_[0, jigg_amp, 0] new_jigged_targs = image_coordinates(jigged_points, calibs[0], mult_params) targs_jigged.append(new_jigged_targs) targs_plain = np.array(targs_plain).transpose(1, 0, 2) targs_jigged = np.array(targs_jigged).transpose(1, 0, 2) skew_dist_plain = point_positions(targs_plain, cpar, calibs, vpar) skew_dist_jigged = point_positions(targs_jigged, cpar, calibs, vpar) if np.any(np.linalg.norm(points - skew_dist_plain[0], axis=1) > 1e-6): self.fail('Rays converge on wrong position.') if np.any( np.linalg.norm(jigged_points - skew_dist_jigged[0], axis=1) > 1e-6): self.fail('Rays converge on wrong position after jigging.')
def test_instantiate_fast(self): """ControlParams instantiation through constructor""" cp = ControlParams(4, ['headers', 'hp', 'allcam'], (1280, 1024), (15.15,16.16), 18, [19.19], [21.21], 20.20) self.failUnless(cp.get_num_cams() == 4) self.failUnless(cp.get_hp_flag()) self.failUnless(cp.get_allCam_flag()) self.failUnless(cp.get_tiff_flag()) self.failUnless(cp.get_image_size(), (1280, 1024)) self.failUnless(cp.get_pixel_size() == (15.15,16.16)) self.failUnless(cp.get_chfield() == 0) mm = cp.get_multimedia_params() self.failUnless(mm.get_n1() == 18) self.failUnless(mm.get_n2()[0] == 19.19) self.failUnless(mm.get_n3() == 20.20) self.failUnless(mm.get_d()[0] == 21.21)
def test_instantiate_fast(self): """ControlParams instantiation through constructor""" cp = ControlParams(4, ['headers', 'hp', 'allcam'], (1280, 1024), (15.15,16.16), 18, [19.19], [21.21], 20.20) self.failUnless(cp.get_num_cams() == 4) self.failUnless(cp.get_hp_flag()) self.failUnless(cp.get_allCam_flag()) self.failUnless(cp.get_tiff_flag()) self.failUnless(cp.get_image_size(), (1280, 1024)) self.failUnless(cp.get_pixel_size() == (15.15,16.16)) self.failUnless(cp.get_chfield() == 0) mm = cp.get_multimedia_params() self.failUnless(mm.get_n1() == 18) self.failUnless(mm.get_n2()[0] == 19.19) self.failUnless(mm.get_n3() == 20.20) self.failUnless(mm.get_d()[0] == 21.21)
def test_two_cameras(self): ori_tmpl = "testing_fodder/calibration/sym_cam{cam_num}.tif.ori" add_file = "testing_fodder/calibration/cam1.tif.addpar" orig_cal = Calibration() orig_cal.from_file( ori_tmpl.format(cam_num=1).encode(), add_file.encode()) proj_cal = Calibration() proj_cal.from_file( ori_tmpl.format(cam_num=3).encode(), add_file.encode()) # reorient cams: orig_cal.set_angles(np.r_[0., -np.pi / 4., 0.]) proj_cal.set_angles(np.r_[0., 3 * np.pi / 4., 0.]) cpar = ControlParams(4) cpar.read_control_par(b"testing_fodder/corresp/control.par") sens_size = cpar.get_image_size() vpar = VolumeParams() vpar.read_volume_par(b"testing_fodder/corresp/criteria.par") vpar.set_Zmin_lay([-10, -10]) vpar.set_Zmax_lay([10, 10]) mult_params = cpar.get_multimedia_params() mult_params.set_n1(1.) mult_params.set_layers(np.array([1.]), np.array([1.])) mult_params.set_n3(1.) # Central point translates to central point because cameras point # directly at each other. mid = np.r_[sens_size] / 2. line = epipolar_curve(mid, orig_cal, proj_cal, 5, cpar, vpar) self.failUnless(np.all(abs(line - mid) < 1e-6)) # An equatorial point draws a latitude. line = epipolar_curve(mid - np.r_[100., 0.], orig_cal, proj_cal, 5, cpar, vpar) np.testing.assert_array_equal(np.argsort(line[:, 0]), np.arange(5)[::-1]) self.failUnless(np.all(abs(line[:, 1] - mid[1]) < 1e-6))
def test_two_cameras(self): ori_tmpl = "testing_fodder/calibration/sym_cam{cam_num}.tif.ori" add_file = "testing_fodder/calibration/cam1.tif.addpar" orig_cal = Calibration() orig_cal.from_file(ori_tmpl.format(cam_num=1).encode(), add_file.encode()) proj_cal = Calibration() proj_cal.from_file(ori_tmpl.format(cam_num=3).encode(), add_file.encode()) # reorient cams: orig_cal.set_angles(np.r_[0., -np.pi/4., 0.]) proj_cal.set_angles(np.r_[0., 3*np.pi/4., 0.]) cpar = ControlParams(4) cpar.read_control_par(b"testing_fodder/corresp/control.par") sens_size = cpar.get_image_size() vpar = VolumeParams() vpar.read_volume_par(b"testing_fodder/corresp/criteria.par") vpar.set_Zmin_lay([-10, -10]) vpar.set_Zmax_lay([10, 10]) mult_params = cpar.get_multimedia_params() mult_params.set_n1(1.) mult_params.set_layers(np.array([1.]), np.array([1.])) mult_params.set_n3(1.) # Central point translates to central point because cameras point # directly at each other. mid = np.r_[sens_size]/2. line = epipolar_curve(mid, orig_cal, proj_cal, 5, cpar, vpar) self.failUnless(np.all(abs(line - mid) < 1e-6)) # An equatorial point draws a latitude. line = epipolar_curve( mid - np.r_[100., 0.], orig_cal, proj_cal, 5, cpar, vpar) np.testing.assert_array_equal(np.argsort(line[:,0]), np.arange(5)[::-1]) self.failUnless(np.all(abs(line[:,1] - mid[1]) < 1e-6))
part_traject = np.zeros((num_frames, 3)) part_traject[:, 0] = np.r_[:num_frames] * velocity # Find targets on each camera. cpar = ControlParams(3) cpar.read_control_par("testing_fodder/track/parameters/control_newpart.par") targs = [] for cam in xrange(num_cams): cal = Calibration() cal.from_file("testing_fodder/cal/sym_cam%d.tif.ori" % (cam + 1), "testing_fodder/cal/cam1.tif.addpar") targs.append( convert_arr_metric_to_pixel( image_coordinates(part_traject, cal, cpar.get_multimedia_params()), cpar)) for frame in xrange(num_frames): # write 3D positions: with open("testing_fodder/track/res_orig/particles.%d" % (frame + 1), "w") as outfile: # Note correspondence to the single target in each frame. outfile.writelines([ str(1) + "\n", "{:5d}{:10.3f}{:10.3f}{:10.3f}{:5d}{:5d}{:5d}{:5d}\n".format( 1, part_traject[frame, 0], part_traject[frame, 1], part_traject[frame, 1], 0, 0, 0, 0) ]) # write associated targets from all cameras:
class Test_ControlParams(unittest.TestCase): def setUp(self): self.input_control_par_file_name = "testing_fodder/control_parameters/control.par" self.temp_output_directory = "testing_fodder/control_parameters/testing_output" # create a temporary output directory (will be deleted by the end of test) if not os.path.exists(self.temp_output_directory): os.makedirs(self.temp_output_directory) # create an instance of ControlParams class self.cp_obj = ControlParams(4) def test_read_control(self): # Fill the ControlParams object with parameters from test file self.cp_obj.read_control_par(self.input_control_par_file_name) # check if all parameters are equal to the contents of test file self.failUnless(self.cp_obj.get_img_base_name(0) == "dumbbell/cam1_Scene77_4085") self.failUnless(self.cp_obj.get_img_base_name(1) == "dumbbell/cam2_Scene77_4085") self.failUnless(self.cp_obj.get_img_base_name(2) == "dumbbell/cam3_Scene77_4085") self.failUnless(self.cp_obj.get_img_base_name(3) == "dumbbell/cam4_Scene77_4085") self.failUnless(self.cp_obj.get_cal_img_base_name(0) == "cal/cam1.tif") self.failUnless(self.cp_obj.get_cal_img_base_name(1) == "cal/cam2.tif") self.failUnless(self.cp_obj.get_cal_img_base_name(2) == "cal/cam3.tif") self.failUnless(self.cp_obj.get_cal_img_base_name(3) == "cal/cam4.tif") self.failUnless(self.cp_obj.get_num_cams() == 4) self.failUnless(self.cp_obj.get_hp_flag()) self.failUnless(self.cp_obj.get_allCam_flag()) self.failUnless(self.cp_obj.get_tiff_flag()) self.failUnless(self.cp_obj.get_image_size(), (1280, 1024)) self.failUnless(self.cp_obj.get_pixel_size() == (15.15,16.16)) self.failUnless(self.cp_obj.get_chfield() == 17) self.failUnless(self.cp_obj.get_multimedia_params().get_n1() == 18) self.failUnless(self.cp_obj.get_multimedia_params().get_n2()[0] == 19.19) self.failUnless(self.cp_obj.get_multimedia_params().get_n3() == 20.20) self.failUnless(self.cp_obj.get_multimedia_params().get_d()[0] == 21.21) def test_instantiate_fast(self): """ControlParams instantiation through constructor""" cp = ControlParams(4, ['headers', 'hp', 'allcam'], (1280, 1024), (15.15,16.16), 18, [19.19], [21.21], 20.20) self.failUnless(cp.get_num_cams() == 4) self.failUnless(cp.get_hp_flag()) self.failUnless(cp.get_allCam_flag()) self.failUnless(cp.get_tiff_flag()) self.failUnless(cp.get_image_size(), (1280, 1024)) self.failUnless(cp.get_pixel_size() == (15.15,16.16)) self.failUnless(cp.get_chfield() == 0) mm = cp.get_multimedia_params() self.failUnless(mm.get_n1() == 18) self.failUnless(mm.get_n2()[0] == 19.19) self.failUnless(mm.get_n3() == 20.20) self.failUnless(mm.get_d()[0] == 21.21) def test_getters_setters(self): cams_num = 4 for cam in range(cams_num): new_str = str(cam) + "some string" + str(cam) self.cp_obj.set_img_base_name(cam, new_str) self.failUnless(self.cp_obj.get_img_base_name(cam) == new_str) self.cp_obj.set_cal_img_base_name(cam, new_str) self.failUnless(self.cp_obj.get_cal_img_base_name(cam) == new_str) self.cp_obj.set_hp_flag(True) self.failUnless(self.cp_obj.get_hp_flag()) self.cp_obj.set_hp_flag(False) self.failUnless(not self.cp_obj.get_hp_flag()) self.cp_obj.set_allCam_flag(True) self.failUnless(self.cp_obj.get_allCam_flag()) self.cp_obj.set_allCam_flag(False) self.failUnless(not self.cp_obj.get_allCam_flag()) self.cp_obj.set_tiff_flag(True) self.failUnless(self.cp_obj.get_tiff_flag()) self.cp_obj.set_tiff_flag(False) self.failUnless(not self.cp_obj.get_tiff_flag()) self.cp_obj.set_image_size((4, 5)) self.failUnless(self.cp_obj.get_image_size()== (4, 5)) print self.cp_obj.get_pixel_size() self.cp_obj.set_pixel_size((6.1, 7.0)) numpy.testing.assert_array_equal(self.cp_obj.get_pixel_size(), (6.1, 7)) self.cp_obj.set_chfield(8) self.failUnless(self.cp_obj.get_chfield() == 8) # testing __richcmp__ comparison method of ControlParams class def test_rich_compare(self): self.cp_obj2 = ControlParams(4) self.cp_obj2.read_control_par(self.input_control_par_file_name) self.cp_obj3 = ControlParams(4) self.cp_obj3.read_control_par(self.input_control_par_file_name) self.failUnless(self.cp_obj2 == self.cp_obj3) self.failIf(self.cp_obj2 != self.cp_obj3) self.cp_obj2.set_hp_flag(False) self.failUnless(self.cp_obj2 != self.cp_obj3) self.failIf(self.cp_obj2 == self.cp_obj3) with self.assertRaises(TypeError): var = (self.cp_obj2 > self.cp_obj3) # unhandled operator > def tearDown(self): # remove the testing output directory and its files shutil.rmtree(self.temp_output_directory)
class TestGradientDescent(unittest.TestCase): # Based on the C tests in liboptv/tests/check_orientation.c def setUp(self): control_file_name = r'testing_fodder/corresp/control.par' self.control = ControlParams(4) self.control.read_control_par(control_file_name) self.cal = Calibration() self.cal.from_file("testing_fodder/calibration/cam1.tif.ori", "testing_fodder/calibration/cam1.tif.addpar") self.orig_cal = Calibration() self.orig_cal.from_file("testing_fodder/calibration/cam1.tif.ori", "testing_fodder/calibration/cam1.tif.addpar") def test_external_calibration(self): """External calibration using clicked points.""" ref_pts = np.array([[-40., -25., 8.], [40., -15., 0.], [40., 15., 0.], [40., 0., 8.]]) # Fake the image points by back-projection targets = convert_arr_metric_to_pixel( image_coordinates(ref_pts, self.cal, self.control.get_multimedia_params()), self.control) # Jigg the fake detections to give raw_orient some challenge. targets[:, 1] -= 0.1 self.assertTrue( external_calibration(self.cal, ref_pts, targets, self.control)) np.testing.assert_array_almost_equal(self.cal.get_angles(), self.orig_cal.get_angles(), decimal=4) np.testing.assert_array_almost_equal(self.cal.get_pos(), self.orig_cal.get_pos(), decimal=3) def test_full_calibration(self): ref_pts = np.array([ a.flatten() for a in np.meshgrid(np.r_[-60:-30:4j], np.r_[0:15:4j], np.r_[0:15:4j]) ]).T # Fake the image points by back-projection targets = convert_arr_metric_to_pixel( image_coordinates(ref_pts, self.cal, self.control.get_multimedia_params()), self.control) # Full calibration works with TargetArray objects, not NumPy. target_array = TargetArray(len(targets)) for i in xrange(len(targets)): target_array[i].set_pnr(i) target_array[i].set_pos(targets[i]) # Perturb the calibration object, then compore result to original. self.cal.set_pos(self.cal.get_pos() + np.r_[15., -15., 15.]) self.cal.set_angles(self.cal.get_angles() + np.r_[-.5, .5, -.5]) ret, used, err_est = full_calibration(self.cal, ref_pts, target_array, self.control) np.testing.assert_array_almost_equal(self.cal.get_angles(), self.orig_cal.get_angles(), decimal=4) np.testing.assert_array_almost_equal(self.cal.get_pos(), self.orig_cal.get_pos(), decimal=3)
class Test_Orientation(unittest.TestCase): def setUp(self): self.input_ori_file_name = r'testing_fodder/calibration/cam1.tif.ori' self.input_add_file_name = r'testing_fodder/calibration/cam2.tif.addpar' self.control_file_name = r'testing_fodder/control_parameters/control.par' self.calibration = Calibration() self.calibration.from_file(self.input_ori_file_name, self.input_add_file_name) self.control = ControlParams(4) self.control.read_control_par(self.control_file_name) def test_match_detection_to_ref(self): """Match detection to reference (sortgrid)""" xyz_input = np.array([(10, 10, 10), (200, 200, 200), (600, 800, 100), (20, 10, 2000), (30, 30, 30)], dtype=float) coords_count = len(xyz_input) xy_img_pts_metric = image_coordinates( xyz_input, self.calibration, self.control.get_multimedia_params()) xy_img_pts_pixel = convert_arr_metric_to_pixel(xy_img_pts_metric, control=self.control) # convert to TargetArray object target_array = TargetArray(coords_count) for i in range(coords_count): target_array[i].set_pnr(i) target_array[i].set_pos( (xy_img_pts_pixel[i][0], xy_img_pts_pixel[i][1])) # create randomized target array indices = range(coords_count) shuffled_indices = range(coords_count) while indices == shuffled_indices: random.shuffle(shuffled_indices) rand_targ_array = TargetArray(coords_count) for i in range(coords_count): rand_targ_array[shuffled_indices[i]].set_pos(target_array[i].pos()) rand_targ_array[shuffled_indices[i]].set_pnr(target_array[i].pnr()) # match detection to reference matched_target_array = match_detection_to_ref(cal=self.calibration, ref_pts=xyz_input, img_pts=rand_targ_array, cparam=self.control) # assert target array is as before for i in range(coords_count): if matched_target_array[i].pos() != target_array[i].pos() \ or matched_target_array[i].pnr() != target_array[i].pnr(): self.fail() # pass ref_pts and img_pts with non-equal lengths with self.assertRaises(ValueError): match_detection_to_ref(cal=self.calibration, ref_pts=xyz_input, img_pts=TargetArray(coords_count - 1), cparam=self.control) def test_point_positions(self): """Point positions""" # prepare MultimediaParams mult_params = self.control.get_multimedia_params() mult_params.set_n1(1.) mult_params.set_layers(np.array([1.]), np.array([1.])) mult_params.set_n3(1.) # 3d point points = np.array([[17, 42, 0], [17, 42, 0]], dtype=float) num_cams = 4 ori_tmpl = r'testing_fodder/calibration/sym_cam{cam_num}.tif.ori' add_file = r'testing_fodder/calibration/cam1.tif.addpar' calibs = [] targs_plain = [] targs_jigged = [] jigg_amp = 0.5 # read calibration for each camera from files for cam in range(num_cams): ori_name = ori_tmpl.format(cam_num=cam + 1) new_cal = Calibration() new_cal.from_file(ori_file=ori_name, add_file=add_file) calibs.append(new_cal) for cam_num, cam_cal in enumerate(calibs): new_plain_targ = image_coordinates( points, cam_cal, self.control.get_multimedia_params()) targs_plain.append(new_plain_targ) if (cam_num % 2) == 0: jigged_points = points - np.r_[0, jigg_amp, 0] else: jigged_points = points + np.r_[0, jigg_amp, 0] new_jigged_targs = image_coordinates( jigged_points, cam_cal, self.control.get_multimedia_params()) targs_jigged.append(new_jigged_targs) targs_plain = np.array(targs_plain).transpose(1, 0, 2) targs_jigged = np.array(targs_jigged).transpose(1, 0, 2) skew_dist_plain = point_positions(targs_plain, self.control, calibs) skew_dist_jigged = point_positions(targs_jigged, self.control, calibs) if np.any(skew_dist_plain[1] > 1e-10): self.fail(('skew distance of target#{targ_num} ' \ + 'is more than allowed').format( targ_num=np.nonzero(skew_dist_plain[1] > 1e-10)[0][0])) if np.any(np.linalg.norm(points - skew_dist_plain[0], axis=1) > 1e-6): self.fail('Rays converge on wrong position.') if np.any(skew_dist_jigged[1] > 0.7): self.fail(('skew distance of target#{targ_num} ' \ + 'is more than allowed').format( targ_num=np.nonzero(skew_dist_jigged[1] > 1e-10)[0][0])) if np.any(np.linalg.norm(points - skew_dist_jigged[0], axis=1) > 0.1): self.fail('Rays converge on wrong position after jigging.') def test_dumbbell(self): # prepare MultimediaParams mult_params = self.control.get_multimedia_params() mult_params.set_n1(1.) mult_params.set_layers(np.array([1.]), np.array([1.])) mult_params.set_n3(1.) # 3d point points = np.array([[17.5, 42, 0], [-17.5, 42, 0]], dtype=float) num_cams = 4 ori_tmpl = r'testing_fodder/dumbbell/cam{cam_num}.tif.ori' add_file = r'testing_fodder/calibration/cam1.tif.addpar' calibs = [] targs_plain = [] # read calibration for each camera from files for cam in range(num_cams): ori_name = ori_tmpl.format(cam_num=cam + 1) new_cal = Calibration() new_cal.from_file(ori_file=ori_name, add_file=add_file) calibs.append(new_cal) for cam_num, cam_cal in enumerate(calibs): new_plain_targ = flat_image_coordinates( points, cam_cal, self.control.get_multimedia_params()) targs_plain.append(new_plain_targ) targs_plain = np.array(targs_plain).transpose(1, 0, 2) # The cameras are not actually fully calibrated, so the result is not # an exact 0. The test is that changing the expected distance changes # the measure. tf = dumbbell_target_func(targs_plain, self.control, calibs, 35., 0.) self.assertAlmostEqual(tf, 7.14860, 5) # just a regression test # As we check the db length, the measure increases... tf_len = dumbbell_target_func(targs_plain, self.control, calibs, 35., 1.) self.assertTrue(tf_len > tf) # ...but not as much as when giving the wrong length. tf_too_long = dumbbell_target_func(targs_plain, self.control, calibs, 25., 1.) self.assertTrue(tf_too_long > tf_len > tf)
class Test_ControlParams(unittest.TestCase): def setUp(self): self.input_control_par_file_name = "testing_fodder/control_parameters/control.par" self.temp_output_directory = "testing_fodder/control_parameters/testing_output" # create a temporary output directory (will be deleted by the end of test) if not os.path.exists(self.temp_output_directory): os.makedirs(self.temp_output_directory) # create an instance of ControlParams class self.cp_obj = ControlParams(4) def test_read_control(self): # Fill the ControlParams object with parameters from test file self.cp_obj.read_control_par(self.input_control_par_file_name) # check if all parameters are equal to the contents of test file self.failUnless(self.cp_obj.get_img_base_name(0) == "dumbbell/cam1_Scene77_4085") self.failUnless(self.cp_obj.get_img_base_name(1) == "dumbbell/cam2_Scene77_4085") self.failUnless(self.cp_obj.get_img_base_name(2) == "dumbbell/cam3_Scene77_4085") self.failUnless(self.cp_obj.get_img_base_name(3) == "dumbbell/cam4_Scene77_4085") self.failUnless(self.cp_obj.get_cal_img_base_name(0) == "cal/cam1.tif") self.failUnless(self.cp_obj.get_cal_img_base_name(1) == "cal/cam2.tif") self.failUnless(self.cp_obj.get_cal_img_base_name(2) == "cal/cam3.tif") self.failUnless(self.cp_obj.get_cal_img_base_name(3) == "cal/cam4.tif") self.failUnless(self.cp_obj.get_num_cams() == 4) self.failUnless(self.cp_obj.get_hp_flag()) self.failUnless(self.cp_obj.get_allCam_flag()) self.failUnless(self.cp_obj.get_tiff_flag()) self.failUnless(self.cp_obj.get_image_size(), (1280, 1024)) self.failUnless(self.cp_obj.get_pixel_size() == (15.15,16.16)) self.failUnless(self.cp_obj.get_chfield() == 17) self.failUnless(self.cp_obj.get_multimedia_params().get_n1() == 18) self.failUnless(self.cp_obj.get_multimedia_params().get_n2()[0] == 19.19) self.failUnless(self.cp_obj.get_multimedia_params().get_n3() == 20.20) self.failUnless(self.cp_obj.get_multimedia_params().get_d()[0] == 21.21) def test_instantiate_fast(self): """ControlParams instantiation through constructor""" cp = ControlParams(4, ['headers', 'hp', 'allcam'], (1280, 1024), (15.15,16.16), 18, [19.19], [21.21], 20.20) self.failUnless(cp.get_num_cams() == 4) self.failUnless(cp.get_hp_flag()) self.failUnless(cp.get_allCam_flag()) self.failUnless(cp.get_tiff_flag()) self.failUnless(cp.get_image_size(), (1280, 1024)) self.failUnless(cp.get_pixel_size() == (15.15,16.16)) self.failUnless(cp.get_chfield() == 0) mm = cp.get_multimedia_params() self.failUnless(mm.get_n1() == 18) self.failUnless(mm.get_n2()[0] == 19.19) self.failUnless(mm.get_n3() == 20.20) self.failUnless(mm.get_d()[0] == 21.21) def test_getters_setters(self): cams_num = 4 for cam in range(cams_num): new_str = str(cam) + "some string" + str(cam) self.cp_obj.set_img_base_name(cam, new_str) self.failUnless(self.cp_obj.get_img_base_name(cam) == new_str) self.cp_obj.set_cal_img_base_name(cam, new_str) self.failUnless(self.cp_obj.get_cal_img_base_name(cam) == new_str) self.cp_obj.set_hp_flag(True) self.failUnless(self.cp_obj.get_hp_flag()) self.cp_obj.set_hp_flag(False) self.failUnless(not self.cp_obj.get_hp_flag()) self.cp_obj.set_allCam_flag(True) self.failUnless(self.cp_obj.get_allCam_flag()) self.cp_obj.set_allCam_flag(False) self.failUnless(not self.cp_obj.get_allCam_flag()) self.cp_obj.set_tiff_flag(True) self.failUnless(self.cp_obj.get_tiff_flag()) self.cp_obj.set_tiff_flag(False) self.failUnless(not self.cp_obj.get_tiff_flag()) self.cp_obj.set_image_size((4, 5)) self.failUnless(self.cp_obj.get_image_size()== (4, 5)) print self.cp_obj.get_pixel_size() self.cp_obj.set_pixel_size((6.1, 7.0)) numpy.testing.assert_array_equal(self.cp_obj.get_pixel_size(), (6.1, 7)) self.cp_obj.set_chfield(8) self.failUnless(self.cp_obj.get_chfield() == 8) # testing __richcmp__ comparison method of ControlParams class def test_rich_compare(self): self.cp_obj2 = ControlParams(4) self.cp_obj2.read_control_par(self.input_control_par_file_name) self.cp_obj3 = ControlParams(4) self.cp_obj3.read_control_par(self.input_control_par_file_name) self.failUnless(self.cp_obj2 == self.cp_obj3) self.failIf(self.cp_obj2 != self.cp_obj3) self.cp_obj2.set_hp_flag(False) self.failUnless(self.cp_obj2 != self.cp_obj3) self.failIf(self.cp_obj2 == self.cp_obj3) with self.assertRaises(TypeError): var = (self.cp_obj2 > self.cp_obj3) # unhandled operator > def tearDown(self): # remove the testing output directory and its files shutil.rmtree(self.temp_output_directory)
class TestGradientDescent(unittest.TestCase): # Based on the C tests in liboptv/tests/check_orientation.c def setUp(self): control_file_name = r'testing_fodder/corresp/control.par' self.control = ControlParams(4) self.control.read_control_par(control_file_name) self.cal = Calibration() self.cal.from_file( "testing_fodder/calibration/cam1.tif.ori", "testing_fodder/calibration/cam1.tif.addpar") self.orig_cal = Calibration() self.orig_cal.from_file( "testing_fodder/calibration/cam1.tif.ori", "testing_fodder/calibration/cam1.tif.addpar") def test_external_calibration(self): """External calibration using clicked points.""" ref_pts = np.array([ [-40., -25., 8.], [ 40., -15., 0.], [ 40., 15., 0.], [ 40., 0., 8.]]) # Fake the image points by back-projection targets = convert_arr_metric_to_pixel(image_coordinates( ref_pts, self.cal, self.control.get_multimedia_params()), self.control) # Jigg the fake detections to give raw_orient some challenge. targets[:,1] -= 0.1 self.assertTrue(external_calibration( self.cal, ref_pts, targets, self.control)) np.testing.assert_array_almost_equal( self.cal.get_angles(), self.orig_cal.get_angles(), decimal=4) np.testing.assert_array_almost_equal( self.cal.get_pos(), self.orig_cal.get_pos(), decimal=3) def test_full_calibration(self): ref_pts = np.array([a.flatten() for a in np.meshgrid( np.r_[-60:-30:4j], np.r_[0:15:4j], np.r_[0:15:4j])]).T # Fake the image points by back-projection targets = convert_arr_metric_to_pixel(image_coordinates( ref_pts, self.cal, self.control.get_multimedia_params()), self.control) # Full calibration works with TargetArray objects, not NumPy. target_array = TargetArray(len(targets)) for i in xrange(len(targets)): target_array[i].set_pnr(i) target_array[i].set_pos(targets[i]) # Perturb the calibration object, then compore result to original. self.cal.set_pos(self.cal.get_pos() + np.r_[15., -15., 15.]) self.cal.set_angles(self.cal.get_angles() + np.r_[-.5, .5, -.5]) ret, used, err_est = full_calibration( self.cal, ref_pts, target_array, self.control) np.testing.assert_array_almost_equal( self.cal.get_angles(), self.orig_cal.get_angles(), decimal=4) np.testing.assert_array_almost_equal( self.cal.get_pos(), self.orig_cal.get_pos(), decimal=3)
class Test_Orientation(unittest.TestCase): def setUp(self): self.input_ori_file_name = r'testing_fodder/calibration/cam1.tif.ori' self.input_add_file_name = r'testing_fodder/calibration/cam2.tif.addpar' self.control_file_name = r'testing_fodder/control_parameters/control.par' self.calibration = Calibration() self.calibration.from_file( self.input_ori_file_name, self.input_add_file_name) self.control = ControlParams(4) self.control.read_control_par(self.control_file_name) def test_match_detection_to_ref(self): """Match detection to reference (sortgrid)""" xyz_input = np.array([(10, 10, 10), (200, 200, 200), (600, 800, 100), (20, 10, 2000), (30, 30, 30)], dtype=float) coords_count = len(xyz_input) xy_img_pts_metric = image_coordinates( xyz_input, self.calibration, self.control.get_multimedia_params()) xy_img_pts_pixel = convert_arr_metric_to_pixel( xy_img_pts_metric, control=self.control) # convert to TargetArray object target_array = TargetArray(coords_count) for i in range(coords_count): target_array[i].set_pnr(i) target_array[i].set_pos( (xy_img_pts_pixel[i][0], xy_img_pts_pixel[i][1])) # create randomized target array indices = range(coords_count) shuffled_indices = range(coords_count) while indices == shuffled_indices: random.shuffle(shuffled_indices) rand_targ_array = TargetArray(coords_count) for i in range(coords_count): rand_targ_array[shuffled_indices[i]].set_pos(target_array[i].pos()) rand_targ_array[shuffled_indices[i]].set_pnr(target_array[i].pnr()) # match detection to reference matched_target_array = match_detection_to_ref(cal=self.calibration, ref_pts=xyz_input, img_pts=rand_targ_array, cparam=self.control) # assert target array is as before for i in range(coords_count): if matched_target_array[i].pos() != target_array[i].pos() \ or matched_target_array[i].pnr() != target_array[i].pnr(): self.fail() # pass ref_pts and img_pts with non-equal lengths with self.assertRaises(ValueError): match_detection_to_ref(cal=self.calibration, ref_pts=xyz_input, img_pts=TargetArray(coords_count - 1), cparam=self.control) def test_point_positions(self): """Point positions""" # prepare MultimediaParams mult_params = self.control.get_multimedia_params() mult_params.set_n1(1.) mult_params.set_layers(np.array([1.]), np.array([1.])) mult_params.set_n3(1.) # 3d point points = np.array([[17, 42, 0], [17, 42, 0]], dtype=float) num_cams = 4 ori_tmpl = r'testing_fodder/calibration/sym_cam{cam_num}.tif.ori' add_file = r'testing_fodder/calibration/cam1.tif.addpar' calibs = [] targs_plain = [] targs_jigged = [] jigg_amp = 0.5 # read calibration for each camera from files for cam in range(num_cams): ori_name = ori_tmpl.format(cam_num=cam + 1) new_cal = Calibration() new_cal.from_file(ori_file=ori_name, add_file=add_file) calibs.append(new_cal) for cam_num, cam_cal in enumerate(calibs): new_plain_targ = image_coordinates( points, cam_cal, self.control.get_multimedia_params()) targs_plain.append(new_plain_targ) if (cam_num % 2) == 0: jigged_points = points - np.r_[0, jigg_amp, 0] else: jigged_points = points + np.r_[0, jigg_amp, 0] new_jigged_targs = image_coordinates( jigged_points, cam_cal, self.control.get_multimedia_params()) targs_jigged.append(new_jigged_targs) targs_plain = np.array(targs_plain).transpose(1,0,2) targs_jigged = np.array(targs_jigged).transpose(1,0,2) skew_dist_plain = point_positions(targs_plain, self.control, calibs) skew_dist_jigged = point_positions(targs_jigged, self.control, calibs) if np.any(skew_dist_plain[1] > 1e-10): self.fail(('skew distance of target#{targ_num} ' \ + 'is more than allowed').format( targ_num=np.nonzero(skew_dist_plain[1] > 1e-10)[0][0])) if np.any(np.linalg.norm(points - skew_dist_plain[0], axis=1) > 1e-6): self.fail('Rays converge on wrong position.') if np.any(skew_dist_jigged[1] > 0.7): self.fail(('skew distance of target#{targ_num} ' \ + 'is more than allowed').format( targ_num=np.nonzero(skew_dist_jigged[1] > 1e-10)[0][0])) if np.any(np.linalg.norm(points - skew_dist_jigged[0], axis=1) > 0.1): self.fail('Rays converge on wrong position after jigging.') def test_dumbbell(self): # prepare MultimediaParams mult_params = self.control.get_multimedia_params() mult_params.set_n1(1.) mult_params.set_layers(np.array([1.]), np.array([1.])) mult_params.set_n3(1.) # 3d point points = np.array([[17.5, 42, 0], [-17.5, 42, 0]], dtype=float) num_cams = 4 ori_tmpl = r'testing_fodder/dumbbell/cam{cam_num}.tif.ori' add_file = r'testing_fodder/calibration/cam1.tif.addpar' calibs = [] targs_plain = [] # read calibration for each camera from files for cam in range(num_cams): ori_name = ori_tmpl.format(cam_num=cam + 1) new_cal = Calibration() new_cal.from_file(ori_file=ori_name, add_file=add_file) calibs.append(new_cal) for cam_num, cam_cal in enumerate(calibs): new_plain_targ = flat_image_coordinates( points, cam_cal, self.control.get_multimedia_params()) targs_plain.append(new_plain_targ) targs_plain = np.array(targs_plain).transpose(1,0,2) # The cameras are not actually fully calibrated, so the result is not # an exact 0. The test is that changing the expected distance changes # the measure. tf = dumbbell_target_func(targs_plain, self.control, calibs, 35., 0.) self.assertAlmostEqual(tf, 7.14860, 5) # just a regression test # As we check the db length, the measure increases... tf_len = dumbbell_target_func( targs_plain, self.control, calibs, 35., 1.) self.assertTrue(tf_len > tf) # ...but not as much as when giving the wrong length. tf_too_long = dumbbell_target_func( targs_plain, self.control, calibs, 25., 1.) self.assertTrue(tf_too_long > tf_len > tf)
class Test_image_coordinates(unittest.TestCase): def setUp(self): self.control = ControlParams(4) self.calibration = Calibration() def test_img_coord_typecheck(self): with self.assertRaises(TypeError): list = [[0 for x in range(3)] for x in range(10) ] # initialize a 10x3 list (but not numpy matrix) flat_image_coordinates(list, self.control, out=None) with self.assertRaises(TypeError): flat_image_coordinates(np.empty((10, 2)), self.calibration, self.control.get_multimedia_params(), output=None) with self.assertRaises(TypeError): image_coordinates(np.empty((10, 3)), self.calibration, self.control.get_multimedia_params(), output=np.zeros((10, 3))) with self.assertRaises(TypeError): image_coordinates(np.zeros((10, 2)), self.calibration, self.control.get_multimedia_params(), output=np.zeros((10, 2))) def test_image_coord_regress(self): self.calibration.set_pos(np.array([0, 0, 40])) self.calibration.set_angles(np.array([0, 0, 0])) self.calibration.set_primary_point(np.array([0, 0, 10])) self.calibration.set_glass_vec(np.array([0, 0, 20])) self.calibration.set_radial_distortion(np.array([0, 0, 0])) self.calibration.set_decentering(np.array([0, 0])) self.calibration.set_affine_trans(np.array([1, 0])) self.mult = MultimediaParams(n1=1, n2=np.array([1]), n3=1, d=np.array([1])) input = np.array([[10., 5., -20.], [10., 5., -20.]]) # vec3d output = np.zeros((2, 2)) x = 10. / 6. y = x / 2. correct_output = np.array([[x, y], [x, y]]) flat_image_coordinates(input=input, cal=self.calibration, mult_params=self.mult, output=output) np.testing.assert_array_equal(output, correct_output) output = np.full((2, 2), 999.) image_coordinates(input=input, cal=self.calibration, mult_params=self.mult, output=output) np.testing.assert_array_equal(output, correct_output)
part_traject = np.zeros((num_frames,3)) part_traject[:,0] = np.r_[:num_frames]*velocity # Find targets on each camera. cpar = ControlParams(3) cpar.read_control_par("testing_fodder/track/parameters/control_newpart.par") targs = [] for cam in xrange(num_cams): cal = Calibration() cal.from_file( "testing_fodder/cal/sym_cam%d.tif.ori" % (cam + 1), "testing_fodder/cal/cam1.tif.addpar") targs.append(convert_arr_metric_to_pixel(image_coordinates( part_traject, cal, cpar.get_multimedia_params()), cpar)) for frame in xrange(num_frames): # write 3D positions: with open("testing_fodder/track/res_orig/particles.%d" % (frame + 1), "w") as outfile: # Note correspondence to the single target in each frame. outfile.writelines([ str(1) + "\n", "{:5d}{:10.3f}{:10.3f}{:10.3f}{:5d}{:5d}{:5d}{:5d}\n".format( 1, part_traject[frame,0], part_traject[frame,1], part_traject[frame,1], 0, 0, 0, 0)]) # write associated targets from all cameras: for cam in xrange(num_cams): with open("testing_fodder/track/newpart/cam%d.%04d_targets" \ % (cam + 1, frame + 1), "w") as outfile: