def test_array_camera_frame_local_point(): dir_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../data/plant_1") side_calibration = phm_data.calibrations(dir_path)["side"] camera_frame = side_calibration.get_camera_frame() pt_3d = (-322.20389648, 162.67521638, -4866.89129462) result = camera_frame.local_point(pt_3d) result = numpy.round(result.astype(float), 5) assert tuple(result) == (95.38307, -4909.59993, -5763.88954) pts_3d = [[-322.20389648, 162.67521638, -4866.89129462], [-322.20389648, 162.67521638, -4866.89129462], [-322.20389648, 162.67521638, -4866.89129462], [-322.20389648, 162.67521638, -4866.89129462]] result = camera_frame.arr_local_point(pts_3d) result = numpy.round(result.astype(float), 5) for pt_3d in result: assert tuple(pt_3d) == (95.38307, -4909.59993, -5763.88954) pts_3d = [[-322.20389648, 162.67521638, -4866.89129462]] result = camera_frame.arr_local_point(pts_3d) result = numpy.round(result.astype(float), 5) for pt_3d in result: assert tuple(pt_3d) == (95.38307, -4909.59993, -5763.88954)
def calibrations(chessboards, size_image=(2056, 2454), number_of_repetition=1): id_cameras = ["side", "top"] calibrations = dict() for id_camera in id_cameras: calibration = None if len(chessboards) == 1: calibration = phm_calib.CalibrationCameraSideWith1Target() err = calibration.calibrate( chessboards[0].get_corners_2d(id_camera), chessboards[0].get_corners_local_3d(), size_image, number_of_repetition=number_of_repetition, # repetion here is 0 to optimize time consuming (for better result put 4) verbose=False) if len(chessboards) == 2: calibration = phm_calib.CalibrationCameraSideWith2TargetYXZ() err = calibration.calibrate( chessboards[0].get_corners_2d(id_camera), chessboards[0].get_corners_local_3d(), chessboards[1].get_corners_2d(id_camera), chessboards[1].get_corners_local_3d(), size_image, number_of_repetition=number_of_repetition, # repetion here is 0 to optimize time consuming (for better result put 4) verbose=False) calibrations[id_camera] = calibration # Error of reprojection (in pixel distance) for all point in the target (48) # So real error is err / 48 return phm_data.calibrations(6)
def test_split_and_projection(): plant_number = 1 angle = 0 calibrations = phm_data.calibrations(plant_number=plant_number) projection = calibrations["side"].get_projection(angle) voxels_position = numpy.array([[0, 0, 0]]) voxels_size = 64 for i in range(5): res = phm_mvr.get_bounding_box_voxel_projected(voxels_position, voxels_size, projection) res = numpy.floor(res).astype(int) img = numpy.zeros((3000, 3000)) for x_min, y_min, x_max, y_max in res: img[y_min:y_max + 1, x_min:x_max + 1] = 255 assert 3864 == numpy.count_nonzero(img) img = phm_mvr.project_voxel_centers_on_image(voxels_position, voxels_size, (3000, 3000), projection) assert 3864 == numpy.count_nonzero(img) voxels = phm_mvr.split_voxels_in_eight( phm_mvr.Voxels(voxels_position, voxels_size)) voxels_position = voxels.position voxels_size = voxels.size
def test_array_camera_frame_local_point(): plant_number = 1 side_calibration = phm_data.calibrations(plant_number=plant_number)["side"] camera_frame = side_calibration.get_camera_frame() pt_3d = (-322.20389648, 162.67521638, -4866.89129462) result = camera_frame.local_point(pt_3d) result = numpy.round(result.astype(float), 5) assert tuple(result) == (95.38307, -4909.59993, -5763.88954) pts_3d = [[-322.20389648, 162.67521638, -4866.89129462], [-322.20389648, 162.67521638, -4866.89129462], [-322.20389648, 162.67521638, -4866.89129462], [-322.20389648, 162.67521638, -4866.89129462]] result = camera_frame.arr_local_point(pts_3d) result = numpy.round(result.astype(float), 5) for pt_3d in result: assert tuple(pt_3d) == (95.38307, -4909.59993, -5763.88954) pts_3d = [[-322.20389648, 162.67521638, -4866.89129462]] result = camera_frame.arr_local_point(pts_3d) result = numpy.round(result.astype(float), 5) for pt_3d in result: assert tuple(pt_3d) == (95.38307, -4909.59993, -5763.88954)
def test_frame(): dir_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../data/plant_1") c = phm_data.calibrations(dir_path)["side"] print(c._cam_pos_x, c._cam_pos_y, c._cam_pos_z) print(c._cam_rot_x, c._cam_rot_y, c._cam_rot_z) fr_cam = c.camera_frame(c._cam_pos_x, c._cam_pos_y, c._cam_pos_z, c._cam_rot_x, c._cam_rot_y, c._cam_rot_z, c._cam_origin_axis) # print fr_cam.global_point((0, 0, 0)) # print fr_cam.global_point((-50, 0, -2500)) print(fr_cam.global_point((-100, 0, -5000))) print(fr_cam.local_point((0, 0, 0))) print(c._cam_focal_length_x, c._cam_focal_length_y) print(c._cam_width_image / 2.0, c._cam_height_image / 2.0) pt2d = c.pixel_coordinates(fr_cam.local_point( (0, 0, 0)), c._cam_width_image, c._cam_height_image, c._cam_focal_length_x, c._cam_focal_length_y) print(pt2d)
def test_id_recons3d(): with open(cache_test+'binImage') as f: images_bin = dill.load(f) with open(cache_test+'binImage') as f: images_bin2 = dill.load(f) calibration = calibrations(plant_number=1) image_views = get_image_views(images_bin, calibration) image_views2 = get_image_views(images_bin2, calibration) print try_id(reconstruction_3d, image_views, start_voxel_size=256) == try_id(reconstruction_3d, image_views2, start_voxel_size=256)
def test_projection(): angle = 0 plant_number = 1 side_calibration = phm_data.calibrations(plant_number=plant_number)["side"] projection = side_calibration.get_projection(angle) pts_3d = numpy.array([[-472, -472, 200], [-472, -472, 200], [-472, -472, 200], [-472, -472, 200]]) result = projection(pts_3d) for pt_2d in result: assert tuple(pt_2d) == (1337.425449561377, 1070.8621710384346)
def test_projection(): angle = 0 dir_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../data/plant_1") side_calibration = phm_data.calibrations(dir_path)["side"] projection = side_calibration.get_projection(angle) pts_3d = numpy.array([[-472, -472, 200], [-472, -472, 200], [-472, -472, 200], [-472, -472, 200]]) result = projection(pts_3d) for pt_2d in result: assert tuple(pt_2d) == (1337.425449561377, 1070.8621710384346)
def test_get_bounding_box_voxel_projected_2(): plant_number = 1 angle = 0 calibrations = phm_data.calibrations(plant_number=plant_number) projection = calibrations["side"].get_projection(angle) voxels_position = numpy.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]]) voxels_size = 8 res = phm_mvr.get_bounding_box_voxel_projected(voxels_position, voxels_size, projection) ref = numpy.array([[1017.309, 1258.280, 1025.788, 1265.171], [1017.309, 1258.280, 1025.788, 1265.171], [1017.309, 1258.280, 1025.788, 1265.171]]) assert numpy.allclose(ref, res)
def test_running(): plant_number = 1 voxels_size = 16 bin_images = phm_data.bin_images(plant_number=plant_number) calibrations = phm_data.calibrations(plant_number=plant_number) voxel_grid = phm_data.voxel_grid(plant_number=plant_number, voxels_size=voxels_size) graph = phm_seg.graph_from_voxel_grid(voxel_grid) voxel_skeleton = phm_seg.skeletonize(voxel_grid, graph) image_projection = list() for angle in [0, 120, 270]: projection = calibrations["side"].get_projection(angle) image_projection.append((bin_images["side"][angle], projection)) voxel_skeleton_reduced = phm_seg.segment_reduction(voxel_skeleton, image_projection, required_visible=1, nb_min_pixel=100)
def test_running(): bin_images = phm_data.bin_images(plant_1_dir) calibrations = phm_data.calibrations(plant_1_dir) voxel_grid = phm_data.voxel_grid(data_dir, 1, 32) # Load images binarize graph = phm_seg.graph_from_voxel_grid(voxel_grid) voxel_skeleton = phm_seg.skeletonize(voxel_grid, graph) image_projection = list() for angle in [0, 120, 270]: projection = calibrations["side"].get_projection(angle) image_projection.append((bin_images["side"][angle], projection)) voxel_skeleton = phm_seg.segment_reduction( voxel_skeleton, image_projection, required_visible=1, nb_min_pixel=100)
def get_image_views_cube_projected(with_ref=False): plant_number = 1 # ========================================================================== # Create object voxels_size = 10 voxels_position = phm_data.build_cube(cube_size=10, voxels_size=voxels_size, voxels_position=(0, 0, 0)) assert len(voxels_position) == 1000 volume = len(voxels_position) * (10**3) assert volume == 1000000 # ========================================================================== calibrations = phm_data.calibrations(plant_number=plant_number) shape_image = (2454, 2056) image_views = list() for angle in range(0, 360, 30): projection = calibrations["side"].get_projection(angle) img = phm_mvr.project_voxel_centers_on_image(voxels_position, voxels_size, shape_image, projection) image_ref = None if with_ref: if angle == 0: img[:] = 0 if angle == 90: image_ref = img iv = phm_obj.ImageView(img, projection, inclusive=False, image_ref=image_ref) image_views.append(iv) return image_views
def test_array_pixel_coordinates(): dir_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../data/plant_1") side_calibration = phm_data.calibrations(dir_path)["side"] pt3d = (-322.20389648, 162.67521638, -4866.89129462) pt_2d = side_calibration.pixel_coordinates( pt3d, side_calibration._cam_width_image, side_calibration._cam_height_image, side_calibration._cam_focal_length_x, side_calibration._cam_focal_length_y) assert pt_2d == (1337.425449561858, 1070.86217103428) pts_3d = numpy.array([[-322.20389648, 162.67521638, -4866.89129462], [-322.20389648, 162.67521638, -4866.89129462], [-322.20389648, 162.67521638, -4866.89129462], [-322.20389648, 162.67521638, -4866.89129462]]) pts_2d = side_calibration.arr_pixel_coordinates( pts_3d, side_calibration._cam_width_image, side_calibration._cam_height_image, side_calibration._cam_focal_length_x, side_calibration._cam_focal_length_y) for pt_2d in pts_2d: assert tuple(pt_2d) == (1337.425449561858, 1070.86217103428) pts_3d = numpy.array([[-322.20389648, 162.67521638, -4866.89129462]]) pts_2d = side_calibration.arr_pixel_coordinates( pts_3d, side_calibration._cam_width_image, side_calibration._cam_height_image, side_calibration._cam_focal_length_x, side_calibration._cam_focal_length_y) for pt_2d in pts_2d: assert tuple(pt_2d) == (1337.425449561858, 1070.86217103428)
def test_frame(): plant_number = 1 c = phm_data.calibrations(plant_number=plant_number)["side"] print(c._cam_pos_x, c._cam_pos_y, c._cam_pos_z) print(c._cam_rot_x, c._cam_rot_y, c._cam_rot_z) fr_cam = c.camera_frame(c._cam_pos_x, c._cam_pos_y, c._cam_pos_z, c._cam_rot_x, c._cam_rot_y, c._cam_rot_z, c._cam_origin_axis) # print fr_cam.global_point((0, 0, 0)) # print fr_cam.global_point((-50, 0, -2500)) print(fr_cam.global_point((-100, 0, -5000))) print(fr_cam.local_point((0, 0, 0))) print(c._cam_focal_length_x, c._cam_focal_length_y) print(c._cam_width_image / 2.0, c._cam_height_image / 2.0) pt2d = c.pixel_coordinates(fr_cam.local_point( (0, 0, 0)), c._cam_width_image, c._cam_height_image, c._cam_focal_length_x, c._cam_focal_length_y) print(pt2d)
def test_reconstruction_3d_1(): # Load images binarize bin_images = phm_data.bin_images(plant_1_dir) calibrations = phm_data.calibrations(plant_1_dir) image_views = list() for id_camera in bin_images: for angle in bin_images[id_camera]: projection = calibrations[id_camera].get_projection(angle) iv = phm_obj.ImageView(bin_images[id_camera][angle], projection, inclusive=False, image_ref=None) image_views.append(iv) voxels_size = 64 error_tolerance = 0 vg = phm_mvr.reconstruction_3d(image_views, voxels_size=voxels_size, error_tolerance=error_tolerance) assert len(vg.voxels_position) > 0
def test_array_pixel_coordinates(): plant_number = 1 side_calibration = phm_data.calibrations(plant_number=plant_number)["side"] pt3d = (-322.20389648, 162.67521638, -4866.89129462) pt_2d = side_calibration.pixel_coordinates( pt3d, side_calibration._cam_width_image, side_calibration._cam_height_image, side_calibration._cam_focal_length_x, side_calibration._cam_focal_length_y) assert pt_2d == (1337.425449561858, 1070.86217103428) pts_3d = numpy.array([[-322.20389648, 162.67521638, -4866.89129462], [-322.20389648, 162.67521638, -4866.89129462], [-322.20389648, 162.67521638, -4866.89129462], [-322.20389648, 162.67521638, -4866.89129462]]) pts_2d = side_calibration.arr_pixel_coordinates( pts_3d, side_calibration._cam_width_image, side_calibration._cam_height_image, side_calibration._cam_focal_length_x, side_calibration._cam_focal_length_y) for pt_2d in pts_2d: assert tuple(pt_2d) == (1337.425449561858, 1070.86217103428) pts_3d = numpy.array([[-322.20389648, 162.67521638, -4866.89129462]]) pts_2d = side_calibration.arr_pixel_coordinates( pts_3d, side_calibration._cam_width_image, side_calibration._cam_height_image, side_calibration._cam_focal_length_x, side_calibration._cam_focal_length_y) for pt_2d in pts_2d: assert tuple(pt_2d) == (1337.425449561858, 1070.86217103428)
#!/usr/bin/env python import sys import pickle import openalea.phenomenal.data as phm_data import json import pandas output_files = { 'bin_images': '/home/ubuntu/multiviewReconstruction/bin_images.pkl', 'calibrations': '/home/ubuntu/multiviewReconstruction/calibrations.pkl' } plant_number = 2 # Available : 1, 2, 3, 4 or 5 bin_images = phm_data.bin_images(plant_number=plant_number) calibrations = phm_data.calibrations(plant_number=plant_number) with open(output_files['bin_images'], 'w') as fd: pickle.dump(bin_images, fd) with open(output_files['calibrations'], 'w') as fd: pickle.dump(calibrations, fd) print('{}\n{}\n{}'.format(plant_number, output_files['bin_images'], output_files['calibrations']))