Commit 557499bd authored by Sean Fitzgibbon's avatar Sean Fitzgibbon
Browse files

Added alignment plot + bugfix

parent c99996d1
......@@ -69,7 +69,7 @@ def register_chart_to_slide(chart, slide, slide_res, out, config=None):
# calculate normal line to boundary points
xfm_edge_coords = _apply_xfm(init, edge_crds)
init_nrmls = normal(xfm_edge_coords, plot=False, distance=1, verbose=True)
init_nrmls = normal(xfm_edge_coords)
# refine edge_coords (to image)
refined_edge_coords = _refine_edge_coord(img, slide_res, xfm_edge_coords, init_nrmls)
......@@ -92,6 +92,28 @@ def register_chart_to_slide(chart, slide, slide_res, out, config=None):
contour_xfm = {
k: _apply_xfm(opt, v[:, :2] * [1, -1]).tolist() for k, v in contour.items()
}
if DO_PLOTS:
fig, ax = plt.subplots(figsize=(10, 10))
extent = np.array([0, img.shape[1], img.shape[0], 0]) * slide_res
ax.imshow(img, extent=extent)
for name, coords in contour_xfm.items():
coords = np.array(coords)
cog = np.mean(coords,axis=0)
ax.text(cog[0],cog[1],name)
ax.plot(coords[:,0],coords[:,1],'k-')
ax.set_xlabel('mm')
ax.set_ylabel('mm')
ax.set_title('Aligned Chart')
fig.savefig(f'{OUTDIR}/aligned_chart.png', bbox_inches='tight', dpi=300)
with open(f'{out}/contour.json', 'w') as fp:
json.dump(contour_xfm, fp, indent=4)
......@@ -110,7 +132,7 @@ def _refine_edge_coord(img, img_res, edge_coords, normals):
edge_x, edge_y = edge_coords.T
nrml_x, nrml_y = normals.T
# line_smpl = np.arange(-10, 10) * img_res
# TODO: move these line extents to the config file
line_smpl = np.linspace(-0.03, 0.15, 20)
line_x = edge_x[:, np.newaxis] + nrml_x[:, np.newaxis] * line_smpl
......@@ -184,11 +206,10 @@ def _refine_edge_coord(img, img_res, edge_coords, normals):
ax.legend(['edge_coords', 'refined_edge_coords'])
fig.savefig(f'{OUTDIR}/refined_coords.png', bbox_inches='tight', dpi=300)
fig.savefig(f'{OUTDIR}/refined_coords.png', bbox_inches='tight', dpi=150)
return refined_edge_coords * img_res
return refined_edge_coords
def _img_props(img, img_resolution, verbose=False):
......@@ -327,7 +348,7 @@ def _apply_xfm(xfm, pnts):
return (xfm.params @ np.concatenate((pnts, np.ones((pnts.shape[0], 1))), axis=-1).T).T[:, :2]
def normal(pnts, distance=100, plot=False, verbose=False):
def normal(pnts):
d_pnts = np.roll(pnts, 1, axis=0) - np.roll(pnts, -1, axis=0)
normal = np.stack([d_pnts[:, 1], -d_pnts[:, 0]], axis=-1)
normal = normal / np.linalg.norm(normal, axis=-1, keepdims=True)
......
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