This project implements a cloud detection algorithm for hyperspectral images. It identifies and quantifies cloud coverage by combining spectral thresholding, texture-based classification, and manual cloud mask refinement.
Clouds can obscure the Earth's surface and affect the quality of hyperspectral observations. Identifying cloud-covered regions helps assess image usability and supports the selection and processing of suitable data for further analysis.
The algorithm uses a semi-automated approach: spectral thresholding identifies core cloud regions, texture analysis detects additional cloud margins, and an interactive editing tool allows users to refine the final cloud mask.
This implementation is based on the research presented in Detection and removal of cloud and cloud shadow contamination from hyperspectral images of Hyperion sensor.
The algorithm consists of three main stages: core cloud detection, cloud margin detection, and manual cloud mask refinement. These stages produce a final cloud mask that can be used to visualize cloud coverage, mask the original data, and quantify the proportion of cloud pixels.
Core clouds are identified by thresholding radiance values in a user-selected spectral band, where clouds may appear brighter than surrounding land cover.
The user selects a spectral band and radiance threshold, either interactively or through configured parameters. Pixels with radiance values above the threshold are classified as cloud (1), while all other pixels are classified as non-cloud (0). This produces the initial binary cloud mask.
To identify cloud edges missed during thresholding, the algorithm uses texture-based classification.
Texture Image Generation: A texture image is generated from the selected spectral band by calculating the variance of a Grey-Level Co-occurrence Matrix (GLCM) within a sliding window around each pixel. The resulting image highlights local texture variations that may indicate cloud boundaries.
Rule-Based Object Classification: Otsu's thresholding method identifies regions of high texture variation. Connected regions are labeled, filtered by minimum area, and checked for proximity to core clouds using a dilated version of the initial cloud mask. Morphological closing is then applied to clean the resulting cloud margin mask.
The core cloud and cloud margin masks are combined to create a more complete cloud mask.
The combined cloud mask can be refined using an interactive Matplotlib interface displaying the original spectral band, cloud mask, and masked data.
Users can select the displayed band, adjust the brush size, and paint to add or erase cloud pixels. The result is a refined binary mask, with cloud pixels represented by 1 and non-cloud pixels by 0.
The final mask can then be applied to the hyperspectral datacube or used to calculate cloud coverage.
Cloud Detection/
├── data/ # Sample hyperspectral datacube
├── data_output/ # Sample plots, masks, and masked data
├── cloud_detection.py # Main cloud detection algorithms
├── config.py # Configuration and parameters
├── main.py # Main algorithm workflow
├── utility.py # Utility functions
├── visualize.py # Visualization functions
└── README.md
The algorithm can produce:
- Core cloud mask: Cloud regions identified through spectral thresholding.
- Texture image: GLCM variance used for cloud margin detection.
- Cloud margin mask: Additional cloud regions identified through texture analysis.
- Final cloud mask: Combined and manually refined binary mask.
- Masked datacube: Hyperspectral data with cloud pixels set to zero.
- Cloud coverage: Fraction of image pixels classified as cloud.
Cloud coverage is calculated as:
Create and activate a virtual environment:
Windows
python -m venv venv
venv\Scripts\activatemacOS / Linux
python3 -m venv venv
source venv/bin/activateInstall the required dependencies from requirements.txt:
pip install -r requirements.txtWith the virtual environment activated, run the algorithm from the project root:
python main.pyThe workflow loads the hyperspectral data, generates the core cloud mask, detects cloud margins, and allows manual refinement. Depending on the configuration, interactive windows may open for band selection, threshold adjustment, and mask editing.
Sample input data is provided in data/, and example outputs can be found in data_output/.
Detection accuracy depends on spectral band selection, threshold values, texture parameters, and manual refinement. High texture variation may also occur in non-cloud regions, so the final mask should be inspected before using it in further analysis.