%% backup the current results before merging 
neuron_bk = neuron.copy();
ctr = neuron.estCenter(); 
K = size(neuron.A,2);   %neuron numbers 
ind_del = false(K, 1);   
Coor = neuron.get_contours(); 
[img, col0, AA] = neuron.overlapA([], 0.1);  % overlap neurons
color_all = zeros(K,3);
col = col0; 
for m=1:3
color_all(:,m) = mod(col,2); 
col = floor(col/2); 
end

%% UI CONTROL 
IDs = []; 
fig_merge = figure('position', [10, 300, 1400, 500], 'papersize', [14, 5]); 
ax_all = axes('position', [0.005 0.1, 0.4, 0.8]); 
axes(ax_all); 
imagesc(img, 'parent', ax_all);  hold on; 
axis equal off tight; 
pb_select = uicontrol(fig_merge, 'style', 'pushbutton', ...
    'unit', 'normalized',  'position', [0.05, 0.01, 0.1, 0.08], ...
    'string', 'add neurons', 'fontweight', 'bold', ...
    'fontsize', 16,     'callback', 'cnmfe_manual_merge_pick_neuron;'); 
pb_zoom = uicontrol(fig_merge, 'style', 'pushbutton', ...
    'unit', 'normalized',  'position', [0.16, 0.01, 0.1, 0.08], ...
    'string', 'zoom out', 'fontweight', 'bold', ...
    'fontsize', 16,     'callback', 'axes(ax_all); cla; imagesc(img); axis tight;'); 
pb_finish = uicontrol(fig_merge, 'style', 'pushbutton', ...
    'unit', 'normalized',  'position', [0.27, 0.01, 0.1, 0.08], ...
    'string', 'FINISH', 'fontweight', 'bold', ...
    'fontsize', 16,     'callback', 'neuron.delete(ind_del); close(gcf); '); 

ax_selected = axes('position', [0.41, 0.6, 0.15, 0.35]); 
ax_trace = axes('position', [0.6, 0.55, 0.35, 0.4]); 
ax_merged = axes('position', [0.41, 0.05, 0.15, 0.35]); 
ax_merged_trace = axes('position', [0.6, 0.05, 0.35, 0.4]); 

%%
pb_remove = uicontrol(fig_merge, 'style', 'pushbutton', ...
    'unit', 'normalized',  'position', [.41, 0.5, 0.08, 0.08], ...
    'string', 'remove', 'fontweight', 'bold', ...
    'fontsize', 16, ...
    'callback', 'cnmfe_manual_merge_remove_neuron;'); 
pb_merge = uicontrol(fig_merge, 'style', 'pushbutton', ...
    'unit', 'normalized',  'position', [.49, 0.5, 0.08, 0.08], ...
    'string', 'merge', 'fontweight', 'bold', ...
    'fontsize', 16, ...
    'callback', 'cnmfe_manual_merge_merge_neurons;'); 
pb_reverse = uicontrol(fig_merge, 'style', 'pushbutton', ...
    'unit', 'normalized',  'position', [.41, 0.42, 0.08, 0.08], ...
    'string', 'cancel', 'fontweight', 'bold', ...
    'fontsize', 16, ...
    'callback', 'cnmfe_manual_merge_cancel;'); 
pb_reverse = uicontrol(fig_merge, 'style', 'pushbutton', ...
    'unit', 'normalized',  'position', [.49, 0.42, 0.08, 0.08], ...
    'string', 'NEW', 'fontweight', 'bold', ...
    'fontsize', 16, ...
    'callback', 'IDs=[]; axes(ax_selected); cla; axes(ax_trace); cla; '); 