pandora.img_tools ================= .. py:module:: pandora.img_tools .. autoapi-nested-parse:: This module contains functions associated to raster images. Attributes ---------- .. autoapisummary:: pandora.img_tools.interpolate_nodata_sgm pandora.img_tools.find_valid_neighbors Functions --------- .. autoapisummary:: pandora.img_tools.rasterio_open pandora.img_tools.get_window pandora.img_tools.add_disparity pandora.img_tools.add_disparity_grid pandora.img_tools.add_classif pandora.img_tools.add_segm pandora.img_tools.add_no_data pandora.img_tools.add_mask pandora.img_tools.add_global_disparity pandora.img_tools.create_dataset_from_inputs pandora.img_tools.get_metadata pandora.img_tools.get_pyramids pandora.img_tools.prepare_pyramid pandora.img_tools.fill_nodata_image pandora.img_tools.masks_pyramid pandora.img_tools.convert_pyramid_to_dataset pandora.img_tools.shift_right_img pandora.img_tools.check_inside_image pandora.img_tools.census_transform pandora.img_tools.compute_mean_raster pandora.img_tools.compute_mean_patch pandora.img_tools.compute_std_raster pandora.img_tools.read_disp pandora.img_tools.fuse_classification_bands Module Contents --------------- .. py:function:: rasterio_open(*args: str, **kwargs: Union[int, str, None]) -> rasterio.io.DatasetReader rasterio.open wrapper to silence UserWarning like NotGeoreferencedWarning. (see https://rasterio.readthedocs.io/en/latest/api/rasterio.errors.html) :param args: args to be given to rasterio.open method :type args: str :param kwargs: kwargs to be given to rasterio.open method :type kwargs: Union[int, str, None] :return: rasterio DatasetReader :rtype: rasterio.io.DatasetReader .. py:function:: get_window(roi: Dict, width: int, height: int) -> rasterio.windows.Window Get window from image size and roi :param roi: dictionnary with a roi "col": {"first": , "last": }, "row": {"first": , "last": }, "margins": [, , , ] with margins : left, up, right, down :type roi: dict :param width: image width :type width: int :param height: image height :type height: int :return: Window : Windowed reading with rasterio :rtype: Window .. py:function:: add_disparity(dataset: xarray.Dataset, disparity: Union[Tuple[int, int], list[int], str, None], window: rasterio.windows.Window) -> xarray.Dataset Add disparity to dataset If `disparity` is None, will return dataset unmodified. :param dataset: xarray dataset without classification :type dataset: xr.Dataset :param disparity: disparity, or path to the disparity grid :type disparity: Tuple[int, int] or list[int] or str or None :param window : Windowed reading with rasterio :type window: Window :return: dataset : updated dataset :rtype: xr.Dataset .. py:function:: add_disparity_grid(dataset: xarray.Dataset, disparity_grid: Union[xarray.DataArray, None] = None, disparity_source: Union[Tuple[int, int], list[int], str, None] = 'xr.Dataset') -> xarray.Dataset Add a disparity grid to dataset. If `disparity_grid` is None, will return dataset unmodified. :param dataset: xarray dataset without classification :type dataset: xr.Dataset :param disparity_grid: disparity grid dataset with dimensions ["band_disp", "row", "col"] or None. :type disparity_grid: xr.DataArray or None :param disparity_source: source of the disparity: either a path to a file or `xr.Dataset` if the dataset was directly provided. :type disparity_source: Union[Tuple[int, int], list[int], str, None] :return: dataset : updated dataset :rtype: xr.Dataset .. py:function:: add_classif(dataset: xarray.Dataset, classif: Union[str, None], window: rasterio.windows.Window) -> xarray.Dataset Add classification information and image to dataset :param dataset: xarray dataset without classification :type dataset: xr.Dataset :param classif: classification image path :type classif: str or None :param window : Windowed reading with rasterio :type window: Window :return: dataset : updated dataset :rtype: xr.Dataset .. py:function:: add_segm(dataset: xarray.Dataset, segm: Union[str, None], window: rasterio.windows.Window) -> xarray.Dataset Add Segmentation information and image to dataset :param dataset: xarray dataset without segmentation :type dataset: xr.Dataset :param segm: segmentation image path :type segm: str or None :param window : Windowed reading with rasterio :type window: Window :return: dataset : updated dataset :rtype: xr.Dataset .. py:function:: add_no_data(dataset: xarray.Dataset, no_data: Union[int, float], no_data_pixels: numpy.ndarray) -> xarray.Dataset Add no data information to dataset :param dataset: xarray dataset without no_data information :type dataset: xr.Dataset :param no_data: value :type no_data: int or float :param no_data_pixels: matrix with no_data value :type no_data_pixels: np.ndarray :return: dataset : updated dataset :rtype: xr.Dataset .. py:function:: add_mask(dataset: xarray.Dataset, mask: Union[str, None], no_data_pixels: numpy.ndarray, width: int, height: int, window: rasterio.windows.Window) -> xarray.Dataset Add mask information and image to dataset :param dataset: xarray dataset without mask :type dataset: xr.Dataset :param mask: mask image path :type mask: str or None :param no_data_pixels: matrix with no_data value :type no_data_pixels: np.ndarray :param width: nb columns :type width: int :param height: nb rows :type height: int :param window: information about window :type window: rasterio.window :return: dataset : updated dataset :rtype: xr.Dataset .. py:function:: add_global_disparity(dataset: xarray.Dataset, global_disp_min: int, global_disp_max: int) -> xarray.Dataset Add global disparity information to dataset :param dataset: xarray dataset without no_data information :type dataset: xr.Dataset :param global_disp_min: global minimum disparity :type global_disp_min: int :param global_disp_max: global maximum disparity :type global_disp_max: int :return: dataset : updated dataset :rtype: xr.Dataset .. py:function:: create_dataset_from_inputs(input_config: dict, roi: dict = None) -> xarray.Dataset Read image and mask, and return the corresponding xarray.DataSet :param input_config: configuration used to create dataset. :type input_config: dict :param roi: dictionary with a roi "col": {"first": , "last": }, "row": {"first": , "last": }, "margins": [, , , ] with margins : left, up, right, down :type roi: dict :return: xarray.DataSet containing the variables : - im: 2D (row, col) or 3D (band_im, row, col) xarray.DataArray float32 - disparity (optional): 3D (disp, row, col) xarray.DataArray float32 - msk (optional): 2D (row, col) xarray.DataArray int16 - classif (optional): 3D (band_classif, row, col) xarray.DataArray int16 - segm (optional): 2D (row, col) xarray.DataArray int16 :rtype: xarray.DataSet .. py:function:: get_metadata(img: str, disparity: list[int] | str | None = None, classif: str = None, segm: str = None) -> xarray.Dataset Read metadata from image, and return the corresponding xarray.DataSet :param img: img_path :type img: str :param disparity: disparity couple of ints or path to disparity grid file (optional) :type disparity: list[int] | str | None :param classif: path to the classif (optional) :type classif: str :param segm: path to the segm (optional) :type segm: str :return: partial xarray.DataSet (attributes and coordinates) :rtype: xarray.DataSet .. py:function:: get_pyramids(data, num_scales, scale_factor, channel_axis=None) Returns the pyramid of images, of height num_scales and width factor scale_factor :param data: image :type data: 2D or 3D np.ndarray :param num_scales: number of scaled images :type num_scales: int :param scale_factor: ratio in width/height between two consecutives images of the pyramid :type scale_factor: float :type channel_axis: if specified, axis to use as the channel in the 3D data :type channel_axis: int :return: the list of images in the pyramid, large to small :rtype: list(np.ndarray) .. py:function:: prepare_pyramid(img_left: xarray.Dataset, img_right: xarray.Dataset, num_scales: int, scale_factor: int) -> Tuple[List[xarray.Dataset], List[xarray.Dataset]] Return a List with the datasets at the different scales :param img_left: left Dataset image containing the image im : 2D (row, col) xarray.Dataset :type img_left: xarray.Dataset :param img_right: right Dataset image containing the image im : 2D (row, col) xarray.Dataset :type img_right: xarray.Dataset :param num_scales: number of scales :type num_scales: int :param scale_factor: factor by which downsample the images :type scale_factor: int :return: a List that contains the different scaled datasets :rtype: List of xarray.Dataset .. py:function:: fill_nodata_image(dataset: xarray.Dataset) -> Tuple[numpy.ndarray, numpy.ndarray] Interpolate no data values in image. If no mask was given, create all valid masks :param dataset: Dataset image containing the image im : 2D (row, col) xarray.Dataset :type dataset: xarray.Dataset :return: a Tuple that contains the filled image and mask :rtype: Tuple of np.ndarray .. py:data:: interpolate_nodata_sgm .. py:function:: masks_pyramid(msk: numpy.ndarray, scale_factor: int, num_scales: int) -> List[numpy.ndarray] Return a List with the downsampled masks for each scale :param msk: full resolution mask :type msk: np.ndarray :param scale_factor: scale factor :type scale_factor: int :param num_scales: number of scales :type num_scales: int :return: a List that contains the different scaled masks :rtype: List of np.ndarray .. py:function:: convert_pyramid_to_dataset(img_orig: xarray.Dataset, images: List[numpy.ndarray], masks: List[numpy.ndarray], disps=None) -> List[xarray.Dataset] Return a List with the datasets at the different scales :param img_orig: Dataset image containing the image im : 2D (row, col) xarray.Dataset :type img_orig: xarray.Dataset :param images: list of images for the pyramid :type images: list[np.ndarray] :param masks: list of masks for the pyramid :type masks: list[np.ndarray] :return: a List that contains the different scaled datasets :rtype: List of xarray.Dataset .. py:function:: shift_right_img(img_right: xarray.Dataset, subpix: int, band: str = None, order: int = 1) -> List[xarray.Dataset] Return an array that contains the shifted right images :param img_right: Dataset image containing the image im : 2D (row, col) xarray.Dataset :type img_right: xarray.Dataset :param subpix: subpixel precision = (1 or pair number) :type subpix: int :param band: User's value for selected band :type band: str :param order: order parameter on zoom method :type order: int :return: an array that contains the shifted right images :rtype: array of xarray.Dataset .. py:function:: check_inside_image(img: xarray.Dataset, row: int, col: int) -> bool Check if the coordinates row,col are inside the image :param img: Dataset image containing the image im : 2D (row, col) xarray.Dataset :type img: xarray.Dataset :param row: row coordinates :type row: int :param col: column coordinates :type col: int :return: True if the coordinates row,col are inside the image :rtype: boolean .. py:function:: census_transform(image: xarray.Dataset, window_size: int, band: str = None) -> xarray.Dataset Generates the census transformed image from an image :param image: Dataset image containing the image im : 2D (row, col) xarray.Dataset :type image: xarray.Dataset :param window_size: Census window size :type window_size: int :param band: User's value for selected band :type band: str :return: Dataset census transformed uint32 containing the transformed image im: 2D (row, col) xarray.DataArray uint32 :rtype: xarray.Dataset .. py:function:: compute_mean_raster(img: xarray.Dataset, win_size: int, band: str = None) -> numpy.ndarray Compute the mean within a sliding window for the whole image :param img: Dataset image containing the image im : 2D (row, col) xarray.Dataset :type img: xarray.Dataset :param win_size: window size :type win_size: int :param band: User's value for selected band :type band: str :return: mean raster :rtype: numpy array .. py:data:: find_valid_neighbors .. py:function:: compute_mean_patch(img: xarray.Dataset, row: int, col: int, win_size: int) -> numpy.float64 Compute the mean within a window centered at position row,col :param img: Dataset image containing the image im : 2D (row, col) xarray.Dataset :type img: xarray.Dataset :param row: row coordinates :type row: int :param col: column coordinates :type col: int :param win_size: window size :type win_size: int :return: mean :rtype: float .. py:function:: compute_std_raster(img: xarray.Dataset, win_size: int, band: str = None) -> numpy.ndarray Compute the standard deviation within a sliding window for the whole image with the formula : std = sqrt( E[row^2] - E[row]^2 ) :param img: Dataset image containing the image im : 2D (row, col) xarray.Dataset :type img: xarray.Dataset :param win_size: window size :type win_size: int :param band: User's value for selected band :type band: str :return: std raster :rtype: numpy array .. py:function:: read_disp(disparity: Tuple[int, int] | list[int] | str) -> Tuple[int, int] | Tuple[numpy.ndarray, numpy.ndarray] Read the disparity : - if cfg_disp is the path of a disparity grid, read and return the grids (type tuple of numpy arrays) - else return the value of cfg_disp :param disparity: disparity, or path to the disparity grid :type disparity: Tuple[int, int] or list[int] or str :return: the disparity :rtype: Tuple[int, int] | Tuple[np.ndarray, np.ndarray] .. py:function:: fuse_classification_bands(img: xarray.Dataset, class_names: List[str]) -> numpy.ndarray Get the multiband classification map present in the input image dataset and select the given classes to make a single-band classification map :param img: image dataset :type img: xr.Dataset :param class_names: chosen classification classes :type class_names: List[str] :return: the map representing the selected classifications :rtype: np.ndarray