pyosv.io.batch_reader

  1from ..utils.paths import get_path_gui
  2
  3from rasterio.windows import Window
  4import numpy as np
  5import rasterio
  6
  7
  8def load(path : str, patch_shape : tuple = (64,64)) -> np.ndarray:
  9    '''
 10        Load an image patch by patch
 11
 12        Supported data format
 13
 14        RASTERIO_EXTENSIONS   = ['.tif', '.tiff', '.geotiff']  
 15        MATPLOTLIB_EXTENSIONS = ['.png', '.jpg', 'jpeg']
 16
 17        Returns always data in channel last format.
 18
 19        If image extension is in MATPLOTLIB_EXTENSIONS, metadata and bound will be None.
 20        
 21        Parameters:
 22        -----------
 23            - path : str
 24                position of the image, if None the function will ask for the image path using a menu
 25            - patch_shape :  tuple[int,int]
 26                tuple of two integers representing the size of the patches to be loaded from the image
 27
 28        Returns:
 29        --------
 30            - data : np.ndarray
 31                patch_shape[0]xpatch_shape[1]xB image patch, with patch_shape[0] width, patch_shape[1] height and B bands
 32        
 33        Usage:
 34        ------
 35        ```python
 36            iterator = iter(
 37                load(None, patch_size = (64,64))
 38                )
 39
 40                patch = next(iterator) 
 41        ``` 
 42        or
 43        ```python
 44            img = iter(
 45                load("path/to/image.png")
 46                )
 47            patch = next(iterator) 
 48
 49        ``` 
 50
 51        Output:
 52        -------
 53        ```
 54        P1:
 55
 56            array([[[5872., 5532., 5516., ...,    0.,    0., 1024.],  
 57                    [5872., 5588., 5451., ...,    0.,    0., 1024.],  
 58                    [5872., 5606., 5333., ...,    0.,    0., 1024.],  
 59                    ...,  
 60                    [2672., 2602., 2368., ...,    0.,    0., 1024.],  
 61                    [2672., 2689., 2394., ...,    0.,    0., 1024.],  
 62                    [2672., 2705., 2431., ...,    0.,    0., 1024.]],  
 63                    ...,  
 64                    [[1571., 1318., 1167., ...,    0.,    0.,    0.],  
 65                    [1571., 1206., 1113., ...,    0.,    0.,    0.],  
 66                    [1571., 1230., 1094., ...,    0.,    0.,    0.],  
 67                    ...,  
 68                    [1330., 1044.,  837., ...,    0.,    0.,    0.],  
 69                    [1330., 1045.,  842., ...,    0.,    0.,    0.],  
 70                    [1330., 1032.,  833., ...,    0.,    0.,    0.]]])
 71        
 72        P2:
 73            array([[[1015.,  741.,  673., ...,    0.,    0.,    0.],  
 74                    [1015.,  729.,  676., ...,    0.,    0.,    0.],  
 75                    [1015.,  757.,  670., ...,    0.,    0.,    0.],  
 76                    ...,  
 77                    [1039.,  764.,  692., ...,    0.,    0.,    0.],  
 78                    [1039.,  752.,  702., ...,    0.,    0.,    0.],  
 79                    [1039.,  761.,  736., ...,    0.,    0.,    0.]],  
 80                    ...,  
 81                    [[1012.,  728.,  630., ...,    0.,    0.,    0.],  
 82                    [1012.,  742.,  686., ...,    0.,    0.,    0.],  
 83                    [1012.,  754.,  724., ...,    0.,    0.,    0.],  
 84                    ...,  
 85                    [1033.,  773.,  715., ...,    0.,    0.,    0.],  
 86                    [1033.,  768.,  733., ...,    0.,    0.,    0.],  
 87                    [1033.,  763.,  745., ...,    0.,    0.,    0.]]])  
 88
 89        ```
 90    '''
 91
 92    RASTERIO_EXTENSIONS = ['.tif', '.tiff']
 93
 94    if path is None:
 95        path = get_path_gui()
 96
 97    if any(frmt in path for frmt in RASTERIO_EXTENSIONS):
 98
 99        with rasterio.open(path) as src:
100            c, w, h = src.read().shape
101
102        for i in range(0, w - patch_shape[0], patch_shape[0]):
103            for j in range(0, h -  patch_shape[1], patch_shape[1]):
104
105                with rasterio.open(path) as src:
106                    data = src.read(window=Window(j, i, patch_shape[1], patch_shape[0]))
107                    data = np.moveaxis(data, 0, -1)
108                yield data
109    else:
110        raise Exception('Error: file can not be opened or format not supported!')
def load(path: str, patch_shape: tuple = (64, 64)) -> numpy.ndarray:
  9def load(path : str, patch_shape : tuple = (64,64)) -> np.ndarray:
 10    '''
 11        Load an image patch by patch
 12
 13        Supported data format
 14
 15        RASTERIO_EXTENSIONS   = ['.tif', '.tiff', '.geotiff']  
 16        MATPLOTLIB_EXTENSIONS = ['.png', '.jpg', 'jpeg']
 17
 18        Returns always data in channel last format.
 19
 20        If image extension is in MATPLOTLIB_EXTENSIONS, metadata and bound will be None.
 21        
 22        Parameters:
 23        -----------
 24            - path : str
 25                position of the image, if None the function will ask for the image path using a menu
 26            - patch_shape :  tuple[int,int]
 27                tuple of two integers representing the size of the patches to be loaded from the image
 28
 29        Returns:
 30        --------
 31            - data : np.ndarray
 32                patch_shape[0]xpatch_shape[1]xB image patch, with patch_shape[0] width, patch_shape[1] height and B bands
 33        
 34        Usage:
 35        ------
 36        ```python
 37            iterator = iter(
 38                load(None, patch_size = (64,64))
 39                )
 40
 41                patch = next(iterator) 
 42        ``` 
 43        or
 44        ```python
 45            img = iter(
 46                load("path/to/image.png")
 47                )
 48            patch = next(iterator) 
 49
 50        ``` 
 51
 52        Output:
 53        -------
 54        ```
 55        P1:
 56
 57            array([[[5872., 5532., 5516., ...,    0.,    0., 1024.],  
 58                    [5872., 5588., 5451., ...,    0.,    0., 1024.],  
 59                    [5872., 5606., 5333., ...,    0.,    0., 1024.],  
 60                    ...,  
 61                    [2672., 2602., 2368., ...,    0.,    0., 1024.],  
 62                    [2672., 2689., 2394., ...,    0.,    0., 1024.],  
 63                    [2672., 2705., 2431., ...,    0.,    0., 1024.]],  
 64                    ...,  
 65                    [[1571., 1318., 1167., ...,    0.,    0.,    0.],  
 66                    [1571., 1206., 1113., ...,    0.,    0.,    0.],  
 67                    [1571., 1230., 1094., ...,    0.,    0.,    0.],  
 68                    ...,  
 69                    [1330., 1044.,  837., ...,    0.,    0.,    0.],  
 70                    [1330., 1045.,  842., ...,    0.,    0.,    0.],  
 71                    [1330., 1032.,  833., ...,    0.,    0.,    0.]]])
 72        
 73        P2:
 74            array([[[1015.,  741.,  673., ...,    0.,    0.,    0.],  
 75                    [1015.,  729.,  676., ...,    0.,    0.,    0.],  
 76                    [1015.,  757.,  670., ...,    0.,    0.,    0.],  
 77                    ...,  
 78                    [1039.,  764.,  692., ...,    0.,    0.,    0.],  
 79                    [1039.,  752.,  702., ...,    0.,    0.,    0.],  
 80                    [1039.,  761.,  736., ...,    0.,    0.,    0.]],  
 81                    ...,  
 82                    [[1012.,  728.,  630., ...,    0.,    0.,    0.],  
 83                    [1012.,  742.,  686., ...,    0.,    0.,    0.],  
 84                    [1012.,  754.,  724., ...,    0.,    0.,    0.],  
 85                    ...,  
 86                    [1033.,  773.,  715., ...,    0.,    0.,    0.],  
 87                    [1033.,  768.,  733., ...,    0.,    0.,    0.],  
 88                    [1033.,  763.,  745., ...,    0.,    0.,    0.]]])  
 89
 90        ```
 91    '''
 92
 93    RASTERIO_EXTENSIONS = ['.tif', '.tiff']
 94
 95    if path is None:
 96        path = get_path_gui()
 97
 98    if any(frmt in path for frmt in RASTERIO_EXTENSIONS):
 99
100        with rasterio.open(path) as src:
101            c, w, h = src.read().shape
102
103        for i in range(0, w - patch_shape[0], patch_shape[0]):
104            for j in range(0, h -  patch_shape[1], patch_shape[1]):
105
106                with rasterio.open(path) as src:
107                    data = src.read(window=Window(j, i, patch_shape[1], patch_shape[0]))
108                    data = np.moveaxis(data, 0, -1)
109                yield data
110    else:
111        raise Exception('Error: file can not be opened or format not supported!')

Load an image patch by patch

Supported data format

RASTERIO_EXTENSIONS = ['.tif', '.tiff', '.geotiff']
MATPLOTLIB_EXTENSIONS = ['.png', '.jpg', 'jpeg']

Returns always data in channel last format.

If image extension is in MATPLOTLIB_EXTENSIONS, metadata and bound will be None.

Parameters:

- path : str
    position of the image, if None the function will ask for the image path using a menu
- patch_shape :  tuple[int,int]
    tuple of two integers representing the size of the patches to be loaded from the image

Returns:

- data : np.ndarray
    patch_shape[0]xpatch_shape[1]xB image patch, with patch_shape[0] width, patch_shape[1] height and B bands

Usage:

    iterator = iter(
        load(None, patch_size = (64,64))
        )

        patch = next(iterator) 

or

    img = iter(
        load("path/to/image.png")
        )
    patch = next(iterator) 

Output:

P1:

    array([[[5872., 5532., 5516., ...,    0.,    0., 1024.],  
            [5872., 5588., 5451., ...,    0.,    0., 1024.],  
            [5872., 5606., 5333., ...,    0.,    0., 1024.],  
            ...,  
            [2672., 2602., 2368., ...,    0.,    0., 1024.],  
            [2672., 2689., 2394., ...,    0.,    0., 1024.],  
            [2672., 2705., 2431., ...,    0.,    0., 1024.]],  
            ...,  
            [[1571., 1318., 1167., ...,    0.,    0.,    0.],  
            [1571., 1206., 1113., ...,    0.,    0.,    0.],  
            [1571., 1230., 1094., ...,    0.,    0.,    0.],  
            ...,  
            [1330., 1044.,  837., ...,    0.,    0.,    0.],  
            [1330., 1045.,  842., ...,    0.,    0.,    0.],  
            [1330., 1032.,  833., ...,    0.,    0.,    0.]]])

P2:
    array([[[1015.,  741.,  673., ...,    0.,    0.,    0.],  
            [1015.,  729.,  676., ...,    0.,    0.,    0.],  
            [1015.,  757.,  670., ...,    0.,    0.,    0.],  
            ...,  
            [1039.,  764.,  692., ...,    0.,    0.,    0.],  
            [1039.,  752.,  702., ...,    0.,    0.,    0.],  
            [1039.,  761.,  736., ...,    0.,    0.,    0.]],  
            ...,  
            [[1012.,  728.,  630., ...,    0.,    0.,    0.],  
            [1012.,  742.,  686., ...,    0.,    0.,    0.],  
            [1012.,  754.,  724., ...,    0.,    0.,    0.],  
            ...,  
            [1033.,  773.,  715., ...,    0.,    0.,    0.],  
            [1033.,  768.,  733., ...,    0.,    0.,    0.],  
            [1033.,  763.,  745., ...,    0.,    0.,    0.]]])