pages/_projects/sap.md

3.8 KiB
Raw Blame History

layout title description img importance category
page SAP Python package to compute morphological hierarchies of images and more. /assets/img/sap.svg 1 thesis

SAP (for Simple Attribute Profiles) is a Python package to easily compute attribute profiles of images. I have developed this package as part of my PhD thesis.

The source code is available on github. I used this project to experiments CI/CD with gitlab pipelines (the project was initially hosted on the INRIA's gitlab) and lately with Github actions. Testing, code coverage, release publishing on PyPI and online documentation are all automatically updated.

Installation

To start tinkering images with the package, you just have to:

pip install sap

Quick start

A small Python snippet to get you started quickly:

import sap
import numpy as np
import matplotlib.pyplot as plt

image = np.random.random((512, 512))

t = sap.MaxTree(image)
area = t.get_attribute('area')
filtered_image = t.reconstruct(area < 100)

plt.imshow(filtered_image)
plt.show()

Slower launch

This package is a combination of three submodules.

Trees

The first submodule sap.trees is to build trees from images, to compute attributes, and to filter them.

For example, we can build the max-tree of an image, compute the area attributes of the nodes and reconstruct a filtered image removing nodes with area less than 100 pixels:

t = sap.MaxTree(image)
area = t.get_attribute('area')
filtered_image = t.reconstruct(area < 100)

Profiles

The second submodule sap.profiles is provided to compute Attribute Profiles (and other profiles) of images. The submodule contains the utils to easily concatenate the profiles (Extended Attribute Profiles) and to display them.

import imageio.v3 as iio # Reads and writes images
import sap

image = iio.imread('image.png')

ap = sap.attribute_profiles(image, {'area': [100, 1000]})
sap.show_profiles(ap)

Attribute profiles stacks connected component filtering of images (opening and closing) at several scales.{.img-fluid .rounded .z-depth-1}

Spectra

The third submodule is sap.spectra. We use it to compute Pattern Spectra of trees and to display them. Pattern Spectra can be useful to set thresholds of attribute filters and Attribute Profiles.

import rasterio as rio # Reads and writes geospatial raster data
from matplotlib import pyplot as plt # Display plots and images
import sap

dsm = rio.open('dsm.tif').read()[0]

max_tree = sap.MaxTree(dsm)

plt.imshow(max_tree.reconstruct())
plt.show()

{.img-fluid .z-depth-1 .rounded}

ps = sap.spectrum2d(max_tree, 'area', 'compactness', x_log=True)

sap.show_spectrum(*ps)

plt.xlabel('area')
plt.ylabel('compactness')
plt.colorbar()
plt.title('SAP 2D spectrum')

plt.show()

{.img-fluid .z-depth-1 .rounded}

To go further, please have a look to the online documentation.

This package has been used, amongst other projects, to perform an experimental comparison of the attribute profiles and their variations in a published paper 1.


  1. Deise Santana Maia, Minh-Tan Pham, Erchan Aptoula, Florent Guiotte, et Sébastien Lefèvre, « Classification of Remote Sensing Data With Morphological Attribute Profiles: A decade of advances », GRSM, vol. 9, nᵒ 3, p. 4371, sept. 2021, doi: 10.1109/MGRS.2021.3051859. ↩︎