function plate_images=collect_plate_images_v4(imagefilenames,plate_indices,bestangle)

%Fred Cross June 2016 The Rockefeller University

%for collection of blue-channel images from Olympus camera, where files
%are saved as 'P1040045.JPG' then sequentially adding to 45.  Assume a
%stack; assume the first 4 are replicates of a grid standard.  In this
%example if there are 5 images in addition to the 4 grid standards,
%name_stem is 'P100400', start_number is 45, end_number is 53

%%

global corners_x corners_y;
global data_directory;

n_plate_images=length(plate_indices);  
k=ceil(sqrt(n_plate_images));

figure(201);clf;
for image_index=1:length(plate_indices)
    plateimagefilenames{image_index}=[data_directory,'/',imagefilenames{plate_indices(image_index)}];
end
    
 %%   
plate_images{n_plate_images}=get_plate_image(plateimagefilenames{n_plate_images});

  %%

for image_index=1:n_plate_images-1
    fprintf('processing image %d: %s\n',image_index,plateimagefilenames{image_index});
    plate_images{image_index}=get_plate_image(plateimagefilenames{image_index});
    
    
    
    if mod(image_index,2)==0
        figure(100);set(100,'Position',[ 1326 553  560   420]);
        subplot(2,1,1);imagesc(plate_images{image_index-1});title(sprintf('Image pair # %d',image_index/2));
        subplot(2,1,2);imagesc(plate_images{image_index});title(plateimagefilenames{image_index});
    end
        
    figure(101);set(101,'Position',[  10          30        1287         945]);
    subplot(k,k,image_index);
    imagesc(plate_images{image_index});colormap gray;drawnow;
end

subplot(k,k,n_plate_images);
imagesc(plate_images{n_plate_images});colormap gray;drawnow;

   %%%%%%%%%
 %%%%%%%%%
function final_im=get_plate_image(filename)

    global corners_x corners_y;

fprintf('Opening image %s \n',filename);
im_rgb=imread(filename);
disp('converting to mxn blue pos');
    
im_bp=blue_pos(im_rgb);   
clear im_rgb;
disp('processing');
final_im=im_process(im_bp);
clear im_bp;

%%%%%%%%%%%%%

function im_bp=blue_pos(im_rgb)

%get blue, make pos double image

    imn=double(im_rgb(:,:,3));  %chlamy best seen in blue channel since neg image; 
    clear im_rgb;
    imn=imn/max(imn(:)); %make sure 0-1
    im_bp=1 - imn; %conv to pos
    clear imn;
    
  %%%%%%%
    function proc_im=im_process(im_b)
        
        global corners_x corners_y;

       %zero out margins
       [im_rows,im_cols]=size(im_b);
       top=max(1,corners_y(1,1)-50);
       bottom=min(im_rows,corners_y(end,1)+50);
       
       left=max(1,corners_x(1,1)-50);
       right=max(im_cols,corners_x(1,end)+50);
       
       im_b([1:top,bottom:end],:)=0;
       im_b(:,[1:left,right:end])=0;
       
    %crude background subtraction
    [n,xout]=hist(im_b(:));
    thresh=xout(min(find(cumsum(n)/sum(n)>0.75)));
    im_b=max(0,im_b-thresh);
    
     N=10;s=5;
     H=fspecial('gaussian',[N N],s);
    imf=imfilter(im_b,H);
   % figure(12);imagesc(imf);
    clear im_b;
    
    proc_im=imtophat(imf,strel('disk',200));
  %  figure(13);imagesc(proc_im);
    clear imf;
    
    
 
            