Skip to content
Snippets Groups Projects
FoliageCoverage_PC_EN.txt 4.89 KiB
Newer Older
Eric Duchene's avatar
Eric Duchene committed
//This macro for ImageJ find the geen leaves of grapevine on a blue Background;
//Éric Duchêne, 2012, eric.duchene@inrae.fr;
//Reference: Duchêne E, Dumas V, Jaegli N, Merdinoglu D. 2012. Deciphering the ability of different grapevine genotypes to accumulate sugar in berries. Australian Journal of Grape and Wine Research 18(3): 319-328;
//Update (English version) August 2023;
//The output in an Excel files with the mean grey of each image;

//How to use this macro in ImageJ?
//Select "Plugins/Macros/Install" in the ImageJ menu and choose this macro;
//Select the directory with the images to process in "Plugins/Macros/Directory and Parameters" (or "alt-d" on MacOS);
//Start to analyze the images with "Plugins/Macros/Foliage coverage" (or "alt-c" on MacOS);

//General variables 
var imagesDir="";
var resultsDir;
var tempDir;
var Rfile;
var list;
var Titre;
var TimeString;
setKeyDown("esc");

//This function records the results and delete intermediate files;

function CleanFiles(tempDir,resultsDir,Rfile) {

saveAs("measurements",resultsDir+Rfile+".xls");
if(nImages>0) run("Close All");
ListTemp=getFileList(tempDir);
for(i=0;i<lengthOf(ListTemp);i++) File.delete(tempDir+ListTemp[i]);
File.delete(substring(tempDir,0,lengthOf(tempDir)-1));
roiManager("Reset");
run("Clear Results");
}

macro "directory and parameters [d]"
{
print("\\Clear");
imagesDir=getDirectory("");
print("Working directory: "+imagesDir);
getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
TimeString ="Date: "+dayOfMonth+"/"+month+1+"/"+year;
print(TimeString);
list=getFileList(imagesDir);
//Create a subdirectory for the results;
resultsDir=imagesDir+"resultsS/";
File.makeDirectory(resultsDir);
print("Results in "+resultsDir);
//Create a subdirectory for temporary files;
tempDir=imagesDir+"temp/";
File.makeDirectory(tempDir);
print("Tampon: "+tempDir);
run("Clear Results");
roiManager("Reset");
Dialog.create("File with the results");
Dialog.addMessage("Provide a name for the file with the results");
Dialog.addString("Nom", "Results");
Dialog.show();
Method= Dialog.getChoice();
//PhotoType=Dialog.getChoice();
Rfile=Dialog.getString();


run("Set Measurements...", "area mean  center of mass redirect=None decimal=1");
if(nImages>0) run("Close All");
run("Colors...", "foreground=black background=white selection=yellow");
run("Options...", "iterations=1 count=1 edm=Overwrite");
print ("Fichier de résultat: "+Rfile);
print ("#      Settings OK    #");
}

macro "Foliage coverage [c]" {

if (imagesDir=="") {
showMessage("Choose a working directory then open an image");
print("\\Clear");
imagesDir=getDirectory("");
print("Working directory "+imagesDir);
getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
TimeString ="Date: "+dayOfMonth+"/"+month+1+"/"+year;
print(TimeString);
list=getFileList(imagesDir);
//Create a subdirectory for the results;
resultsDir=imagesDir+"resultsS/";
File.makeDirectory(resultsDir);
print("Results in "+resultsDir);
//Create a subdirectory for temporary files;
tempDir=imagesDir+"temp/";
File.makeDirectory(tempDir);
print("Tampon: "+tempDir);
run("Clear Results");
roiManager("Reset");
Dialog.create("Question");
Dialog.addMessage("Name of the results file")

Dialog.addString("Nom", "Results");
Dialog.show();
Method= Dialog.getChoice();
//PhotoType=Dialog.getChoice();
Rfile=Dialog.getString();

run("Set Measurements...", "area mean  center of mass redirect=None decimal=1");
if(nImages>0) run("Close All");
run("Colors...", "foreground=black background=white selection=yellow");
run("Options...", "iterations=1 count=1 edm=Overwrite");
print ("#      Settings OK     #");
}

do
{
if(nImages==0) {
//showMessage("Open an image");
open();
}
if(selectionType==-1) {
makeRectangle(280, 0, 954, getHeight());
ROI_Ok=getBoolean("Modify the region of interest?");
if (ROI_Ok==1) exit();
} 
roiManager("reset");

if(selectionType!=-1) run("Crop");

Titre=getTitle()+"_";
saveAs("Tiff", tempDir+"IMGWo.tif");
run("Brightness/Contrast...");
run("Enhance Contrast", "saturated=0.8");
run("Split Channels");
imageCalculator("Subtract", "IMGWo.tif (green)","IMGWo.tif (blue)");
//run("Image Calculator...", "image1=[IMGWo.tif (green)] operation=Subtract image2=[IMGWo.tif (blue)] create");
run("Set Measurements...", "area mean area_fraction display redirect=None decimal=3");
selectWindow("IMGWo.tif (red)");
close();
selectWindow("IMGWo.tif (blue)");
close();
saveAs("Tiff", tempDir+Titre);
setAutoThreshold();
setThreshold(20, 255);
run("Measure");
//run("Convert to Mask");
run("Create Selection");
roiManager("Add");
roiManager("Deselect");
run("Select None");
close();
File.delete(tempDir+Titre);
open(tempDir+"IMGWo.tif");
selectWindow("IMGWo.tif");
setOption("Show All",true);
Continuer=getBoolean("Continue with another image?");
close();
File.delete(tempDir+"IMGWo.tif");

} while (Continuer==1);
showMessage("Results are in the directory "+ resultsDir);
CleanFiles(tempDir,resultsDir,Rfile);
imagesDir="";
}