Esempio n. 1
0
    def build_city_driveable_area_roi_index(
            self) -> Mapping[str, Mapping[str, np.ndarray]]:
        """
        Load driveable area files from disk. Dilate driveable area to get ROI (takes about 1/2 second).

        Returns:
            city_rasterized_da_dict: a dictionary of dictionaries. Key is city_name, and
                    value is a dictionary with driveable area info. For example, includes da_matrix: Numpy array of
                    shape (M,N) representing binary values for driveable area
                    city_to_pkl_image_se2: SE(2) that produces takes point in pkl image to city coordinates, e.g.
                    p_city = city_Transformation_pklimage * p_pklimage
        """
        city_rasterized_da_roi_dict: Dict[str, Dict[str, np.ndarray]] = {}
        for city_name, city_id in self.city_name_to_city_id_dict.items():
            city_id = self.city_name_to_city_id_dict[city_name]
            city_rasterized_da_roi_dict[city_name] = {}
            npy_fpath = MAP_FILES_ROOT / f"{city_name}_{city_id}_driveable_area_mat_2019_05_28.npy"
            city_rasterized_da_roi_dict[city_name]["da_mat"] = np.load(
                npy_fpath)

            se2_npy_fpath = MAP_FILES_ROOT / f"{city_name}_{city_id}_npyimage_to_city_se2_2019_05_28.npy"
            city_rasterized_da_roi_dict[city_name][
                "npyimage_to_city_se2"] = np.load(se2_npy_fpath)
            da_mat = copy.deepcopy(
                city_rasterized_da_roi_dict[city_name]["da_mat"])
            city_rasterized_da_roi_dict[city_name]["roi_mat"] = dilate_by_l2(
                da_mat, dilation_thresh=ROI_ISOCONTOUR)

        return city_rasterized_da_roi_dict
def test_dilate_by_l2_2x2square_1px() -> None:
    """Test dilate_by_l2() with a 1 pixel dilation.

    Dilate the following 8-bit image w/ 1 px border

    0 0 0 0 0 0    0 0 0 0 0 0
    0 0 0 0 0 0    0 0 x x 0 0
    0 0 x x 0 0 -> 0 x x x x 0
    0 0 x x 0 0    0 x x x x 0
    0 0 0 0 0 0    0 0 x x 0 0
    0 0 0 0 0 0    0 0 0 0 0 0
    """
    img = np.zeros((6, 6), dtype=np.uint8)
    img[2:4, 2:4] = 1
    dilated_img = dilate_by_l2(img, dilation_thresh=1.0)
    print(dilated_img)
    # ground truth
    dilated_img_gt = np.array(
        [
            [0, 0, 0, 0, 0, 0],
            [0, 0, 1, 1, 0, 0],
            [0, 1, 1, 1, 1, 0],
            [0, 1, 1, 1, 1, 0],
            [0, 0, 1, 1, 0, 0],
            [0, 0, 0, 0, 0, 0],
        ],
        dtype=np.uint8,
    )
    assert np.allclose(dilated_img, dilated_img_gt)
def test_dilate_by_l2_horizline_1point42px() -> None:
    """Test dilate_by_l2() with a non-integer dilation that should get cut off.

    Dilate the following 8-bit image w/ 1.42 px border,
    allowing us to access diagonal neighbors:

    0 0 0 0 0    0 0 0 0 0
    0 0 0 0 0    0 x x x x
    0 0 x x 0 -> 0 x x x x
    0 0 0 0 0    0 x x x x
    0 0 0 0 0    0 0 0 0 0
    """
    img = np.zeros((5, 5), dtype=np.uint8)
    img[2, 2:4] = 1
    dilated_img = dilate_by_l2(img, dilation_thresh=1.42)
    # ground truth
    dilated_img_gt = np.array(
        [
            [0, 0, 0, 0, 0],
            [0, 1, 1, 1, 1],
            [0, 1, 1, 1, 1],
            [0, 1, 1, 1, 1],
            [0, 0, 0, 0, 0],
        ],
        dtype=np.uint8,
    )
    assert np.allclose(dilated_img, dilated_img_gt)
def test_dilate_by_l2_no_change() -> None:
    """Test dilate_by_l2() and expect no change.

    Do not modify the following 8-bit image (0 px dilation)
    0 0 0 0 0
    0 x x x 0
    0 x x x 0
    0 x x x 0
    0 0 0 0 0
    """
    img = np.zeros((5, 5), dtype=np.uint8)
    img[1:4, 1:4] = 1
    dilated_img = dilate_by_l2(img.copy(), dilation_thresh=0.0)
    assert np.allclose(img, dilated_img)