├── file2 ├── file1 ├── README.md └── file3 /file2: -------------------------------------------------------------------------------- 1 | import rasterio 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | # Step 1: Load Green and SWIR bands 6 | green_path = 'path_to_green_band.tif' # Landsat 8 Band 3 (Green) 7 | swir_path = 'path_to_swir_band.tif' # Landsat 8 Band 6 (SWIR1) 8 | 9 | with rasterio.open(green_path) as green_src: 10 | green_band = green_src.read(1).astype('float32') 11 | 12 | with rasterio.open(swir_path) as swir_src: 13 | swir_band = swir_src.read(1).astype('float32') 14 | 15 | # Step 2: Normalize if needed (Optional: depends on dataset) 16 | green_band /= np.max(green_band) 17 | swir_band /= np.max(swir_band) 18 | 19 | # Step 3: Compute MNDWI 20 | mndwi = (green_band - swir_band) / (green_band + swir_band + 1e-6) 21 | 22 | # Step 4: Water Detection (MNDWI > 0) 23 | water_detected = mndwi > 0 24 | 25 | # Step 5: Visualization 26 | fig, axs = plt.subplots(1, 3, figsize=(18, 6)) 27 | 28 | axs[0].imshow(green_band, cmap='Greens') 29 | axs[0].set_title("Green Band") 30 | 31 | axs[1].imshow(mndwi, cmap='coolwarm') 32 | axs[1].set_title("MNDWI") 33 | 34 | axs[2].imshow(water_detected, cmap='Blues') 35 | axs[2].set_title("Detected Water Bodies") 36 | 37 | for ax in axs: 38 | ax.axis('off') 39 | 40 | plt.tight_layout() 41 | plt.show() 42 | -------------------------------------------------------------------------------- /file1: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | # Step 1: Generate Synthetic Satellite Data (Green and SWIR bands) 5 | np.random.seed(42) # For reproducibility 6 | image_shape = (100, 100) 7 | 8 | # Simulate water and non-water regions 9 | green_band = np.random.uniform(0.1, 0.6, image_shape) # Green reflectance 10 | swir_band = np.random.uniform(0.1, 0.6, image_shape) # SWIR reflectance 11 | 12 | # Simulate water bodies (low SWIR, high green reflectance) 13 | water_mask = np.zeros(image_shape) 14 | water_mask[30:70, 30:70] = 1 15 | 16 | green_band[water_mask == 1] = np.random.uniform(0.4, 0.6) 17 | swir_band[water_mask == 1] = np.random.uniform(0.1, 0.2) 18 | 19 | # Step 2: Compute MNDWI 20 | def compute_mndwi(green, swir): 21 | return (green - swir) / (green + swir + 1e-6) 22 | 23 | mndwi = compute_mndwi(green_band, swir_band) 24 | 25 | # Step 3: Threshold to Detect Water Bodies 26 | # Common MNDWI threshold is 0 27 | water_detected = mndwi > 0 28 | 29 | # Step 4: Visualization 30 | fig, axs = plt.subplots(1, 4, figsize=(20, 5)) 31 | 32 | axs[0].imshow(green_band, cmap='Greens') 33 | axs[0].set_title("Synthetic Green Band") 34 | 35 | axs[1].imshow(swir_band, cmap='pink') 36 | axs[1].set_title("Synthetic SWIR Band") 37 | 38 | axs[2].imshow(mndwi, cmap='coolwarm') 39 | axs[2].set_title("MNDWI") 40 | 41 | axs[3].imshow(water_detected, cmap='Blues') 42 | axs[3].set_title("Detected Water Bodies (MNDWI > 0)") 43 | 44 | for ax in axs: 45 | ax.axis('off') 46 | 47 | plt.tight_layout() 48 | plt.show() 49 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 🌊 Water Body Detection from Satellite Imagery 2 | 3 | This project detects water bodies from satellite imagery using: 4 | 5 | Spectral Indices (MNDWI) based thresholding 6 | 7 | Deep Learning (CNN/U-Net) segmentation 8 | 9 | Both synthetic data and real Landsat/Sentinel data 10 | 11 | 📂 Project Structure 12 | 13 | ├── water_body_detection/ 14 | │ ├── synthetic_mndwi_detection.py # MNDWI water detection using synthetic data 15 | │ ├── real_landsat_mndwi_detection.py # Water detection from real Landsat data (rasterio) 16 | │ ├── deep_learning_water_detection.py # Water segmentation using CNN (U-Net) 17 | │ ├── README.md 18 | │ └── requirements.txt 19 | 20 | 🚀 How to Run 21 | 22 | Clone the Repository 23 | 24 | git clone https://github.com/your-username/water-body-detection.git 25 | cd water-body-detection 26 | 27 | Install Dependencies 28 | 29 | pip install -r requirements.txt 30 | 31 | Run Scripts 32 | 33 | Synthetic MNDWI Detection 34 | 35 | python synthetic_mndwi_detection.py 36 | 37 | Real Landsat Data Detection 38 | 39 | python real_landsat_mndwi_detection.py 40 | 41 | Deep Learning Water Detection 42 | 43 | python deep_learning_water_detection.py 44 | 45 | 📚 Methods Used 46 | 1. Spectral Index - MNDWI 47 | 48 | Modified Normalized Difference Water Index (MNDWI) formula: 49 | MNDWI=Green−SWIR1Green+SWIR1 50 | MNDWI=Green+SWIR1Green−SWIR1​ 51 | 52 | Water bodies typically have MNDWI > 0. 53 | 54 | 2. CNN/U-Net Deep Learning 55 | 56 | U-Net model segments water bodies from satellite images. 57 | 58 | Trained on synthetic masks; can be extended to real-world datasets. 59 | 60 | 🛰️ Data Sources 61 | 62 | Synthetic Data: Randomly generated for model prototyping 63 | 64 | Real Data: 65 | 66 | USGS EarthExplorer - Landsat 8/9 67 | 68 | Google Earth Engine 69 | 70 | 📈 Example Results 71 | Input Image Ground Truth Mask Predicted Water Mask 72 | 73 | 74 | 📋 Requirements 75 | 76 | List of Python packages: 77 | 78 | tensorflow 79 | numpy 80 | matplotlib 81 | rasterio 82 | scikit-learn 83 | opencv-python 84 | 85 | 💡 Future Work 86 | 87 | Integrate Sentinel-2 MSI datasets 88 | 89 | Train CNN model on real labeled water datasets (e.g., GSW) 90 | 91 | Deploy using Streamlit dashboard 92 | 93 | Add cloud masking for Landsat/Sentinel images 94 | 95 | 👨‍💻 Author 96 | 97 | Your Name — GitHub | LinkedIn 98 | 99 | 📝 License 100 | 101 | This project is licensed under the MIT License. See the LICENSE file for details. 102 | ✅ Quick Badge 103 | 104 | Water Detection 🔵 | Deep Learning 🧠 | Satellite Imagery 🛰️ 105 | -------------------------------------------------------------------------------- /file3: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | import tensorflow as tf 4 | from tensorflow.keras import layers, models 5 | from sklearn.model_selection import train_test_split 6 | import cv2 7 | 8 | # Step 1: Generate synthetic image data (simulate multispectral bands) 9 | def create_synthetic_data(num_samples=100, img_size=64): 10 | X = [] 11 | Y = [] 12 | for _ in range(num_samples): 13 | # Synthetic RGB image 14 | image = np.random.rand(img_size, img_size, 3) 15 | 16 | # Synthetic water mask (center circle) 17 | mask = np.zeros((img_size, img_size, 1)) 18 | cv2.circle(mask, (img_size//2, img_size//2), img_size//4, (1,), -1) 19 | 20 | X.append(image) 21 | Y.append(mask) 22 | return np.array(X), np.array(Y) 23 | 24 | X, Y = create_synthetic_data(num_samples=200) 25 | 26 | # Step 2: Split into train/test sets 27 | X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2) 28 | 29 | # Step 3: Define U-Net-like CNN model 30 | def build_unet_model(input_shape): 31 | inputs = layers.Input(input_shape) 32 | 33 | # Encoder 34 | c1 = layers.Conv2D(16, 3, activation='relu', padding='same')(inputs) 35 | c1 = layers.Conv2D(16, 3, activation='relu', padding='same')(c1) 36 | p1 = layers.MaxPooling2D()(c1) 37 | 38 | c2 = layers.Conv2D(32, 3, activation='relu', padding='same')(p1) 39 | c2 = layers.Conv2D(32, 3, activation='relu', padding='same')(c2) 40 | p2 = layers.MaxPooling2D()(c2) 41 | 42 | # Bottleneck 43 | b = layers.Conv2D(64, 3, activation='relu', padding='same')(p2) 44 | 45 | # Decoder 46 | u1 = layers.UpSampling2D()(b) 47 | concat1 = layers.concatenate([u1, c2]) 48 | c3 = layers.Conv2D(32, 3, activation='relu', padding='same')(concat1) 49 | 50 | u2 = layers.UpSampling2D()(c3) 51 | concat2 = layers.concatenate([u2, c1]) 52 | c4 = layers.Conv2D(16, 3, activation='relu', padding='same')(concat2) 53 | 54 | outputs = layers.Conv2D(1, 1, activation='sigmoid')(c4) 55 | 56 | model = models.Model(inputs, outputs) 57 | return model 58 | 59 | # Step 4: Compile and Train 60 | model = build_unet_model((64, 64, 3)) 61 | model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) 62 | model.fit(X_train, Y_train, epochs=10, batch_size=8, validation_split=0.1) 63 | 64 | # Step 5: Predict and Visualize 65 | preds = model.predict(X_test[:5]) 66 | 67 | for i in range(5): 68 | fig, axs = plt.subplots(1, 3, figsize=(10, 4)) 69 | axs[0].imshow(X_test[i]) 70 | axs[0].set_title('Input Image') 71 | 72 | axs[1].imshow(Y_test[i].squeeze(), cmap='gray') 73 | axs[1].set_title('True Water Mask') 74 | 75 | axs[2].imshow(preds[i].squeeze() > 0.5, cmap='Blues') 76 | axs[2].set_title('Predicted Water Mask') 77 | 78 | for ax in axs: 79 | ax.axis('off') 80 | plt.tight_layout() 81 | plt.show() 82 | --------------------------------------------------------------------------------