Commit 9028e626 authored by Gaetano Raffaele's avatar Gaetano Raffaele
Browse files

ENH: tentative tiled vectorization.

parent 2af79e43
No related merge requests found
Showing with 57 additions and 1 deletion
+57 -1
import sys import sys
import numpy as np
from Common.otb_numpy_proc import to_otb_pipeline from Common.otb_numpy_proc import to_otb_pipeline
import os.path import os.path
import otbApplication as otb import otbApplication as otb
...@@ -7,6 +10,7 @@ from math import sqrt, floor ...@@ -7,6 +10,7 @@ from math import sqrt, floor
from itertools import product, chain from itertools import product, chain
from dataclasses import dataclass from dataclasses import dataclass
import psutil import psutil
from skimage.measure import regionprops
# GLOBAL # GLOBAL
node_size = 700 # size of a graph node (=pixel) in GRM 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): ...@@ -182,4 +186,56 @@ def write_qgis_seg_style(out_file, line_color='255,255,0,255', line_width=0.46):
" <layerGeometryType>2</layerGeometryType>", " <layerGeometryType>2</layerGeometryType>",
"</qgis>"] "</qgis>"]
) )
return out_file return out_file
\ No newline at end of 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
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment