Fahimeh Orvati Nia
commited on
Commit
·
e4a9ce0
1
Parent(s):
92eda31
update
Browse files
sorghum_pipeline/data/preprocessor.py
CHANGED
|
@@ -1,14 +1,14 @@
|
|
| 1 |
"""Minimal image preprocessing following the requested composite/spectral logic.
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 5 |
"""
|
| 6 |
|
| 7 |
import numpy as np
|
| 8 |
from PIL import Image
|
| 9 |
from typing import Dict, Tuple, Any
|
| 10 |
from itertools import product
|
| 11 |
-
from src.composite import process_raw_image as src_process_raw_image
|
| 12 |
|
| 13 |
|
| 14 |
class ImagePreprocessor:
|
|
@@ -27,9 +27,34 @@ class ImagePreprocessor:
|
|
| 27 |
return norm.astype(np.uint8)
|
| 28 |
|
| 29 |
def process_raw_image(self, pil_img: Image.Image) -> Tuple[np.ndarray, Dict[str, np.ndarray]]:
|
| 30 |
-
"""
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
def create_composites(self, plants: Dict[str, Dict[str, Any]]) -> Dict[str, Dict[str, Any]]:
|
| 35 |
"""
|
|
|
|
| 1 |
"""Minimal image preprocessing following the requested composite/spectral logic.
|
| 2 |
|
| 3 |
+
Self-contained: builds composite/spectral directly (no external imports).
|
| 4 |
+
Assumes a 2x2 mosaic: TL, TR, BL, BR → green, red, red_edge, nir.
|
| 5 |
+
Composite BGR order: (green, red_edge, red) so displayed RGB = (red, red_edge, green).
|
| 6 |
"""
|
| 7 |
|
| 8 |
import numpy as np
|
| 9 |
from PIL import Image
|
| 10 |
from typing import Dict, Tuple, Any
|
| 11 |
from itertools import product
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
class ImagePreprocessor:
|
|
|
|
| 27 |
return norm.astype(np.uint8)
|
| 28 |
|
| 29 |
def process_raw_image(self, pil_img: Image.Image) -> Tuple[np.ndarray, Dict[str, np.ndarray]]:
|
| 30 |
+
"""Split 4-band mosaic, return 8-bit BGR composite and spectral dict."""
|
| 31 |
+
# Determine quadrant size
|
| 32 |
+
d_w = pil_img.size[0] // 2
|
| 33 |
+
d_h = pil_img.size[1] // 2
|
| 34 |
+
boxes = [
|
| 35 |
+
(0, 0, d_w, d_h), # TL → green
|
| 36 |
+
(d_w, 0, d_w * 2, d_h), # TR → red
|
| 37 |
+
(0, d_h, d_w, d_h * 2), # BL → red_edge
|
| 38 |
+
(d_w, d_h, d_w * 2, d_h * 2), # BR → nir
|
| 39 |
+
]
|
| 40 |
+
# Extract each quadrant as grayscale float
|
| 41 |
+
quads = [
|
| 42 |
+
np.array(pil_img.crop(b).convert('L'), dtype=float)
|
| 43 |
+
for b in boxes
|
| 44 |
+
]
|
| 45 |
+
green, red, red_edge, nir = quads
|
| 46 |
+
|
| 47 |
+
# Build composite in BGR order: (green, red_edge, red)
|
| 48 |
+
comp = np.dstack([green, red_edge, red])
|
| 49 |
+
comp_uint8 = self.convert_to_uint8(comp)
|
| 50 |
+
|
| 51 |
+
spectral_bands = {
|
| 52 |
+
"green": green[..., np.newaxis],
|
| 53 |
+
"red": red[..., np.newaxis],
|
| 54 |
+
"red_edge": red_edge[..., np.newaxis],
|
| 55 |
+
"nir": nir[..., np.newaxis],
|
| 56 |
+
}
|
| 57 |
+
return comp_uint8, spectral_bands
|
| 58 |
|
| 59 |
def create_composites(self, plants: Dict[str, Dict[str, Any]]) -> Dict[str, Dict[str, Any]]:
|
| 60 |
"""
|