Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SVLT
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
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
ALMF
SVLT
Commits
c02b572a
Commit
c02b572a
authored
1 year ago
by
Christopher Randolph Rhodes
Browse files
Options
Downloads
Patches
Plain Diff
Separate method to mask out all but largest object
parent
9c68d8df
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
extensions/chaeo/process.py
+38
-0
38 additions, 0 deletions
extensions/chaeo/process.py
extensions/chaeo/tests/test_process.py
+21
-0
21 additions, 0 deletions
extensions/chaeo/tests/test_process.py
with
59 additions
and
0 deletions
extensions/chaeo/process.py
0 → 100644
+
38
−
0
View file @
c02b572a
import
numpy
as
np
from
skimage.measure
import
label
,
regionprops_table
def
mask_largest_object
(
img
:
np
.
ndarray
,
max_allowed
:
int
=
10
,
verbose
:
bool
=
True
)
->
np
.
ndarray
:
"""
Where more than one connected component is found in an image, return the largest object by area
:param img: (np.ndarray) containing object labels
:param max_allowed: raise an error if more than this number of objects is found
:param verbose: print a message each time more than one object is found
:return: np.ndarray of same size as img
"""
binary
=
img
>
0
ob_id
=
label
(
binary
)
num_obj
=
len
(
np
.
unique
(
ob_id
))
-
1
if
num_obj
>
max_allowed
:
raise
TooManyObjectError
(
f
'
Found
{
num_obj
}
objects in frame
'
)
if
num_obj
>
1
:
if
verbose
:
print
(
f
'
Found
{
num_obj
}
nonzero unique values in object map; keeping the one with the largest area
'
)
pr
=
regionprops_table
(
ob_id
,
properties
=
[
'
label
'
,
'
area
'
])
idx_max_area
=
pr
[
'
area
'
].
argmax
()
mask
=
ob_id
==
pr
[
'
label
'
][
idx_max_area
]
return
mask
*
img
else
:
return
img
class
Error
(
Exception
):
pass
class
TooManyObjectError
(
Exception
):
pass
This diff is collapsed.
Click to expand it.
extensions/chaeo/tests/test_process.py
0 → 100644
+
21
−
0
View file @
c02b572a
import
unittest
import
numpy
as
np
from
extensions.chaeo.process
import
mask_largest_object
class
TestMaskLargestObject
(
unittest
.
TestCase
):
def
test_mask_largest_object
(
self
):
arr
=
np
.
zeros
([
5
,
5
])
arr
[
0
:
3
,
0
:
3
]
=
2
arr
[
4
,
2
:
5
]
=
4
masked
=
mask_largest_object
(
arr
)
self
.
assertTrue
(
np
.
all
(
np
.
unique
(
masked
)
==
[
0
,
2
]))
self
.
assertTrue
(
np
.
all
(
masked
[
4
:
5
,
:]
==
0
))
self
.
assertTrue
(
np
.
all
(
masked
[:,
4
:
5
]
==
0
))
def
test_no_change
(
self
):
arr
=
np
.
zeros
([
5
,
5
])
arr
[
0
:
3
,
0
:
3
]
=
2
masked
=
mask_largest_object
(
arr
)
self
.
assertTrue
(
np
.
all
(
masked
==
arr
))
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