Commit 1aad409a authored by Sean Fitzgibbon's avatar Sean Fitzgibbon
Browse files

Tidied code

parent 23849806
......@@ -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}\nTranslation:\t{opt.translation}\nScale:\t\t{list(opt.scale)}\nShear:\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}\nTranslation:\t{opt.translation}\nScale:\t\t{list(opt.scale)}\nShear:\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):
......
......@@ -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')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment