def test_transforms_regress(self): """Transformed values are as before.""" input = np.full((3, 2), 100.) output = np.zeros((3, 2)) correct_output_pixel_to_metric = [[-8181., 6657.92], [-8181., 6657.92], [-8181., 6657.92]] correct_output_metric_to_pixel = [[646.60066007, 505.81188119], [646.60066007, 505.81188119], [646.60066007, 505.81188119]] # Test when passing an array for output convert_arr_pixel_to_metric(input, self.control, out=output) np.testing.assert_array_almost_equal(output, correct_output_pixel_to_metric, decimal=7) output = np.zeros((3, 2)) convert_arr_metric_to_pixel(input, self.control, out=output) np.testing.assert_array_almost_equal(output, correct_output_metric_to_pixel, decimal=7) # Test when NOT passing an array for output output = convert_arr_pixel_to_metric(input, self.control, out=None) np.testing.assert_array_almost_equal(output, correct_output_pixel_to_metric, decimal=7) output = np.zeros((3, 2)) output = convert_arr_metric_to_pixel(input, self.control, out=None) np.testing.assert_array_almost_equal(output, correct_output_metric_to_pixel, decimal=7)
def test_transforms_typecheck(self): """Transform bindings check types""" # Assert TypeError is raised when passing a non (n,2) shaped numpy ndarray with self.assertRaises(TypeError): list = [[0 for x in range(2)] for x in range(10)] # initialize a 10x2 list (but not numpy matrix) convert_arr_pixel_to_metric(list, self.control, out=None) with self.assertRaises(TypeError): convert_arr_pixel_to_metric(np.empty((10, 3)), self.control, out=None) with self.assertRaises(TypeError): convert_arr_metric_to_pixel(np.empty((2, 1)), self.control, out=None) with self.assertRaises(TypeError): convert_arr_metric_to_pixel(np.zeros((11, 2)), self.control, out=np.zeros((12, 2)))
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)
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)
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_transforms(self): """Transform in well-known setup gives precalculates results.""" cpar = ControlParams(1) cpar.set_image_size((1280, 1000)) cpar.set_pixel_size((0.1, 0.1)) metric_pos = np.array([[1., 1.], [-10., 15.], [20., -30.]]) pixel_pos = np.array([[650., 490.], [540., 350.], [840., 800.]]) np.testing.assert_array_almost_equal( pixel_pos, convert_arr_metric_to_pixel(metric_pos, cpar)) np.testing.assert_array_almost_equal( metric_pos, convert_arr_pixel_to_metric(pixel_pos, cpar))
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 _project_cal_points(self, i_cam, color="yellow"): x, y = [], [] for row in self.cal_points: projected = image_coordinates(np.atleast_2d(row['pos']), \ self.cals[i_cam], self.cpar.get_multimedia_params()) pos = convert_arr_metric_to_pixel(projected, self.cpar) x.append(pos[0][0]) y.append(pos[0][1]) # x.append(x1) # y.append(y1) self.drawcross("init_x", "init_y", x, y, color, 3, i_cam=i_cam) self.status_text = 'Initial guess finished.'
def test_transforms_regress(self): """Transformed values are as before.""" input = np.full((3, 2), 100.) output = np.zeros((3, 2)) correct_output_pixel_to_metric = [[-8181. , 6657.92], [-8181. , 6657.92], [-8181. , 6657.92]] correct_output_metric_to_pixel= [[ 646.60066007, 505.81188119], [ 646.60066007, 505.81188119], [ 646.60066007, 505.81188119]] # Test when passing an array for output convert_arr_pixel_to_metric(input, self.control, out=output) np.testing.assert_array_almost_equal(output, correct_output_pixel_to_metric,decimal=7) output = np.zeros((3, 2)) convert_arr_metric_to_pixel(input, self.control, out=output) np.testing.assert_array_almost_equal(output, correct_output_metric_to_pixel, decimal=7) # Test when NOT passing an array for output output=convert_arr_pixel_to_metric(input, self.control, out=None) np.testing.assert_array_almost_equal(output, correct_output_pixel_to_metric,decimal=7) output = np.zeros((3, 2)) output=convert_arr_metric_to_pixel(input, self.control, out=None) np.testing.assert_array_almost_equal(output, correct_output_metric_to_pixel, decimal=7)
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_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_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_transforms(self): """Transform in well-known setup gives precalculates results.""" cpar = ControlParams(1) cpar.set_image_size((1280, 1000)) cpar.set_pixel_size((0.1, 0.1)) metric_pos = np.array([ [1., 1.], [-10., 15.], [20., -30.] ]) pixel_pos = np.array([ [650., 490.], [540., 350.], [840., 800.] ]) np.testing.assert_array_almost_equal(pixel_pos, convert_arr_metric_to_pixel(metric_pos, cpar)) np.testing.assert_array_almost_equal(metric_pos, convert_arr_pixel_to_metric(pixel_pos, cpar))
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_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)
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:
velocity = 0.01 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: