Commit c99996d1 authored by Sean Fitzgibbon's avatar Sean Fitzgibbon
Browse files

Restored plots to chart_reg and fixed a bug

parent f94ac02e
......@@ -42,6 +42,15 @@ def register_chart_to_slide(chart, slide, slide_res, out, config=None):
rlevel = config['slide']['resolution_level']
boundary_key = config['chart']['boundary_key']
# set global variables required for summary plots
plots = config['general']['plots']
global DO_PLOTS
DO_PLOTS = plots
global OUTDIR
OUTDIR = out
# load chart
contour, cells = neurolucida.read(chart)
......@@ -53,7 +62,7 @@ def register_chart_to_slide(chart, slide, slide_res, out, config=None):
# initial scaling based on boundng boxes
edge_crds = contour[boundary_key][:, :2] * [1, -1]
init = _init_scale(img, slide_res, edge_crds, verbose=True, plots=False)
init = _init_scale(img, slide_res, edge_crds, verbose=True)
print(init)
print(
f'Rotation: {init.rotation}, Translation: {init.translation}, Scale: {init.scale}, Shear: {init.shear}')
......@@ -98,18 +107,15 @@ def _refine_edge_coord(img, img_res, edge_coords, normals):
"""
# calculate normal line (to edge_coords)
edge_x, edge_y = np.round(edge_coords.T).astype(int)
edge_x, edge_y = edge_coords.T
nrml_x, nrml_y = normals.T
line_smpl = np.arange(-20, 20) * img_res
# line_smpl = np.arange(-10, 10) * img_res
line_smpl = np.linspace(-0.03, 0.15, 20)
line_x = edge_x[:, np.newaxis] + nrml_x[:, np.newaxis] * line_smpl
line_y = edge_y[:, np.newaxis] + nrml_y[:, np.newaxis] * line_smpl
line_x = np.round(line_x / img_res).astype(int)
line_y = np.round(line_y / img_res).astype(int)
# find threshold for background
image = rgb2gray(img)
threshold_value = filters.threshold_otsu(image)
......@@ -129,22 +135,63 @@ def _refine_edge_coord(img, img_res, edge_coords, normals):
p = p[ridx]
cc0 = (cc == p.label)
# plt.imshow(cc0)
if DO_PLOTS:
fig, ax = plt.subplots(figsize=(20, 30))
extent = np.array([0, img.shape[1], img.shape[0], 0]) * img_res
ax.imshow(cc0, extent=extent)
for line_x0, line_y0 in zip(line_x, line_y):
ax.plot(line_x0, line_y0, 'b.')
ax.plot(edge_x, edge_y, 'r.')
ax.set_xlabel('mm')
ax.set_ylabel('mm')
ax.set_title('Largest connected component + normals')
fig.savefig(f'{OUTDIR}/normals.png', bbox_inches='tight', dpi=300)
# sample image along normal line
line_x = np.round(line_x / img_res).astype(int)
line_y = np.round(line_y / img_res).astype(int)
line_int = cc0[line_y, line_x]
min_idx = np.argmax(np.diff(line_int, axis=-1), axis=-1)
refined_edge_coords = np.stack([
line_x[np.arange(min_idx.size), min_idx],
line_y[np.arange(min_idx.size), min_idx],
line_x[np.arange(min_idx.size), min_idx] * img_res,
line_y[np.arange(min_idx.size), min_idx] * img_res,
], axis=-1)
# TODO: plot edge_coords + refined_edge_coords
if DO_PLOTS:
fig, ax = plt.subplots(figsize=(20, 30))
extent = np.array([0, img.shape[1], img.shape[0], 0]) * img_res
ax.imshow(img, extent=extent)
ax.plot(edge_x, edge_y, 'r.-')
ax.plot(refined_edge_coords[:, 0], refined_edge_coords[:, 1], 'g.-')
ax.set_xlabel('mm')
ax.set_ylabel('mm')
# ax.set_title('edge_co')
ax.legend(['edge_coords', 'refined_edge_coords'])
fig.savefig(f'{OUTDIR}/refined_coords.png', bbox_inches='tight', dpi=300)
return refined_edge_coords * img_res
def _img_props(img, img_resolution, verbose=False, plots=False):
def _img_props(img, img_resolution, verbose=False):
# convert to grayscale
image = rgb2gray(img)
......@@ -175,24 +222,31 @@ def _img_props(img, img_resolution, verbose=False, plots=False):
(bbox[1] + bbox[3])/2,
)
# TODO: make plots work again
# if plots:
if DO_PLOTS:
# plt.imshow(cc == p.label)
# plt.colorbar()
fig, ax = plt.subplots()
extent = np.array([0, img.shape[1], img.shape[0], 0]) * img_resolution
# rect = patches.Rectangle(
# (bbox[1], bbox[0]),
# bbox[3]-bbox[1],
# bbox[2]-bbox[0],
# linewidth=1,
# edgecolor='r',
# facecolor='none'
# )
# plt.gca().add_patch(rect)
im = ax.imshow(cc == p.label, extent=extent)
fig.colorbar(im)
# plt.plot(centroid[1], centroid[0], 'ro')
# plt.show()
rect = patches.Rectangle(
(bbox[1], bbox[0]),
bbox[3]-bbox[1],
bbox[2]-bbox[0],
linewidth=1,
edgecolor='r',
facecolor='none'
)
ax.add_patch(rect)
ax.plot(centroid[1], centroid[0], 'ro')
ax.set_xlabel('mm')
ax.set_ylabel('mm')
ax.set_title('Image bounding box')
fig.savefig(f'{OUTDIR}/image_bounding_box.png', bbox_inches='tight', dpi=150)
return {
'bbox': bbox,
......@@ -201,7 +255,7 @@ def _img_props(img, img_resolution, verbose=False, plots=False):
}
def _pnt_props(pnts, verbose=False, plots=False):
def _pnt_props(pnts, verbose=False):
x = pnts[:, 0]
y = pnts[:, 1]
......@@ -222,10 +276,13 @@ def _pnt_props(pnts, verbose=False, plots=False):
print(f'bbox={bbox}')
print(f'centroid={centroid}')
if plots:
plt.plot(x, y, 'b.')
plt.axis('equal')
plt.gca().invert_yaxis()
if DO_PLOTS:
fig, ax = plt.subplots()
ax.plot(x, y, 'b.')
ax.axis('equal')
ax.invert_yaxis()
rect = patches.Rectangle(
(bbox[1], bbox[0]),
......@@ -235,15 +292,18 @@ def _pnt_props(pnts, verbose=False, plots=False):
edgecolor='r',
facecolor='none'
)
plt.gca().add_patch(rect)
ax.add_patch(rect)
plt.plot(centroid[1], centroid[0], 'ro')
ax.set_title('Chart bounding box')
fig.savefig(f'{OUTDIR}/chart_bounding_box.png', bbox_inches='tight', dpi=150)
return {'bbox': bbox, 'bbox_centroid': centroid, 'shape': (bbox[2]-bbox[0], bbox[3]-bbox[1])}
def _init_scale(img, img_resolution, crd, verbose=False, plots=False):
def _init_scale(img, img_resolution, crd, verbose=False):
img_p = _img_props(img, img_resolution)
crd_p = _pnt_props(crd)
......
general:
# genarate summary plots of each stage
plots: True
chart:
# key for the contour to use for aligning with image
boundary_key: outline
......
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