crop = 1;
findAlign = 1;
lsAlign = 1;
rotate = 1;

i1 = imread('image1.tif');
imwrite(i1,'image1.jpeg', 'jpeg');

imshow(i1)
title('RAW IMAGE OF SPECIES SPECIFIC ARRAY')
fprintf(1,'hit return key to continue\r')
pause

center = [354 348];           %apriori center of filter
filterR = 187;
alignL = 25;   %length of alignment mark

% crop the image
if(crop==1)
  fprintf(1,'cropping the image\r')
  offset = 1.2 * filterR; 
  rect = [center(1)-offset, center(2)-offset,2*offset,2*offset];
  i2 = imcrop(i1,rect);
  imageSize = size(i2);
  center = [round(imageSize(1)/2), round(imageSize(2)/2)];

  % remove the background stuff  
  fprintf(1,'whiting out the background\r')
  for y=1:imageSize(2)
     for x= 1:imageSize(1)
        test = sqrt((center(1) - x)^2 + (center(2) - y)^2);
        if  test > filterR
           i3(x,y) = uint8(255);
        else
           i3(x,y) = uint8(i2(x,y));
        end
     end
  end

  imwrite(i3,'noBack.tif','tif');
  imshow(i3)
  title('BACKGROUND WHITED OUT');
  fprintf(1,'hit return to continue\r')
end

%mask out everything but alignment ring of filter
if(findAlign==1)
  fprintf(1,'finding alignment ring\r')   
  i3 = imread('noBack.tif');
  imageSize = size(i3);
  center = [round(imageSize(1)/2), round(imageSize(2)/2)];

  alignR = filterR - alignL;

  xStart = round(center(1) - (filterR * 1.1));
  yStart = round(center(2) - (filterR * 1.1));
  xEnd = round(center(1) + (filterR * 1.1));
  yEnd = round(center(2) + (filterR * 1.1));

  i4 = i3;
  for y=yStart:yEnd
     for x= xStart:xEnd
        test = sqrt((center(1) - x)^2 + (center(2) - y)^2);
        if  test < alignR
           i4(x,y) = uint8(255);
        end
     end
  end
  imwrite(i4,'alignOnly.tif','tif');

  imshow(i4)
  title('ALIGNMENT RING ONLY');
  fprintf(1,'hit return to continue\r')
end

%find the intersection of the two alignment lines
if(lsAlign==1)
  i4 = imread('alignOnly.tif');

  bw1=im2bw(i4);
  bw2=~bw1;
  imshow(bw2)
  title('ALIGNMENT LINES ONLY')
  pause
  fprintf(1,'hit return to continue\r')

  l=bwlabel(bw2);
  stats=imfeature(l,'all');
  
  x1=stats(1).PixelList(:,1);
  y1=stats(1).PixelList(:,2);
  x2=stats(2).PixelList(:,1);
  y2=stats(2).PixelList(:,2);

  si1=polyfit(x1, y1, 1);
  si2=polyfit(x2, y2, 1);
  
  xCenter = round((si2(2) - si1(2))/(si1(1) - si2(1)));
  yCenter = round(si1(1) * xCenter + si1(2));

  xCenter = round(xCenter);
  yCenter = round(yCenter);

  bw3 = ~bw2;
  for i = -10:10
     bw3(yCenter, xCenter + i) = 0;
     bw3(yCenter + i, xCenter) = 0;
  end
  
  for x = 1:xCenter
     y = si1(1) * x + si1(2);
     bw3(round(y),round(x))=0;
  end
  for y = 1:yCenter
     x = (y - si2(2)) / si2(1);
     bw3(round(y),round(x))=0;
  end
 
 imshow(bw3);
 title('FIT LINES');
 pause
 fprintf(1,'hit return to continue\r')
end

%clean up and rotate cropped image
if(rotate==1);
   i5 = imread('noBack.tif');
   i = find(i3<100);
   i5(i) = 0;
   i = find(i3>=100);
   i5(i) = 255;
   
   i6 = imrotate(i5,180/pi*atan(si1(1)));
   imshow(i6)
   imwrite(i6,'image2.jpeg', 'jpeg');
   title('PROCESSED IMAGE CROPPED, THRESHOLDED & ALIGNED')
end

