Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
instance_segmentation_classic
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Maciej Wielgosz
instance_segmentation_classic
Commits
8c96a2c4
Commit
8c96a2c4
authored
2 years ago
by
Maciej Wielgosz
Browse files
Options
Downloads
Patches
Plain Diff
remove small tiles
parent
9cb1dffa
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
nibio_preprocessing/remove_small_tiles.py
+37
-15
37 additions, 15 deletions
nibio_preprocessing/remove_small_tiles.py
with
37 additions
and
15 deletions
nibio_preprocessing/remove_small_tiles.py
+
37
−
15
View file @
8c96a2c4
...
@@ -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
]
}
w
ith densit
y
{
density
[
2
]
}
'
)
print
(
f
'
Removing
{
density
[
1
]
}
w
hich has onl
y
{
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
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment