import rastereasy

Read image

name_im='./data/demo/sentinel.tif'
image=rastereasy.open(name_im)
image.info()
image.colorcomp(extent='pixel',title='original image')
- Size of the image:
   - Rows (height): 1000
   - Cols (width): 1000
   - Bands: 12
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.04099599, 38.39058840)
- Driver: GTiff
- Data type: int16
- Projection system: EPSG:32637
- Nodata: -32768.0

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, '11': 11, '12': 12}
../_images/e8fedcb7addb04c07df94856b8f5a3021c2b81c051f614c6e6e505a8c44bf18c.png

Infos on filters

help(image.filter)
Help on method filter in module rastereasy:

filter(
    method='generic',
    kernel=None,
    sigma=1,
    size=3,
    axis=-1,
    pre_smooth_sigma=None,
    inplace=False,
    dest_name=None
) method of rastereasy.Geoimage instance
    Apply a spatial filter to the Geoimage.

    Parameters
    ----------
    method : str, default="generic"
        Type of filter. Options:
        - "generic" : Generic convolution with a kernel.
        - "gaussian" : Gaussian filter.
        - "median"   : Median filter.
        - "sobel"    : Sobel edge detection (discrete operator).
        - "laplace"  : Laplacian operator (discrete operator).

    kernel : numpy.ndarray, optional
        Convolution kernel (required if mode="generic").

    sigma : float, default=1
        Standard deviation for Gaussian filter (if mode="gaussian").

    size : int, default=3
        Size of the filter window (for median).

    axis : int, default=-1
        Axis along which to compute the Sobel filter (if mode="sobel").
        It is 0 for x, 1 for y. If None, computes gradient magnitude.

    pre_smooth_sigma : float or None, default=None
        If set (e.g., 1.0 or 2.0), a Gaussian filter is applied before Sobel or Laplace,
        useful to reduce noise and simulate larger kernels.

    inplace : bool, default False
        If False, returns a new Geoimage instance with the filtered data.
        If True, modifies the current image in place.

    dest_name : str, optional
        Path to save the filtered image. If None, the image is not saved.
        Default is None.

    Returns
    -------
    Geoimage
        A new filtered Geoimage if inplace=False, otherwise self.

    Raises
    ------
    ValueError
        If `method` is unknown.

    Examples
    --------
    >>> # Create a gaussian with sigma = 8
    >>> imf = image.filter("gaussian", sigma=8)
    >>> # Create a median with size = 7
    >>> imf = image.filter("median", size=7)
    >>> # Create a sobel in x-axis
    >>> imf = image.filter("sobel", axis=0)
    >>> # Create a sobel in y-axis
    >>> imf = image.filter("sobel", axis=1)
    >>> # Create the norm of sobel
    >>> imf = image.filter("sobel")
    >>> # Create a sobel in x-axis with pre_smooth_sigma = 2
    >>> imf = image.filter("sobel", axis=0, pre_smooth_sigma=2)
    >>> # Create a sobel in y-axis with pre_smooth_sigma = 2
    >>> imf = image.filter("sobel", axis=1, pre_smooth_sigma=2)
    >>> # Create the norm of sobel with pre_smooth_sigma = 2
    >>> imf = image.filter("sobel", pre_smooth_sigma=2))
    >>> # Create a laplacian filter
    >>> imf = image.filter("laplace")
    >>> # Create a laplacian filter pre_smooth_sigma = 2
    >>> imf = image.filter("laplace", pre_smooth_sigma=2)

Apply a gaussian filter

imf = image.filter("gaussian", sigma=8)
imf.colorcomp()
imf.info()
../_images/7b492d8bcc820a13d99ec23ce9c6f9fc73af29dd28da48b6644d85a2802d2d1d.png
- Size of the image:
   - Rows (height): 1000
   - Cols (width): 1000
   - Bands: 12
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.04099599, 38.39058840)
- Driver: GTiff
- Data type: float64
- Projection system: EPSG:32637
- Nodata: -32768.0

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, '11': 11, '12': 12}

Apply a median filter

imf = image.filter("median", size=5)
imf.colorcomp()
imf.info()
../_images/fb051db5e245e16b03a69f8c41afc7c2fdbe68c5f6675602846ddb4cf4c1a50a.png
- Size of the image:
   - Rows (height): 1000
   - Cols (width): 1000
   - Bands: 12
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.04099599, 38.39058840)
- Driver: GTiff
- Data type: int16
- Projection system: EPSG:32637
- Nodata: -32768.0

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, '11': 11, '12': 12}

Apply a sobel filter

Without pre-smoothing the image

