import rastereasy

Read image

name_im='./data/demo/sentinel.tif'
image=rastereasy.Geoimage(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/92011d6afe6f80afacf700cc472f041f746d215454f4ceccddfb1d77ff77f750.png

Infos on filters

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

filter(method='generic', kernel=None, sigma=1, size=3, axis=-1, pre_smooth_sigma=None, inplace=False, dest_name=None) method of rastereasy.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/989122bee61cbcfb3525bdeeadf589fcfe6568607d68d302a59458a76d5d55ea.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/159d8b2a85553c702485a30432f0cb5cd2d16ef3b92d7644d7b4dfebffa511af.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/b3121a9627bc2d29f9c4c0ebbfc8e4b1fe255312b11c7219f3fc667384eeb22e.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/2d31fbc37550ed6067637203cea82400b31a34708e8e140c8e174cc9157400a5.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/2d31fbc37550ed6067637203cea82400b31a34708e8e140c8e174cc9157400a5.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/fe26ea2f10ad330729cacd642909c78f3e609c0501bcf8f8fe6f60042718b18d.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/c94ffbbfa320f79022af7db9daabb2371bc105707944f1989e824f3c1dac44d4.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/27d01fc1981b46a263910950dd42b4e8447712d5204fd7aaa6f0d68ba5644ec2.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/c701d985ef885200dae5d27560e4e29f5bd02410a9caa6b6782b1d9de1428b47.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/f7ec96b249ba430da99f4f73adc238dc059cf493759a95cef7307db7ecf31f31.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/da6b6ed1158d2795194e30e04597e056fb97dc252d8bf52164b7c083ee98a513.png ../_images/5308761c0c272b2453a63a8bc4feea79a1e109723a24054d9d8a54449b80e432.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/3ffa78fd61f093bc0df57227f95e2a7f877c2012838935b8cfdd24bbac8581e4.png ../_images/8ae9a505ed0942b5f58ebad4835d6969421df0fe58f1bfca97a4f9c2d0dd5686.png