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
6cf4f1c0
Commit
6cf4f1c0
authored
1 year ago
by
Christopher Randolph Rhodes
Browse files
Options
Downloads
Patches
Plain Diff
Overloaded mask_largest_object to include both object maps and binary masks as inputs
parent
d2b233d0
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
extensions/chaeo/process.py
+12
-8
12 additions, 8 deletions
extensions/chaeo/process.py
extensions/chaeo/tests/test_process.py
+21
-10
21 additions, 10 deletions
extensions/chaeo/tests/test_process.py
with
33 additions
and
18 deletions
extensions/chaeo/process.py
+
12
−
8
View file @
6cf4f1c0
import
numpy
as
np
import
numpy
as
np
from
skimage.measure
import
label
,
regionprops_tabl
e
import
skimag
e
from
model_server.process
import
is_mask
def
mask_largest_object
(
def
mask_largest_object
(
img
:
np
.
ndarray
,
img
:
np
.
ndarray
,
...
@@ -9,14 +10,15 @@ def mask_largest_object(
...
@@ -9,14 +10,15 @@ def mask_largest_object(
)
->
np
.
ndarray
:
)
->
np
.
ndarray
:
"""
"""
Where more than one connected component is found in an image, return the largest object by area
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 img: (np.ndarray) containing object labels
or binary mask
:param max_allowed: raise an error if more than this number of objects is found
: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
:param verbose: print a message each time more than one object is found
:return: np.ndarray of same size as img
:return: np.ndarray of same size as img
"""
"""
binary
=
img
>
0
if
is_mask
(
img
):
# assign object labels
ob_id
=
label
(
binary
)
ob_id
=
skimage
.
measure
.
label
(
img
)
# separate problem for mask and object maps
else
:
# assume img is contains object labels
ob_id
=
img
# import skimage
# import skimage
# from pathlib import Path
# from pathlib import Path
...
@@ -28,9 +30,11 @@ def mask_largest_object(
...
@@ -28,9 +30,11 @@ def mask_largest_object(
if
num_obj
>
1
:
if
num_obj
>
1
:
if
verbose
:
if
verbose
:
print
(
f
'
Found
{
num_obj
}
nonzero unique values in object map; keeping the one with the largest area
'
)
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
'
])
# pr = regionprops_table(ob_id, properties=['label', 'area'])
idx_max_area
=
pr
[
'
area
'
].
argmax
()
val
,
cts
=
np
.
unique
(
ob_id
,
return_counts
=
True
)
mask
=
ob_id
==
pr
[
'
label
'
][
idx_max_area
]
mask
=
ob_id
==
val
[
1
+
cts
[
1
:].
argmax
()]
# idx_max_area = pr['area'].argmax()
# mask = ob_id == pr['label'][idx_max_area]
return
mask
*
img
return
mask
*
img
else
:
else
:
return
img
return
img
...
...
This diff is collapsed.
Click to expand it.
extensions/chaeo/tests/test_process.py
+
21
−
10
View file @
6cf4f1c0
...
@@ -5,17 +5,28 @@ import numpy as np
...
@@ -5,17 +5,28 @@ import numpy as np
from
extensions.chaeo.process
import
mask_largest_object
from
extensions.chaeo.process
import
mask_largest_object
class
TestMaskLargestObject
(
unittest
.
TestCase
):
class
TestMaskLargestObject
(
unittest
.
TestCase
):
def
test_mask_largest_object
(
self
):
def
test_mask_largest_
touching_
object
(
self
):
arr
=
np
.
zeros
([
5
,
5
])
arr
=
np
.
zeros
([
5
,
5
]
,
dtype
=
'
uint8
'
)
arr
[
0
:
3
,
0
:
3
]
=
2
arr
[
0
:
3
,
0
:
3
]
=
2
arr
[
4
,
2
:
5
]
=
4
arr
[
3
:
,
2
:]
=
4
masked
=
mask_largest_object
(
arr
)
masked
=
mask_largest_object
(
arr
)
self
.
assertTrue
(
np
.
all
(
np
.
unique
(
masked
)
==
[
0
,
2
]))
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
:
2
]
==
0
))
self
.
assertTrue
(
np
.
all
(
masked
[
:
,
4
:
5
]
==
0
))
self
.
assertTrue
(
np
.
all
(
masked
[
0
:
3
,
3
:
5
]
==
0
))
def
test_no_change
(
self
):
def
test_no_change
(
self
):
arr
=
np
.
zeros
([
5
,
5
])
arr
=
np
.
zeros
([
5
,
5
]
,
dtype
=
'
uint8
'
)
arr
[
0
:
3
,
0
:
3
]
=
2
arr
[
0
:
3
,
0
:
3
]
=
2
masked
=
mask_largest_object
(
arr
)
masked
=
mask_largest_object
(
arr
)
self
.
assertTrue
(
np
.
all
(
masked
==
arr
))
self
.
assertTrue
(
np
.
all
(
masked
==
arr
))
def
test_mask_multiple_objects_in_binary_maks
(
self
):
arr
=
np
.
zeros
([
5
,
5
],
dtype
=
'
uint8
'
)
arr
[
0
:
3
,
0
:
3
]
=
255
arr
[
4
,
2
:
5
]
=
255
masked
=
mask_largest_object
(
arr
)
print
(
np
.
unique
(
masked
))
self
.
assertTrue
(
np
.
all
(
np
.
unique
(
masked
)
==
[
0
,
255
]))
self
.
assertTrue
(
np
.
all
(
masked
[:,
3
:
5
]
==
0
))
self
.
assertTrue
(
np
.
all
(
masked
[
3
:
5
,
:]
==
0
))
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