Spaces:
Runtime error
Runtime error
Remove unused heuristic water mask
Browse files- app/water.py +0 -76
app/water.py
DELETED
|
@@ -1,76 +0,0 @@
|
|
| 1 |
-
from __future__ import annotations
|
| 2 |
-
|
| 3 |
-
import cv2
|
| 4 |
-
import numpy as np
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
def heuristic_water_mask(
|
| 8 |
-
rgb_np: np.ndarray,
|
| 9 |
-
grad_norm: np.ndarray,
|
| 10 |
-
texture_norm: np.ndarray | None = None,
|
| 11 |
-
max_side: int = 512,
|
| 12 |
-
) -> np.ndarray | None:
|
| 13 |
-
"""Cheap water detector using color, low texture, and flat depth cues."""
|
| 14 |
-
if rgb_np is None or grad_norm is None:
|
| 15 |
-
return None
|
| 16 |
-
h, w = rgb_np.shape[:2]
|
| 17 |
-
if h == 0 or w == 0:
|
| 18 |
-
return None
|
| 19 |
-
|
| 20 |
-
scale = min(1.0, float(max_side) / float(max(h, w)))
|
| 21 |
-
if scale < 1.0:
|
| 22 |
-
new_size = (max(1, int(round(w * scale))), max(1, int(round(h * scale))))
|
| 23 |
-
rgb_small = cv2.resize(rgb_np, new_size, interpolation=cv2.INTER_AREA)
|
| 24 |
-
grad_small = cv2.resize(grad_norm, new_size, interpolation=cv2.INTER_LINEAR)
|
| 25 |
-
tex_small = (
|
| 26 |
-
cv2.resize(texture_norm, new_size, interpolation=cv2.INTER_LINEAR) if texture_norm is not None else None
|
| 27 |
-
)
|
| 28 |
-
else:
|
| 29 |
-
rgb_small = rgb_np
|
| 30 |
-
grad_small = grad_norm
|
| 31 |
-
tex_small = texture_norm
|
| 32 |
-
|
| 33 |
-
hsv = cv2.cvtColor(rgb_small, cv2.COLOR_RGB2HSV)
|
| 34 |
-
h_ch, s_ch, v_ch = cv2.split(hsv)
|
| 35 |
-
|
| 36 |
-
hue_mask = (h_ch >= 80) & (h_ch <= 140) # blues/cyans
|
| 37 |
-
sat_mask = s_ch > 40
|
| 38 |
-
val_mask = v_ch > 40
|
| 39 |
-
color_mask = hue_mask & sat_mask & val_mask
|
| 40 |
-
|
| 41 |
-
gray = cv2.cvtColor(rgb_small, cv2.COLOR_RGB2GRAY).astype(np.float32) / 255.0
|
| 42 |
-
gx = cv2.Sobel(gray, cv2.CV_32F, 1, 0, ksize=3)
|
| 43 |
-
gy = cv2.Sobel(gray, cv2.CV_32F, 0, 1, ksize=3)
|
| 44 |
-
texture = np.sqrt(gx * gx + gy * gy)
|
| 45 |
-
texture = cv2.GaussianBlur(texture, (0, 0), sigmaX=1.2, sigmaY=1.2)
|
| 46 |
-
if tex_small is None:
|
| 47 |
-
if texture.max() > texture.min():
|
| 48 |
-
tex_norm = (texture - texture.min()) / (np.ptp(texture) + 1e-6)
|
| 49 |
-
else:
|
| 50 |
-
tex_norm = np.zeros_like(texture)
|
| 51 |
-
else:
|
| 52 |
-
tex_norm = np.clip(tex_small.astype(np.float32), 0.0, 1.0)
|
| 53 |
-
low_texture = tex_norm < 0.35
|
| 54 |
-
|
| 55 |
-
depth_flat = grad_small < 0.08
|
| 56 |
-
|
| 57 |
-
mask = color_mask & low_texture & depth_flat
|
| 58 |
-
|
| 59 |
-
mask_uint = mask.astype(np.uint8)
|
| 60 |
-
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
|
| 61 |
-
try:
|
| 62 |
-
mask_uint = cv2.morphologyEx(mask_uint, cv2.MORPH_OPEN, kernel)
|
| 63 |
-
mask_uint = cv2.morphologyEx(mask_uint, cv2.MORPH_CLOSE, kernel)
|
| 64 |
-
mask_uint = cv2.dilate(mask_uint, kernel, iterations=1)
|
| 65 |
-
except Exception:
|
| 66 |
-
pass
|
| 67 |
-
|
| 68 |
-
if mask_uint.max() == 0:
|
| 69 |
-
return None
|
| 70 |
-
|
| 71 |
-
if (rgb_small.shape[0], rgb_small.shape[1]) != (h, w):
|
| 72 |
-
mask_uint = cv2.resize(mask_uint, (w, h), interpolation=cv2.INTER_NEAREST)
|
| 73 |
-
return mask_uint.astype(bool)
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
__all__ = ["heuristic_water_mask"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|