From 9028e62637a6e6f7344db82caa3819b412948bd0 Mon Sep 17 00:00:00 2001
From: "raffaele.gaetano" <raffaele.gaetano@cirad.fr>
Date: Wed, 27 Jul 2022 18:48:47 +0200
Subject: [PATCH] ENH: tentative tiled vectorization.

---
 VHR/segmentation.py | 58 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 57 insertions(+), 1 deletion(-)

diff --git a/VHR/segmentation.py b/VHR/segmentation.py
index fcdd61b..31a247f 100644
--- a/VHR/segmentation.py
+++ b/VHR/segmentation.py
@@ -1,4 +1,7 @@
 import sys
+
+import numpy as np
+
 from Common.otb_numpy_proc import to_otb_pipeline
 import os.path
 import otbApplication as otb
@@ -7,6 +10,7 @@ from math import sqrt, floor
 from itertools import product, chain
 from dataclasses import dataclass
 import psutil
+from skimage.measure import regionprops
 
 # GLOBAL
 node_size = 700 # size of a graph node (=pixel) in GRM
@@ -182,4 +186,56 @@ def write_qgis_seg_style(out_file, line_color='255,255,0,255', line_width=0.46):
              "  <layerGeometryType>2</layerGeometryType>",
              "</qgis>"]
         )
-        return out_file
\ No newline at end of file
+        return out_file
+
+def vectorize_tile(obj, startx, starty, sizex, sizey, out):
+    print('Using startx {} and starty {}'.format(startx, starty))
+    r = otb.itkRegion()
+    r['index'][0], r['index'][1] = startx, starty
+    r['size'][0], r['size'][1] = sizex, sizey
+    obj.PropagateRequestedRegion('out', r)
+    clip = obj.ExportImage('out')
+
+    min_bb_x = 0
+    min_bb_y = 0
+
+    rp = regionprops(np.squeeze(clip['array'].astype(np.int)))
+    for o in rp:
+        if (starty > 0 and o.bbox[0] == 0) or (startx > 0 and o.bbox[1] == 0):
+            clip['array'][clip['array'] == o.label] = 0
+        if o.bbox[2] > sizey - 1 or o.bbox[3] > sizex - 1:
+            clip['array'][clip['array'] == o.label] = 0
+            if o.bbox[3] > sizex - 1 and sizex - o.bbox[1] > min_bb_x:
+                min_bb_x = sizex - o.bbox[1]
+            if o.bbox[2] > sizey - 1 and sizey - o.bbox[0] > min_bb_y:
+                min_bb_y = sizey - o.bbox[0]
+
+    vec = otb.Registry.CreateApplication('SimpleVectorization')
+    vec.ImportImage('in', clip)
+    vec.SetParameterString('out', out)
+    vec.ExecuteAndWriteOutput()
+
+    print('Returning min_bb_x {} and min_bb_y {}'.format(min_bb_x, min_bb_y))
+
+    return min_bb_x, min_bb_y
+
+def tiled_vectorization(input_segm, nominal_tile_size, output_template):
+    obj_to_tile_map = {}
+    in_seg = to_otb_pipeline(input_segm)
+    W,H = in_seg.GetImageSize('out')
+    idx = 0
+    vec_list = []
+    min_bb_x, min_bb_y = 0, 0
+    for x in range(0,W,nominal_tile_size[0]):
+        x = max(0, x - min_bb_x - 1)
+        tw = min(nominal_tile_size[0] + min_bb_x + 1, W - x)
+        min_bb_x = 0
+        for y in range(0, H, nominal_tile_size[1]):
+            y = max(0, y - min_bb_y - 1)
+            th = min(nominal_tile_size[1]+min_bb_y+1, H-y)
+            fn = output_template.format(idx)
+            min_bb_x_tmp, min_bb_y = vectorize_tile(in_seg, x, y, tw, th, fn)
+            min_bb_x = max(min_bb_x, min_bb_x_tmp)
+            vec_list.append(fn)
+            idx += 1
+
-- 
GitLab