// FISHji5

// LEPABE 09-02-2015

// Daniel Carvalho  & Analia Lourenco

// Macro for ImageJ. Determination of the mean fluorescence intensity.

 

 

var min_size=0;

var max_size=0;

var min_circ=0;

var max_circ=0;

var distance_pixel=0;

var known_distance=0;

var aspect_ratio=0;

var unit_length="";

var channel = "green";

 

 

macro "FISHji5"{                

                 inputFolder = getDirectory("Choose the input folder!");

                

                 openDialog();       

                

                 outputFolder = getDirectory("Choose the output folder!");

                

                 setBatchMode(true);

 

                 images = getFileList(inputFolder);

                 imageCount = countImages(images);

                 notice = "converted: \n";

 

                 //out = File.open(outputFolder+"statistics.txt");

                

                 for (i=0; i<images.length; i++) {

                    showProgress(i, imageCount);

                    inputPath = inputFolder + images[i];

                   

                    if (isImage(inputPath)) {

                                    

                                     processFISH(inputFolder,images[i],outputFolder);

                                    

                                     outputPath = outputFolder + images[i];

                                     //save(outputPath);

                                                                       

                                     notice = notice + outputPath + "\n";

                    }

                 }

                 setBatchMode(false);

                 print(notice);

                 exit();

                

                 function processFISH(inputFolder,image,out) {

                                  inputPath = inputFolder + image;

                                  open(inputPath);

                                 

                                  title=getTitle();

                                  run("RGB Split");

                                                                   

                                  selectImage(title+" ("+channel+")");

                                 

                                  // Set scale: distance in pixels, known distance, pixel aspect ratio, unit of length;

                                  run("Set Scale...", "distance=distance_pixel known=known_distance pixel=aspect_ratio unit=unit_length global");

                                  run("Subtract Background...", "rolling=80"); // Application of the "Rolling Ball" algorithm

                                  t=getTitle();

                                  run("Duplicate...", "title=roi");

                                 

                                 

                                  // Auto Threshold Brigthness and Contrast Treatment

                                                                   

                                  Auto_T = 5000;

                                  getRawStatistics(pixcount);

                                  limit = pixcount/10;

                                  threshold = pixcount/Auto_T;

                                  nBins = 256;

                                  getHistogram(values, histA, nBins);

                                  i = -1;

                                  found = false;

                                  do {

                                                   counts = histA[++i];

                                                   if (counts > limit) counts = 0;

                                                   found = counts > threshold;

                                  } while ((!found) && (i < histA.length-1))

                                  hmin = values[i];

 

                                  i = histA.length;

                                  do {

                                                   counts = histA[--i];

                                                   if (counts > limit) counts = 0;

                                                   found = counts > threshold;

                                  } while ((!found) && (i > 0))

                                  hmax = values[i];

 

                                  setMinAndMax(6, hmax);

                                  run("Apply LUT");

                                 

                                  selectImage(1);

                                  run("Convolve...", "text1=[0 0 0 -1 -1 -1 0 0 0\n0 -1 -1 -3 -3 -3 -1 -1 0\n0 -1 -3 -3 -1 -3 -3 -1 0\n-1 -3 -3 6 13 6 -3 -3 -1\n-1 -3 -1 13 24 13 -1 -3 -1\n-1 -3 -3 6 13 6 -3 -3 -1\n0 -1 -3 -3 -1 -3 -3 -1 0\n0 -1 -1 -3 -3 -3 -1 -1 0\n0 0 0 -1 -1 -1 0 0 0\n] normalize"); // Convolution Laplacian of Guassian kernel 9x9

                                  run("Make Binary");

                                  run("Fill Holes");

                                                                   

                                  run("Set Measurements...", "  mean redirect=["+t+"] decimal=0"); // Overlay;

                                 

                                  run("Analyze Particles...", "size=min_size-max_size pixel circularity=min_circ-max_circ show=Nothing add"); // size and circularity parameters

                                                  

                                  roiManager("Show all without labels");

                                  run("Colors...","selection=magenta");

                                                                   

                                  open(inputPath);

                                  original=getTitle();

                                  print(">>>>O: "+title);

                                 

                                  selectImage("roi");                               

                                  roi=getTitle();

                                  print(">>>>R: "+roi);                                                                                                                                

                                 

                                 

                                  run("Add Image...","image=[&original]");

                                  run("From ROI Manager");

                                  run("Flatten");

                                 

                                  saveAs("Tiff",out+"/["+original+"]_1");         

                                                                                                     

                                  run("Close");

                                  run("Clear Results");

                                 

                                  close();                    

                 }

                

                                 

                 function isImage(filename) {

                                  extensions = newArray("tif", "tiff", "jpg", "bmp");

                                  result = false;

                                  for (i=0; i<extensions.length; i++) {

                                                   if (endsWith(toLowerCase(filename), "." + extensions[i]))

                                                                    result = true;

                                  }

                                  return result;

                 }                                                

                                                  

                 function openDialog(){

                                 

                                  Dialog.create("FISH Image Processing");

                                 

                                  Dialog.setInsets(12,0,0);

                                  Dialog.addNumber("Minimum image size:", 0);

                                  Dialog.addNumber("Maximum image size:", 500);                       

                                                  

                                  Dialog.addNumber("Minimum image circularity:", 0);

                                  Dialog.addNumber("Maximum image circularity:", 1);                                

                                                                   

                                  Dialog.setInsets(12,0,0);

                                  Dialog.addNumber("Distance in Pixels:", 0);

                                  Dialog.addNumber("Known Distance:", 0);

                                  Dialog.addNumber("Pixel Aspect Ratio:", 1.0);

                                  Dialog.addString("Unit of Length:", "pixel");

                                                                   

                                  Dialog.setInsets(12,0,0);

                                  items_c = newArray("green", "blue", "red");

                                  Dialog.setInsets(1,1,10);

                                  Dialog.addRadioButtonGroup("Channel:", items_c, 3, 1, "green");

                                 

                                  help= "<html>"

                                                   +"<h3>Description of Parameters:</h3><br/>"

                                                   +"<p><b>Size:</b> Values range from 0 (minimum image size) to infinity (maximum image size). Values are expressed in size square units or pixels. </p>"

                                                   +"<p><b>Circularity:</b> Values [4*pi*(area/perimeter^2)] range from 0 (infinitely elongated polygon) to 1 (perfect circle).  </p>"                                       

                                                   +"<p><b>Channel:</b> Select the correct channel (red, green, and blue) for each input folder, images will be separated in order to analyse the channel where fluorescence is emitted. </p>"

                                                   +"<p>Further details about the macro and examples can be found at <a href='http://193.137.11.213/fish/'>FISHji's Web site.</a></p>"

                                                   +"<br/><br/></html>";

                                 

                                 

                                  Dialog.addHelp(help);

                                                                   

                                  Dialog.show();

                                 

                                  min_size = Dialog.getNumber();

                                  max_size = Dialog.getNumber();

                                 

                                  min_circ = Dialog.getNumber();

                                  max_circ = Dialog.getNumber();

                                 

                                  distance_pixel = Dialog.getNumber();

                                  known_distance = Dialog.getNumber();

                                  aspect_ratio = Dialog.getNumber();

                                  unit_length = Dialog.getString();

                                 

                                  channel = Dialog.getRadioButton();

                 }              

                                                  

                 function countImages(array) {

                     result = 0;

                     for (i=0; i<array.length; i++) {

                                                   if (isImage(array[i])) result++;

                                  }

                                  return result;

                 }

                

}