Skip to content
Snippets Groups Projects
Commit 8c96a2c4 authored by Maciej Wielgosz's avatar Maciej Wielgosz
Browse files

remove small tiles

parent 9cb1dffa
No related branches found
No related tags found
No related merge requests found
...@@ -5,13 +5,19 @@ import plyfile ...@@ -5,13 +5,19 @@ import plyfile
class RemoveSmallTiles(): class RemoveSmallTiles():
"""
Works with ply files.
"""
def __init__(self, dir, min_density, tile_index_file, verbose=False): def __init__(self, dir, min_density, tile_index_file, verbose=False):
self.dir = dir self.dir = dir
self.min_size = min_density self.min_size = min_density
self.tile_index_file = tile_index_file self.tile_index_file = tile_index_file
self.verbose = verbose self.verbose = verbose
def get_density_of_single_tile(self, tile_path): def get_density_and_points_nr_of_single_tile(self, tile_path):
"""
Get the density of a single tile.
"""
plydata = plyfile.PlyData.read(tile_path) plydata = plyfile.PlyData.read(tile_path)
volume = (plydata['vertex']['x'].max() - plydata['vertex']['x'].min()) \ volume = (plydata['vertex']['x'].max() - plydata['vertex']['x'].min()) \
* (plydata['vertex']['y'].max() - plydata['vertex']['y'].min()) \ * (plydata['vertex']['y'].max() - plydata['vertex']['y'].min()) \
...@@ -20,7 +26,7 @@ class RemoveSmallTiles(): ...@@ -20,7 +26,7 @@ class RemoveSmallTiles():
number_of_points = plydata['vertex'].count number_of_points = plydata['vertex'].count
density = number_of_points / volume density = number_of_points / volume
return density return density, number_of_points
@staticmethod @staticmethod
def remove_line_from_csv(path, line): def remove_line_from_csv(path, line):
...@@ -35,49 +41,65 @@ class RemoveSmallTiles(): ...@@ -35,49 +41,65 @@ class RemoveSmallTiles():
f.write(lines[i]) f.write(lines[i])
def get_density_of_all_tiles(self): def get_density_of_all_tiles(self):
# read all the ply files in the folder """
Get the density of all tiles in the directory.
"""
files = glob.glob(os.path.join(self.dir, "*.ply")) files = glob.glob(os.path.join(self.dir, "*.ply"))
if self.verbose: if self.verbose:
print(f'Found {len(files)} tiles') print(f'Found {len(files)} tiles')
# compute the density of each tile and put it in a dictionary toghehter with the file path # compute the density of each tile and put it in a dictionary toghehter with the file path
densities = {} densities_and_point_nr = {}
for i, ply in enumerate(files): for i, ply in enumerate(files):
# get a fine name without the path and suffix # get a fine name without the path and suffix
file_name = os.path.split(ply)[1].split('.')[0] file_name = os.path.split(ply)[1].split('.')[0]
# full path to the tile # full path to the tile
tile_path = os.path.join(self.dir, file_name + '.ply') tile_path = os.path.join(self.dir, file_name + '.ply')
# get the density of the tile # get the density of the tile
density = self.get_density_of_single_tile(ply) density = self.get_density_and_points_nr_of_single_tile(ply)
# put it into a tuple together with the file name # put it into a tuple together with the file name
densities[i] = (file_name, tile_path, density) densities_and_point_nr[i] = (file_name, tile_path, density[0], density[1])
if self.verbose: if self.verbose:
print(f'Computed density of all tiles') print(f'Computed density of all tiles')
return densities return densities_and_point_nr
def remove_small_tiles(self): def remove_small_tiles(self):
"""
Remove tiles that are too small.
"""
# get the density of all tiles # get the density of all tiles
densities = self.get_density_of_all_tiles() densities_and_point_nr = self.get_density_of_all_tiles()
# remove the tiles that are too small # remove the tiles that whicha are too small (points per m^3)
for i, density in densities.items(): for i, density in densities_and_point_nr.items():
if density[2] < self.min_size: if density[3] < 10000:
if self.verbose: if self.verbose:
print(f'Removing tile {density[0]} with density {density[2]}') print(f'Removing {density[1]} which has only {density[3]} points')
os.remove(density[1]) os.remove(density[1])
self.remove_line_from_csv(self.tile_index_file, i) self.remove_line_from_csv(self.tile_index_file, i)
# remove the tiles that of a small density
for i, density in densities_and_point_nr.items():
if density[2] < self.min_size:
if self.verbose:
print(f'Removing tile {density[0]} with density {density[2]}')
# check if the tile is already removed
if os.path.exists(density[1]):
os.remove(density[1])
self.remove_line_from_csv(self.tile_index_file, i)
# if verbose print the lowest and the highest density along with the tile name # if verbose print the lowest and the highest density along with the tile name
if self.verbose: if self.verbose:
print(f'Lowest density: {min(densities.values(), key=lambda x: x[2])}') print(f'Lowest density: {min(densities_and_point_nr.values(), key=lambda x: x[2])}')
print(f'Highest density: {max(densities.values(), key=lambda x: x[2])}') print(f'Highest density: {max(densities_and_point_nr.values(), key=lambda x: x[2])}')
# if verbose print the number of tiles that were removed # if verbose print the number of tiles that were removed
if self.verbose: if self.verbose:
print(f'Removed {len(densities) - len(glob.glob(os.path.join(self.dir, "*.ply")))} tiles') print(f'Removed {len(densities_and_point_nr) - len(glob.glob(os.path.join(self.dir, "*.ply")))} tiles')
def main(self): def main(self):
self.remove_small_tiles() self.remove_small_tiles()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment