Ejemplo n.º 1
0
def approximate_environment(environment_points,
                            step=0.1,
                            min_number_of_approximated_points=30):
    temp = PointsObject()
    temp.add_points(environment_points)
    normals = temp.get_normals()
    found_shapes = shape_recognition.RANSAC(
        environment_points,
        normals,
        number_of_points_threshold=environment_points.shape[0] * 0.1,
        number_of_iterations=10,
        min_pc_number=environment_points.shape[0] * 0.3,
        number_of_subsets=10,
        use_planes=True,
        use_box=False,
        use_sphere=False,
        use_cylinder=True,
        use_cone=False)
    points = np.empty((0, 3))
    normals = np.empty((0, 3))
    for s in found_shapes:
        s = np.round(s / step) * step
        temp = PointsObject()
        temp.add_points(s)
        points = np.append(points, s, axis=0)
        normals = np.append(normals, temp.get_normals(), axis=0)
    return points, normals
Ejemplo n.º 2
0
    def objects_test_moving_figures_global():
        classes = {}

        rgb_im = image_processing.load_image("falling balls and cylinder",
                                             "rgb_" + str(0) + ".png")
        depth_im = image_processing.load_image("falling balls and cylinder",
                                               "depth_" + str(0) + ".png",
                                               "depth")
        mog = RGBD_MoG(rgb_im, depth_im, number_of_gaussians=3)

        for number_of_frame in range(1, 5):

            color_mask = np.zeros([rgb_im.shape[0], rgb_im.shape[1], 3])

            rgb_im = image_processing.load_image(
                "falling balls and cylinder",
                "rgb_" + str(number_of_frame) + ".png")
            depth_im = image_processing.load_image(
                "falling balls and cylinder",
                "depth_" + str(number_of_frame) + ".png", "depth")
            mask = mog.set_mask(rgb_im, depth_im)

            depth_im = depth_im * (mask / 255).astype(int)
            masks = region_growing(mask / 255,
                                   depth_im / 255,
                                   depth_threshold=0.01,
                                   significant_number_of_points=10)
            if len(masks) == 0:
                print("No moving objects in the frame")
            else:
                for mask in masks:
                    xyz_points, rgb_points = image_processing.calculate_point_cloud(
                        rgb_im / 255, depth_im * mask / 255)
                    current_object = PointsObject()
                    current_object.set_points(xyz_points, rgb_points)
                    norms = current_object.get_normals()
                    compared_object_descriptor = GlobalCovarianceDescriptor(
                        xyz_points,
                        rgb_points,
                        norms,
                        depth_im,
                        rgb_im,
                        mask,
                        use_xyz=True,
                        use_rgb=True,
                        use_normals=True)
                    match_found = False
                    lengths = np.zeros([len(classes)])

                    if number_of_frame == 1:
                        match_found = False
                    else:
                        match_found = True
                        for object_number, object_class in enumerate(classes):
                            lengths[
                                object_number] = object_class.compare_descriptors(
                                    compared_object_descriptor.
                                    object_descriptor)
                        min_arg = np.argmin(lengths)
                        print(lengths)
                        for object_number, object_class in enumerate(classes):
                            if object_number == min_arg:
                                color_mask[:, :,
                                           0] += mask * classes[object_class][0]
                                color_mask[:, :,
                                           1] += mask * classes[object_class][1]
                                color_mask[:, :,
                                           2] += mask * classes[object_class][2]

                    if not match_found:
                        classes[compared_object_descriptor] = np.random.rand(3)
                        color_mask[:, :, 0] += mask * classes[
                            compared_object_descriptor][0]
                        color_mask[:, :, 1] += mask * classes[
                            compared_object_descriptor][1]
                        color_mask[:, :, 2] += mask * classes[
                            compared_object_descriptor][2]
                image_processing.save_image(color_mask,
                                            "tracking_results",
                                            frame_number=number_of_frame,
                                            image_name="global_two_same")
Ejemplo n.º 3
0
    def objects_test_moving_figures_local():
        number_of_comparing_points = 100
        classes = {}

        rgb_im = image_processing.load_image("falling balls and cylinder",
                                             "rgb_" + str(0) + ".png")
        depth_im = image_processing.load_image("falling balls and cylinder",
                                               "depth_" + str(0) + ".png",
                                               "depth")
        mog = RGBD_MoG(rgb_im, depth_im, number_of_gaussians=3)

        for number_of_frame in range(1, 5):

            color_mask = np.zeros([rgb_im.shape[0], rgb_im.shape[1], 3])

            rgb_im = image_processing.load_image(
                "falling balls and cylinder",
                "rgb_" + str(number_of_frame) + ".png")
            depth_im = image_processing.load_image(
                "falling balls and cylinder",
                "depth_" + str(number_of_frame) + ".png", "depth")
            mask = mog.set_mask(rgb_im, depth_im)

            depth_im = depth_im * (mask / 255).astype(int)
            masks = region_growing(mask / 255,
                                   depth_im / 255,
                                   depth_threshold=0.01,
                                   significant_number_of_points=10)
            if len(masks) == 0:
                print("No moving objects in the frame")
            else:
                for mask in masks:
                    xyz_points, rgb_points = image_processing.calculate_point_cloud(
                        rgb_im / 255, depth_im * mask / 255)
                    current_object = PointsObject()
                    current_object.set_points(xyz_points, rgb_points)
                    norms = current_object.get_normals()
                    compared_object_descriptor = CovarianceDescriptor(
                        xyz_points,
                        rgb_points,
                        norms,
                        k_nearest_neighbours=None,
                        relevant_distance=0.1,
                        use_alpha=True,
                        use_beta=True,
                        use_ro=True,
                        use_theta=True,
                        use_psi=True,
                        use_rgb=True)
                    match_found = False
                    lengths = np.zeros([
                        len(classes),
                        np.amin(
                            [number_of_comparing_points, xyz_points.shape[0]])
                    ])

                    if number_of_frame == 1:
                        match_found = False
                    else:
                        match_found = True
                        for object_number, object_class in enumerate(classes):
                            lengths[
                                object_number] = object_class.compare_descriptors(
                                    compared_object_descriptor.
                                    object_descriptor,
                                    number_of_comparing_points)
                            print(np.sum(mask))
                        min_args = np.argmin(
                            lengths, axis=0)[np.amin(lengths, axis=0) < 0.1]
                        unique, counts = np.unique(min_args,
                                                   return_counts=True)
                        best_match = unique[np.argmax(counts)]
                        for object_number, object_class in enumerate(classes):
                            if object_number == best_match:
                                color_mask[:, :,
                                           0] += mask * classes[object_class][0]
                                color_mask[:, :,
                                           1] += mask * classes[object_class][1]
                                color_mask[:, :,
                                           2] += mask * classes[object_class][2]

                    if not match_found:
                        classes[compared_object_descriptor] = np.random.rand(3)
                        color_mask[:, :, 0] += mask * classes[
                            compared_object_descriptor][0]
                        color_mask[:, :, 1] += mask * classes[
                            compared_object_descriptor][1]
                        color_mask[:, :, 2] += mask * classes[
                            compared_object_descriptor][2]
                image_processing.save_image(color_mask,
                                            "tracking_results",
                                            frame_number=number_of_frame,
                                            image_name="local_two_same")