Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Sean Fitzgibbon
SlideR 🍔
Commits
1aad409a
Commit
1aad409a
authored
Jun 17, 2021
by
Sean Fitzgibbon
Browse files
Tidied code
parent
23849806
Changes
2
Hide whitespace changes
Inline
Side-by-side
slider/chart_reg.py
View file @
1aad409a
...
...
@@ -16,9 +16,6 @@ import matplotlib.pyplot as plt
def
register_chart_to_slide
(
chart
,
slide
,
out
,
rlevel
=
3
,
boundary_key
=
'outline'
):
print
(
chart
)
print
(
slide
)
print
(
out
)
# load chart
contour
,
cells
=
neurolucida
.
read
(
chart
)
...
...
@@ -35,12 +32,46 @@ def register_chart_to_slide(chart, slide, out, rlevel=3, boundary_key='outline')
f
'Rotation:
{
init
.
rotation
}
, Translation:
{
init
.
translation
}
, Scale:
{
init
.
scale
}
, Shear:
{
init
.
shear
}
'
)
# calculate normal line to boundary points
# TODO: move to function
xfm_edge_coords
=
_apply_xfm
(
init
,
edge_crds
)
init_nrmls
=
normal
(
xfm_edge_coords
,
plot
=
False
,
distance
=
1
,
verbose
=
True
)
edge_x
,
edge_y
=
np
.
round
(
xfm_edge_coords
.
T
).
astype
(
int
)
nrml_x
,
nrml_y
=
init_nrmls
.
T
# refine edge_coords (to image)
refined_edge_coords
=
_refine_edge_coord
(
img
,
xfm_edge_coords
,
init_nrmls
)
# estimate opimised affine transform
opt
=
transform
.
AffineTransform
()
opt
.
estimate
(
edge_crds
,
refined_edge_coords
)
print
(
opt
)
print
(
f
'Rotation:
\t
{
opt
.
rotation
}
\n
Translation:
\t
{
opt
.
translation
}
\n
Scale:
\t\t
{
list
(
opt
.
scale
)
}
\n
Shear:
\t\t
{
opt
.
shear
}
'
)
# save opt transform
if
not
op
.
exists
(
out
):
os
.
makedirs
(
out
)
np
.
savetxt
(
f
'
{
out
}
/chart-to-image.xfm'
,
opt
.
params
)
# apply opt-xfm to contours and cells and save
contour_xfm
=
{
k
:
_apply_xfm
(
opt
,
v
[:,
:
2
]
*
[
1
,
-
1
]).
tolist
()
for
k
,
v
in
contour
.
items
()}
with
open
(
f
'
{
out
}
/contour.json'
,
'w'
)
as
fp
:
json
.
dump
(
contour_xfm
,
fp
,
indent
=
4
)
if
cells
.
size
>
0
:
cells_xfm
=
_apply_xfm
(
cells
[:,
:
2
]
*
[
1
,
-
1
])
with
open
(
f
'
{
out
}
/cells.json'
,
'w'
)
as
fp
:
json
.
dump
(
cells_xfm
,
fp
,
indent
=
4
)
def
_refine_edge_coord
(
img
,
edge_coords
,
normals
):
"""
Refine edge_coord by sampling binarised image along normal (to edge) and looking for big step change.
"""
# calculate normal line (to edge_coords)
edge_x
,
edge_y
=
np
.
round
(
edge_coords
.
T
).
astype
(
int
)
nrml_x
,
nrml_y
=
normals
.
T
line_smpl
=
np
.
arange
(
-
20
,
20
)
...
...
@@ -52,8 +83,6 @@ def register_chart_to_slide(chart, slide, out, rlevel=3, boundary_key='outline')
edge_y
[:,
np
.
newaxis
]
+
nrml_y
[:,
np
.
newaxis
]
*
line_smpl
).
astype
(
int
)
# --- refine edge coord by sampling along normal and looking for big change
# find threshold for background
image
=
rgb2gray
(
img
)
threshold_value
=
filters
.
threshold_otsu
(
image
)
...
...
@@ -84,30 +113,8 @@ def register_chart_to_slide(chart, slide, out, rlevel=3, boundary_key='outline')
line_y
[
np
.
arange
(
min_idx
.
size
),
min_idx
],
],
axis
=-
1
)
# estimate opimised affine transform
opt
=
transform
.
AffineTransform
()
opt
.
estimate
(
edge_crds
,
refined_edge_coords
)
print
(
opt
)
print
(
f
'Rotation:
\t
{
opt
.
rotation
}
\n
Translation:
\t
{
opt
.
translation
}
\n
Scale:
\t\t
{
list
(
opt
.
scale
)
}
\n
Shear:
\t\t
{
opt
.
shear
}
'
)
# save opt transform
if
not
op
.
exists
(
out
):
os
.
makedirs
(
out
)
np
.
savetxt
(
f
'
{
out
}
/chart-to-image.xfm'
,
opt
.
params
)
# apply opt-xfm to contours and cells and save
return
refined_edge_coords
contour_xfm
=
{
k
:
_apply_xfm
(
opt
,
v
[:,
:
2
]
*
[
1
,
-
1
]).
tolist
()
for
k
,
v
in
contour
.
items
()}
with
open
(
f
'
{
out
}
/contour.json'
,
'w'
)
as
fp
:
json
.
dump
(
contour_xfm
,
fp
,
indent
=
4
)
if
cells
.
size
>
0
:
cells_xfm
=
_apply_xfm
(
cells
[:,
:
2
]
*
[
1
,
-
1
])
with
open
(
f
'
{
out
}
/cells.json'
,
'w'
)
as
fp
:
json
.
dump
(
cells_xfm
,
fp
,
indent
=
4
)
def
_img_props
(
img
,
verbose
=
False
,
plots
=
False
):
...
...
slider_app.py
View file @
1aad409a
...
...
@@ -25,9 +25,6 @@ def add_slide_cli(subparsers):
parser
.
add_argument
(
"--config"
,
metavar
=
"<default.json>"
,
help
=
"configuration file"
,
default
=
'default.json'
,
type
=
str
,
required
=
False
)
# parser.add_argument("-v", "--verbose", default=False, action="store_true",
# help="Print status messages to the command line",
# required=False)
parser
.
set_defaults
(
method
=
'slide'
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment