├── code ├── code1 ├── README.md └── file /code: -------------------------------------------------------------------------------- 1 | import ee 2 | ee.Initialize() 3 | 4 | def get_ndvi_image(year): 5 | image = ee.ImageCollection('COPERNICUS/S2_SR') \ 6 | .filterDate(f'{year}-01-01', f'{year}-12-31') \ 7 | .filterBounds(ee.Geometry.BBox(4.5, 4.3, 5.2, 5.0)) \ 8 | .median() 9 | 10 | ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI') 11 | return ndvi 12 | 13 | def export_ndvi(year, region, file_name): 14 | ndvi_img = get_ndvi_image(year) 15 | task = ee.batch.Export.image.toDrive( 16 | image=ndvi_img.clip(region), 17 | description=file_name, 18 | folder='GEE_Exports', 19 | fileNamePrefix=file_name, 20 | region=region, 21 | scale=10 22 | ) 23 | task.start() 24 | -------------------------------------------------------------------------------- /code1: -------------------------------------------------------------------------------- 1 | import rasterio 2 | import geopandas as gpd 3 | import numpy as np 4 | import pandas as pd 5 | 6 | def extract_ndvi_features(ndvi_tif, shapefile): 7 | with rasterio.open(ndvi_tif) as src: 8 | ndvi = src.read(1) 9 | ndvi[ndvi == src.nodata] = np.nan 10 | 11 | gdf = gpd.read_file(shapefile) 12 | features = [] 13 | 14 | for _, row in gdf.iterrows(): 15 | mask = rasterio.features.geometry_mask([row.geometry], src.shape, src.transform, invert=True) 16 | values = ndvi[mask] 17 | mean_ndvi = np.nanmean(values) 18 | features.append([mean_ndvi, row['health_label']]) # label: 0 = unhealthy, 1 = healthy 19 | 20 | return pd.DataFrame(features, columns=['ndvi_mean', 'label']) 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 🌿 Mangrove Health Monitoring Using NDVI and Machine Learning 2 | 📌 Project Overview 3 | 4 | This project focuses on assessing the health of mangrove ecosystems using satellite-derived vegetation indices, particularly the Normalized Difference Vegetation Index (NDVI), combined with Machine Learning (ML) models. The study aims to map, monitor, and classify mangrove health conditions over time, with applications in environmental conservation, ecosystem restoration, and climate resilience. 5 | 🎯 Objectives 6 | 7 | Extract and analyze NDVI values from multi-temporal satellite imagery (e.g., Landsat, Sentinel-2). 8 | 9 | Classify mangrove vegetation health into categories (e.g., healthy, stressed, degraded). 10 | 11 | Apply supervised ML models (e.g., Random Forest, SVM, XGBoost) for health prediction. 12 | 13 | Generate geospatial health maps to visualize degradation patterns. 14 | 15 | Validate results using ground-truth data and/or high-resolution images. 16 | 17 | 🛰️ Data Sources 18 | 19 | Satellite Imagery: Sentinel-2A, Landsat 8 OLI 20 | 21 | Preprocessing Tools: Google Earth Engine (GEE), QGIS, ArcGIS 22 | 23 | NDVI Calculation: (NIR - Red) / (NIR + Red) 24 | 25 | Ancillary Data: Field observations (if available), land cover maps, DEMs 26 | 27 | 🧠 Machine Learning Models 28 | 29 | Implemented classifiers for NDVI-based mangrove health prediction: 30 | 31 | Random Forest (RF) 32 | 33 | Support Vector Machine (SVM) 34 | 35 | Gradient Boosted Trees (XGBoost) 36 | 37 | K-Nearest Neighbors (KNN) 38 | 39 | Each model is trained and evaluated using accuracy, confusion matrix, precision, recall, and F1-score. 40 | 🗂️ Project Structure 41 | 42 | Mangrove-Health-Monitoring-Using-NDVI-and-ML/ 43 | ├── data/ # Satellite and ancillary data 44 | ├── notebooks/ # Jupyter Notebooks for analysis and modeling 45 | ├── scripts/ # Python scripts for automation 46 | ├── results/ # Maps, model outputs, evaluation metrics 47 | ├── figures/ # Visualizations and plots 48 | ├── models/ # Trained ML models 49 | └── README.md # Project documentation 50 | 51 | 🔧 Tools & Technologies 52 | 53 | Google Earth Engine (GEE) 54 | 55 | Python (Pandas, Scikit-learn, Rasterio, Numpy, Matplotlib, Seaborn) 56 | 57 | QGIS / ArcGIS Pro 58 | 59 | Jupyter Notebooks 60 | 61 | Git for version control 62 | 63 | 📊 Outputs 64 | 65 | NDVI time-series maps of mangrove regions 66 | 67 | Mangrove health classification maps 68 | 69 | ML model performance reports 70 | 71 | Geostatistical and accuracy assessment charts 72 | 73 | 📌 Use Cases 74 | 75 | Coastal ecosystem health monitoring 76 | 77 | Environmental impact assessments 78 | 79 | Restoration planning for degraded mangroves 80 | 81 | Climate change adaptation and biodiversity conservation 82 | 83 | 🧪 Future Work 84 | 85 | Integration with SAR data (e.g., Sentinel-1) for improved detection under cloud cover. 86 | 87 | Use of deep learning (e.g., CNN) for higher classification accuracy. 88 | 89 | Real-time NDVI change detection with cloud-based pipelines. 90 | 91 | 📚 References 92 | 93 | Giri, C., Ochieng, E., Tieszen, L. L., et al. (2011). Status and distribution of mangrove forests of the world using earth observation satellite data. Global Ecology and Biogeography, 20(1), 154–159. 94 | 95 | Tucker, C. J. (1979). Red and photographic infrared linear combinations for monitoring vegetation. Remote Sensing of Environment, 8(2), 127–150. 96 | 97 | Belgiu, M., & Drăguţ, L. (2016). Random forest in remote sensing: A review of applications and future directions. ISPRS Journal of Photogrammetry and Remote Sensing, 114, 24–31. 98 | 99 | 🤝 Contribution 100 | 101 | Feel free to fork the repository, submit issues, or create pull requests to improve the project. Collaboration is welcome! 102 | 📝 License 103 | 104 | This project is open-source and available under the MIT License. 105 | -------------------------------------------------------------------------------- /file: -------------------------------------------------------------------------------- 1 | import ee 2 | import os 3 | import numpy as np 4 | import pandas as pd 5 | import geopandas as gpd 6 | import rasterio 7 | import joblib 8 | from sklearn.ensemble import RandomForestClassifier 9 | from sklearn.model_selection import train_test_split 10 | from sklearn.metrics import classification_report 11 | import matplotlib.pyplot as plt 12 | from rasterio.features import geometry_mask 13 | 14 | # ----------------------------------------------- 15 | # Initialize Earth Engine 16 | # ----------------------------------------------- 17 | ee.Initialize() 18 | 19 | # ----------------------------------------------- 20 | # 1. NDVI Extraction 21 | # ----------------------------------------------- 22 | def get_ndvi_image(year, region_bounds): 23 | image = ee.ImageCollection('COPERNICUS/S2_SR') \ 24 | .filterDate(f'{year}-01-01', f'{year}-12-31') \ 25 | .filterBounds(region_bounds) \ 26 | .median() 27 | 28 | ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI') 29 | return ndvi 30 | 31 | def export_ndvi_to_drive(ndvi_img, region, file_name): 32 | task = ee.batch.Export.image.toDrive( 33 | image=ndvi_img.clip(region), 34 | description=file_name, 35 | folder='GEE_Exports', 36 | fileNamePrefix=file_name, 37 | region=region, 38 | scale=10 39 | ) 40 | task.start() 41 | print(f"Export started for {file_name}...") 42 | 43 | # ----------------------------------------------- 44 | # 2. Prepare Training Dataset 45 | # ----------------------------------------------- 46 | def extract_ndvi_features(ndvi_tif, shapefile): 47 | with rasterio.open(ndvi_tif) as src: 48 | ndvi = src.read(1) 49 | ndvi[ndvi == src.nodata] = np.nan 50 | transform = src.transform 51 | shape = src.shape 52 | 53 | gdf = gpd.read_file(shapefile) 54 | features = [] 55 | 56 | for _, row in gdf.iterrows(): 57 | mask = geometry_mask([row.geometry], shape, transform, invert=True) 58 | values = ndvi[mask] 59 | mean_ndvi = np.nanmean(values) 60 | features.append([mean_ndvi, row['health_label']]) 61 | 62 | return pd.DataFrame(features, columns=['ndvi_mean', 'label']) 63 | 64 | # ----------------------------------------------- 65 | # 3. Train Machine Learning Model 66 | # ----------------------------------------------- 67 | def train_model(training_csv): 68 | df = pd.read_csv(training_csv) 69 | X = df[['ndvi_mean']] 70 | y = df['label'] 71 | 72 | X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) 73 | 74 | model = RandomForestClassifier(n_estimators=100, random_state=42) 75 | model.fit(X_train, y_train) 76 | 77 | y_pred = model.predict(X_test) 78 | print("Classification Report:\n", classification_report(y_test, y_pred)) 79 | 80 | joblib.dump(model, 'mangrove_health_rf.pkl') 81 | print("Model saved as 'mangrove_health_rf.pkl'") 82 | return model 83 | 84 | # ----------------------------------------------- 85 | # 4. Classify Entire NDVI Raster Using Model 86 | # ----------------------------------------------- 87 | def classify_ndvi_raster(ndvi_tif, output_tif, model_path='mangrove_health_rf.pkl'): 88 | model = joblib.load(model_path) 89 | 90 | with rasterio.open(ndvi_tif) as src: 91 | ndvi = src.read(1) 92 | profile = src.profile 93 | ndvi[ndvi == src.nodata] = np.nan 94 | 95 | flat_ndvi = ndvi.flatten() 96 | valid_mask = ~np.isnan(flat_ndvi) 97 | predictions = np.full_like(flat_ndvi, -1, dtype=int) 98 | predictions[valid_mask] = model.predict(flat_ndvi[valid_mask].reshape(-1, 1)) 99 | 100 | classified = predictions.reshape(ndvi.shape) 101 | profile.update(dtype='int16', count=1) 102 | 103 | with rasterio.open(output_tif, 'w', **profile) as dst: 104 | dst.write(classified.astype('int16'), 1) 105 | 106 | print(f"Classified output saved to {output_tif}") 107 | 108 | # ----------------------------------------------- 109 | # 5. Visualize Classified Health Map 110 | # ----------------------------------------------- 111 | def plot_classified_map(tif_path): 112 | with rasterio.open(tif_path) as src: 113 | classified = src.read(1) 114 | 115 | plt.figure(figsize=(10, 6)) 116 | plt.imshow(classified, cmap='RdYlGn') 117 | plt.title("Mangrove Health Classification Map") 118 | plt.colorbar(label='Health Class (0 = Unhealthy, 1 = Healthy)') 119 | plt.axis('off') 120 | plt.show() 121 | 122 | # ----------------------------------------------- 123 | # Example Usage 124 | # ----------------------------------------------- 125 | if __name__ == "__main__": 126 | # Step 1: NDVI Extraction 127 | region = ee.Geometry.BBox(4.5, 4.3, 5.2, 5.0) # Modify to your ROI 128 | ndvi_img = get_ndvi_image(2024, region) 129 | export_ndvi_to_drive(ndvi_img, region, "NDVI_2024_Mangrove") 130 | 131 | # Step 2: Prepare dataset (after downloading NDVI from GEE manually) 132 | # features_df = extract_ndvi_features("NDVI_2024_Mangrove.tif", "training_points.shp") 133 | # features_df.to_csv("training_data.csv", index=False) 134 | 135 | # Step 3: Train model 136 | # model = train_model("training_data.csv") 137 | 138 | # Step 4: Classify full raster 139 | # classify_ndvi_raster("NDVI_2024_Mangrove.tif", "classified_mangrove_health.tif") 140 | 141 | # Step 5: Visualize results 142 | # plot_classified_map("classified_mangrove_health.tif") 143 | --------------------------------------------------------------------------------