% Code to remove analysis points that correlated incorrectly
% Programmed by Rob
% Last revision: 4/13/2007
clear all;
clc;
% Close the current figure if open and caution user
caut_selection = menu(sprintf('Caution: After this prompt, Matlab may appear unresponsive for a few seconds while the data is being loaded and analyzed.\n Please be patient. If no results are shown after 30 seconds, then use Control-C to restart the analysis.'),'OK');
close(gcf);
% Load, format, and plot the raw correlation results
load validx.txt;
load validy.txt;
[num_points num_images] = size(validx);
validx_vector = reshape(validx,num_points*num_images,1); % Convert validx to a one-column vector for plotting
validy_vector = reshape(validy,num_points*num_images,1); % Convert validy to a one-column vector for plotting
set(gcf,'Position',[150,50,800,600]); % [left bottom width height]
plot(validx_vector,validy_vector,'.r');
axis image;
xlabel('x-Location on Image [Pixels]');
ylabel('y-Location on Image [Pixels]');
% Ask user what to remove
selection1 = menu(sprintf('Would you like to remove a group of bad correlation points (located together) or remove bad correlation points one at a time?'),'Remove Group','Remove One At A Time');
% Delete a group of markers
if selection1 == 1;
title(sprintf('Define the region of interest. Pick (single click) a location in the UPPER LEFT region\n of the plotted results first. Then, do the same for a location in the LOWER RIGHT region.\n ALL POINTS OUTSIDE THE SELECTED RECTANGLE WILL BE DELETED!!!'));
[xgrid ygrid] = ginput(2);
x(1,1) = xgrid(1);
x(1,2) = xgrid(2);
y(1,1) = ygrid(2);
y(1,2) = ygrid(1);
% Find the indicies of the points to be saved, which are inside the selected rectangle, by searching through the first image (the inital grid) only
[good_i good_j] = find(validx(:,1)>min(x) & validx(:,1)<max(x) & validy(:,1)>min(y) & validy(:,1)<max(y));
% Extract the good data to arrays good_x_values and good_y_values
z = waitbar(0,'Overall data extraction progress');
for w = 1:length(good_i);
good_x_values(w,:) = validx(good_i(w),:);
good_y_values(w,:) = validy(good_i(w),:);
waitbar(w/(length(good_i)));
end;
% Plot the results
[num_good_points num_good_images] = size(good_x_values);
good_x_values_vector = reshape(good_x_values,num_good_points*num_good_images,1); % Convert good_x_values to vector form for plotting
good_y_values_vector = reshape(good_y_values,num_good_points*num_good_images,1); % Convert good_y_values to vector form for plotting
set(gcf,'Position',[150,50,800,600]); % [left bottom width height]
plot(good_x_values_vector,good_y_values_vector,'.r');
axis image;
xlabel('x-Location on Image [Pixels]');
ylabel('y-Location on Image [Pixels]');
drawnow;
close(z);
% Ask user if it is OK to delete the unselected areas permanently
replace_selection = menu(sprintf('Would you like to make this result permanent?'),'Yes','No');
if replace_selection == 1;
validx = good_x_values;
validy = good_y_values;
save validx.txt validx -ascii -tabs;
save validy.txt validy -ascii -tabs;
end;
end;
% Delete one marker at a time
if selection1 == 2;
title(sprintf('Pick (single click) one marker to remove. Click on the region of the\n marker that corresponds to the correlation results from the first few images.'));
[xpick ypick] = ginput(1);
% Find the indicies of the bad point using "minimum distance from the user's click" logic and searching only in the data from the first image
first_image_distances_from_pick_vector = abs(validx(:,1)-xpick)+abs(validy(:,1)-ypick);
[bad_i bad_j] = find(first_image_distances_from_pick_vector == min(first_image_distances_from_pick_vector));
% Delete the bad marker
validx(bad_i,:) = [];
validy(bad_i,:) = [];
% Plot the results
[num_good_points num_good_images] = size(validx);
good_x_values_vector = reshape(validx,num_good_points*num_good_images,1); % Convert validx to vector form for plotting
good_y_values_vector = reshape(validy,num_good_points*num_good_images,1); % Convert validy to vector form for plotting
set(gcf,'Position',[150,50,800,600]); % [left bottom width height]
plot(good_x_values_vector,good_y_values_vector,'.r');
axis image;
xlabel('x-Location on Image [Pixels]');
ylabel('y-Location on Image [Pixels]');
drawnow;
% Ask user if it is OK to delete the selected badpoint permanently
replace_selection = menu(sprintf('Would you like to make this result permanent?'),'Yes','No');
if replace_selection == 1;
save validx.txt validx -ascii -tabs;
save validy.txt validy -ascii -tabs;
end;
end;
% Ask user if they want to remove more badpoints by sending them back to RJT_process_results
close(gcf);
RJT_process_results;