# on x-axis
imf = image.filter("sobel", axis=0)
imf.colorcomp()
imf.info()
../_images/57bdbaf55a009c41be682c8a1d3a2d3f61be9b1b45e01a57d786f64cd7845619.png
- Size of the image:
   - Rows (height): 1000
   - Cols (width): 1000
   - Bands: 12
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.04099599, 38.39058840)
- Driver: GTiff
- Data type: float64
- Projection system: EPSG:32637
- Nodata: -32768.0

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, '11': 11, '12': 12}
# on y-axis
imf = image.filter("sobel", axis=1)
imf.colorcomp()
imf.info()
../_images/01693b632aa192e90ff85574f716026b65ce6ca15c6edab656e506a23e0f6f1c.png
- Size of the image:
   - Rows (height): 1000
   - Cols (width): 1000
   - Bands: 12
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.04099599, 38.39058840)
- Driver: GTiff
- Data type: float64
- Projection system: EPSG:32637
- Nodata: -32768.0

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, '11': 11, '12': 12}
# norm of the vector
imf = image.filter("sobel")
imf.colorcomp()
imf.info()
../_images/01693b632aa192e90ff85574f716026b65ce6ca15c6edab656e506a23e0f6f1c.png
- Size of the image:
   - Rows (height): 1000
   - Cols (width): 1000
   - Bands: 12
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.04099599, 38.39058840)
- Driver: GTiff
- Data type: float64
- Projection system: EPSG:32637
- Nodata: -32768.0

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, '11': 11, '12': 12}

With pre-smoothing the image

# on x-axis
imf = image.filter("sobel", axis=0, pre_smooth_sigma=4)
imf.colorcomp()
imf.info()
../_images/b55197b8f34a5b33d53428f245e5f2232a265c4acf41a8736458049c2557c490.png
- Size of the image:
   - Rows (height): 1000
   - Cols (width): 1000
   - Bands: 12
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.04099599, 38.39058840)
- Driver: GTiff
- Data type: float64
- Projection system: EPSG:32637
- Nodata: -32768.0

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, '11': 11, '12': 12}
# on y-axis
imf = image.filter("sobel", axis=1,  pre_smooth_sigma=4)
imf.colorcomp()
imf.info()
../_images/84d6dd6c91eb6e2b85aeadfad992b7f7ed8e0117b4268d74de17d57c237ae131.png
- Size of the image:
   - Rows (height): 1000
   - Cols (width): 1000
   - Bands: 12
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.04099599, 38.39058840)
- Driver: GTiff
- Data type: float64
- Projection system: EPSG:32637
- Nodata: -32768.0

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, '11': 11, '12': 12}

Apply a Laplacian filter

Without pre-smoothing the image

imf = image.filter("laplace")
imf.colorcomp()
imf.info()
../_images/527cd5be7c0c7efed6cf0dda8a163fb77bfbe0309ffe7b7146003d1dd825cffd.png
- Size of the image:
   - Rows (height): 1000
   - Cols (width): 1000
   - Bands: 12
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.04099599, 38.39058840)
- Driver: GTiff
- Data type: float64
- Projection system: EPSG:32637
- Nodata: -32768.0

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, '11': 11, '12': 12}
## With pre-smoothing the image
imf = image.filter("laplace", pre_smooth_sigma=9)
imf.colorcomp()
imf.info()
../_images/dfd51b8bb846f44f41da023ba471a307d1a4d36c6f76bab1d8d2ec517ca0a4f5.png
- Size of the image:
   - Rows (height): 1000
   - Cols (width): 1000
   - Bands: 12
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.04099599, 38.39058840)
- Driver: GTiff
- Data type: float64
- Projection system: EPSG:32637
- Nodata: -32768.0

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, '11': 11, '12': 12}

Apply a generic filter

Create a generic filter (example here : average 9x9)

import numpy as np
blur_kernel = np.ones((9, 9)) / (81)

Filter the image and create a new image (with inplace=False, default)

image_filtered=image.filter(method="generic", kernel=blur_kernel)
image_filtered.colorcomp(extent='pixel', title='filtered image')
image_filtered.info()
../_images/9b5a4c49e81cf07814ea659fbb7ff25367ac9a78efe8479066a45eeb1df8c63f.png
- Size of the image:
   - Rows (height): 1000
   - Cols (width): 1000
   - Bands: 12
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.04099599, 38.39058840)
- Driver: GTiff
- Data type: float64
- Projection system: EPSG:32637
- Nodata: -32768.0

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, '11': 11, '12': 12}

Show some details

image.colorcomp(zoom=((200,300),(200,300)),extent='pixel',title='zoom original image')
image_filtered.colorcomp(zoom=((200,300),(200,300)),extent='pixel', title='zoom filtered image')
../_images/9400238b7ce607391c9c46cba9f696bb0d0817a8c914d9d495d33c1a54a631ca.png ../_images/7e24bb2a355ca06809fa175ffae4573598d80826ecb74fff7c13fea397729bb2.png

Filter the image and modify it (with inplace=True)

blur_kernel = np.ones((15, 15)) / (15*15)
image.colorcomp(extent='pixel',title='image before filter')
image.filter(method="generic", kernel=blur_kernel,dest_name='generic_filter.tif',inplace=True)
image.colorcomp(extent='pixel',title='image after filter')
../_images/7d5ee8e158e1191fdc162f70bb45e36d8fa011d690e1d1c0dd6d225c6df99b0e.png ../_images/334e89baad64b7cd41a1198e69c0726794366d1c24edc9d7b126e0b73410ecd2.png