File size: 5,227 Bytes
fab54be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""
OpenCV-based image processing services.
Handles water mask prediction, SAR processing, RGB preview generation, and overlay creation.
"""

import os
import numpy as np
import cv2
import tifffile as tiff
from src import config


def apply_hanning_window(tile_size):
    """Generate a 2D Hanning window for smooth tile blending."""
    window_1d = np.hanning(tile_size)
    return np.outer(window_1d, window_1d).astype(np.float32)


def apply_cloud_detection(prob_cloud, brightness):
    """Detect cloud pixels based on probability and brightness."""
    return ((prob_cloud > config.CLOUD_DETECTION_THRESHOLD) &
            (brightness > config.BRIGHTNESS_THRESHOLD)).astype(np.uint8)


def apply_water_detection(prob_water, ndwi, cloud_mask):
    """Detect water pixels based on probability, NDWI, and cloud exclusion."""
    raw_water = (prob_water > config.NDWI_WATER_THRESHOLD) | (ndwi > config.NDWI_CLOUD_THRESHOLD)
    return raw_water & (cloud_mask == 0) & (ndwi > config.NDWI_MIN_THRESHOLD)


def apply_morphological_cleanup(mask):
    """Apply closing and opening operations to clean mask."""
    close_kernel = np.ones(config.CLOSE_KERNEL_SIZE, np.uint8)
    mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, close_kernel)
    
    open_kernel = np.ones(config.OPEN_KERNEL_SIZE, np.uint8)
    mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, open_kernel)
    
    return mask


def filter_small_contours(mask):
    """Remove contours smaller than minimum area threshold."""
    contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    
    for cnt in contours:
        area = cv2.contourArea(cnt)
        if area < config.MIN_CONTOUR_AREA:
            cv2.drawContours(mask, [cnt], -1, 0, -1)
    
    return mask


def process_sar_image(tiff_path, preview_filename):
    """
    Process SAR image for water detection and visualization.
    """
    if not os.path.exists(tiff_path):
        return None, None
    
    try:
        sar_db = tiff.imread(tiff_path).astype(np.float32)
        
        water_min, water_max = config.SAR_WATER_DB_RANGE
        water_mask = ((sar_db < water_min) & (sar_db > water_max)).astype(np.uint8) * 255
        
        close_kernel = np.ones(config.CLOSE_KERNEL_SIZE, np.uint8)
        water_mask = cv2.morphologyEx(water_mask, cv2.MORPH_CLOSE, close_kernel)
        
        visual = np.clip((sar_db + config.SAR_DB_OFFSET) / config.SAR_DB_OFFSET, 0, 1) * 255
        visual = visual.astype(np.uint8)
        
        h, w = visual.shape
        rgba = np.zeros((h, w, 4), dtype=np.uint8)
        rgba[:, :, 0] = visual  # B
        rgba[:, :, 1] = visual  # G
        rgba[:, :, 2] = visual  # R
        rgba[:, :, 3] = np.where(sar_db < -90, 0, 255)  # Transparency check for missing background data
        
        save_path = os.path.join(config.DOWNLOADS_DIR, preview_filename)
        cv2.imwrite(save_path, rgba)
        
        return water_mask, preview_filename
    
    except Exception as e:
        print(f"❌ SAR Processing Error {tiff_path}: {e}", flush=True)
        return None, None


def create_rgb_preview(tiff_path, filename):
    """
    Create an enhanced RGB preview from optical or water mask data.
    """
    if not os.path.exists(tiff_path):
        return None
    
    try:
        image = tiff.imread(tiff_path).astype(np.float32)
        
        if image.ndim == 2:
            rgb = np.zeros((image.shape[0], image.shape[1], 3), dtype=np.uint8)
            rgb[image == 255] = [255, 0, 0]
            save_path = os.path.join(config.DOWNLOADS_DIR, filename)
            cv2.imwrite(save_path, rgb)
            return filename
        
        if image.shape[0] == 6:
            image = np.transpose(image, (1, 2, 0))
        if np.max(image) > 1.0:
            image = image / config.OPTICAL_NORMALIZATION_FACTOR
        
        rgb = image[:, :, config.RGB_BAND_INDICES]
        rgb = np.clip(rgb * config.RGB_SCALING_FACTOR, 0, 1) * 255
        rgb = rgb.astype(np.uint8)
        
        gaussian = cv2.GaussianBlur(rgb, (0, 0), config.GAUSSIAN_BLUR_SIGMA)
        sharpened = cv2.addWeighted(
            rgb,
            config.GAUSSIAN_BLUR_SHARPEN_STRENGTH,
            gaussian,
            config.GAUSSIAN_BLUR_SUBTRACT_STRENGTH,
            0
        )
        
        save_path = os.path.join(config.DOWNLOADS_DIR, filename)
        cv2.imwrite(save_path, sharpened)
        return filename
    
    except Exception as e:
        print(f"❌ RGB Preview Error {tiff_path}: {e}", flush=True)
        return None


def create_overlay_png(mask, color, filename):
    """
    Create a semi-transparent PNG overlay from a binary mask.
    """
    if mask is None:
        return None
    
    try:
        h, w = mask.shape
        rgba = np.zeros((h, w, 4), dtype=np.uint8)
        b, g, r = color
        
        rgba[mask == 255, 0] = b
        rgba[mask == 255, 1] = g
        rgba[mask == 255, 2] = r
        rgba[mask == 255, 3] = config.OVERLAY_ALPHA
        
        save_path = os.path.join(config.DOWNLOADS_DIR, filename)
        cv2.imwrite(save_path, rgba)
        return filename
    
    except Exception as e:
        print(f"❌ Overlay Error {filename}: {e}", flush=True)
        return None