Fahimeh Orvati Nia
commited on
Commit
·
591c53e
1
Parent(s):
e4a9ce0
update
Browse files
sorghum_pipeline/data/preprocessor.py
CHANGED
|
@@ -1,9 +1,4 @@
|
|
| 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
|
|
@@ -27,34 +22,20 @@ 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
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 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 |
"""
|
|
|
|
| 1 |
+
"""Minimal image preprocessing following the requested composite/spectral logic."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
|
|
|
| 22 |
return norm.astype(np.uint8)
|
| 23 |
|
| 24 |
def process_raw_image(self, pil_img: Image.Image) -> Tuple[np.ndarray, Dict[str, np.ndarray]]:
|
| 25 |
+
"""Split the 4-band RAW into tiles and build composite + spectral stack."""
|
| 26 |
+
d = pil_img.size[0] // 2
|
| 27 |
+
boxes = [(j, i, j + d, i + d)
|
| 28 |
+
for i, j in product(range(0, pil_img.height, d),
|
| 29 |
+
range(0, pil_img.width, d))]
|
| 30 |
+
stack = np.stack([np.array(pil_img.crop(b), float) for b in boxes], axis=-1)
|
| 31 |
+
# bands come in order: [green, red, red_edge, nir]
|
| 32 |
+
green, red, red_edge, nir = np.split(stack, 4, axis=-1)
|
| 33 |
+
# build composite using bands: Red, Red-Edge, Green
|
| 34 |
+
# Note: OpenCV expects BGR ordering when saving/displaying.
|
| 35 |
+
# To achieve displayed RGB = (red, red_edge, green), we arrange BGR = (green, red_edge, red).
|
| 36 |
+
comp = np.concatenate([green, red_edge, red], axis=-1)
|
| 37 |
+
comp8 = self.convert_to_uint8(comp)
|
| 38 |
+
return comp8, {"green": green, "red": red, "red_edge": red_edge, "nir": nir}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
def create_composites(self, plants: Dict[str, Dict[str, Any]]) -> Dict[str, Dict[str, Any]]:
|
| 41 |
"""
|