-
-
Save anonymous/71496f2b62fe5dcf45a7e14fd0fdeb05 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def animate_binary_plot(frames, k=2): | |
sm = np.copy(frames[0].signal_map) | |
signal_map_trace = go.Heatmap(z=sm, | |
) | |
real_binary_trace = go.Heatmap(z=np.float32(sm > 0), | |
) | |
sm_eq, _ = hist_eq(img=sm) | |
sm_eq_trace = go.Heatmap(z=sm_eq, | |
) | |
otsu_sm, _ = otsu_threshold(sm) | |
otsu_sm_eq, _ = otsu_threshold(sm_eq) | |
otsu_trace = go.Heatmap(z=otsu_sm, | |
) | |
otsu_eq_trace = go.Heatmap(z=otsu_sm_eq, | |
) | |
ad_sm = adaptive_threshold(sm) | |
ad_sm_eq = adaptive_threshold(sm_eq) | |
ad_trace = go.Heatmap(z=ad_sm, | |
) | |
ad_eq_trace = go.Heatmap(z=ad_sm_eq, | |
) | |
k_sm, _, _ = cluster(sm, k=k) | |
k_sm_eq, _, _ = cluster(sm_eq, k=k) | |
k_sm_trace = go.Heatmap(z=k_sm, | |
) | |
k_sm_eq_trace = go.Heatmap(z=k_sm_eq, | |
) | |
traces = [[otsu_trace, otsu_eq_trace, signal_map_trace], | |
[ad_trace, ad_eq_trace, sm_eq_trace], | |
[k_sm_trace, k_sm_eq_trace, real_binary_trace]] | |
for (x, y), trace in np.ndenumerate(traces): | |
trace.update(dict(colorscale='Rainbow', | |
opacity=1, | |
) | |
) | |
if np.amax(trace['z']) <= 2: | |
trace.update(dict(zauto=False, zmin=-0.1, zmax=2)) | |
position_color_bars(traces) | |
# Set up the slider | |
sliders = [dict(steps=[], | |
transition=dict(duration=50, | |
), | |
x=frames[0].timestamp, | |
y=0, | |
currentvalue=dict(font=dict(size=12, | |
), | |
prefix='Timestamp (ms): ', | |
visible=True, | |
xanchor='center', | |
# yanchor='middle', | |
# y=-0.1, | |
# x=-0.10 | |
), | |
len=1, | |
) | |
] | |
slider_steps = [] | |
for frame in frames: | |
step_dict = dict(args=[[frame.timestamp], | |
dict(frame=dict(duration=30, | |
# TODO: Set this to false, when using 2D scatter instead of heatmap. | |
redraw=True, | |
transition=dict(duration=0) | |
), | |
mode='immediate', | |
transition=dict(duration=0), | |
) | |
], | |
label=frame.timestamp, | |
method='animate', | |
) | |
slider_steps.append(step_dict) | |
sliders[0]['steps'] = slider_steps | |
# Create frames | |
plotly_frames = [] | |
for frame in frames: | |
sm = np.copy(frame.signal_map) | |
bi_sm = np.float32(sm > 0) | |
sm_eq, _ = hist_eq(img=sm) | |
otsu_sm, _ = otsu_threshold(sm) | |
otsu_sm_eq, _ = otsu_threshold(sm_eq) | |
ad_sm = adaptive_threshold(sm) | |
ad_sm_eq = adaptive_threshold(sm_eq) | |
k_sm, _, _ = cluster(sm, k=k) | |
k_sm_eq, _, _ = cluster(sm_eq, k=k) | |
frames_dict = dict(data=[dict(z=sm, | |
type='heatmap', | |
xaxis='x3', | |
yaxis='y3', | |
), | |
dict(z=sm_eq, | |
type='heatmap', | |
xaxis='x6', | |
yaxis='y6', | |
), | |
dict(z=bi_sm, | |
type='heatmap', | |
xaxis='x9', | |
yaxis='y9', | |
), | |
dict(z=otsu_sm, | |
type='heatmap', | |
xaxis='x1', | |
yaxis='y1', | |
), | |
dict(z=otsu_sm_eq, | |
type='heatmap', | |
xaxis='x2', | |
yaxis='y2', | |
), | |
dict(z=ad_sm, | |
type='heatmap', | |
xaxis='x4', | |
yaxis='y4', | |
), | |
dict(z=ad_sm_eq, | |
type='heatmap', | |
xaxis='x5', | |
yaxis='y5', | |
), | |
dict(z=k_sm, | |
type='heatmap', | |
xaxis='x7', | |
yaxis='y7', | |
), | |
dict(z=k_sm_eq, | |
type='heatmap', | |
xaxis='x8', | |
yaxis='y8', | |
), | |
], | |
traces=range(9), | |
name=frame.timestamp, | |
) | |
plotly_frames.append(frames_dict) | |
# Layout | |
size = 1000 | |
layout = go.Layout(title='Binarization', | |
# TODO: Add fixed size. | |
autosize=False, | |
width=size, | |
height=size, | |
updatemenus=[dict(type='buttons', | |
showactive=False, | |
xanchor='right', | |
yanchor='bottom', | |
pad=dict(t=0, | |
r=10, | |
), | |
buttons=[dict(label='Play', | |
method='animate', | |
args=[None, | |
dict( | |
frame=dict( | |
duration=30, | |
redraw=True, | |
), | |
transition=dict( | |
duration=0), | |
fromcurrent=True, | |
mode='immediate', | |
), | |
], | |
), | |
dict(label='Pause', | |
method='animate', | |
args=[[None], | |
dict( | |
frame=dict( | |
duration=0, | |
redraw=False, | |
), | |
mode='immediate', | |
transition=dict( | |
duration=0, | |
), | |
) | |
], | |
), | |
], | |
) | |
], | |
sliders=sliders, | |
xaxis1=dict(anchor='y1', | |
showgrid=True, | |
gridwidth=2, | |
tickmode='linear', | |
tick0=0.5, | |
dtick=1, | |
scaleanchor='y' | |
), | |
yaxis1 = dict(anchor='x1', | |
showgrid=True, | |
gridwidth=2, | |
tickmode='linear', | |
tick0=0.5, | |
dtick=1, | |
scaleanchor='x', | |
autorange='reversed', | |
) | |
) | |
xaxis = dict(anchor='y1', | |
showgrid=True, | |
gridwidth=2, | |
tickmode='linear', | |
tick0=0.5, | |
dtick=1, | |
scaleanchor='y1' | |
) | |
yaxis = dict(anchor='x1', | |
showgrid=True, | |
gridwidth=2, | |
tickmode='linear', | |
tick0=0.5, | |
dtick=1, | |
scaleanchor='x1', | |
autorange='reversed', | |
) | |
i = 1 | |
titles = ['Otsu', 'Otsu Eq', 'Signal Map', | |
'Local', 'Local Eq', 'SM Eq', | |
'K Mean', 'K Mean Eq', 'SM Binary'] | |
for (x, y), trace in np.ndenumerate(traces): | |
if i == 1: | |
i = i + 1 | |
continue | |
# trace['name'] = titles[i-1] | |
x_key = 'xaxis' + str(i) | |
y_key = 'yaxis' + str(i) | |
xaxis.update(dict(anchor='y' + str(i), | |
scaleanchor='y' + str(i), | |
) | |
) | |
yaxis.update(dict(anchor='x' + str(i), | |
scaleanchor='x' + str(i), | |
) | |
) | |
layout[x_key] = xaxis | |
layout[y_key] = yaxis | |
i = i+1 | |
figure = tools.make_subplots(rows=3, cols=3, | |
subplot_titles=titles, | |
) | |
figure['layout'].update(layout) | |
figure['frames'] = plotly_frames | |
for (x, y), trace in np.ndenumerate(traces): | |
figure.append_trace(trace, x + 1, y + 1) | |
py.plot(figure, filename='Binarisation.html') | |
return figure |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment