Background
My goal is to iterate through a directory and run two functions on most iterations. Some of the images are a bit messed up, and in the effort to fully automate my workflow, I’d like to skip those images. Here is the code I’ve written to determine whether the image is skip-able.
The macro runs great until it encounters an image with no Y1 column in the results pane. Thats why I’ve written the SkipOrNot function to look at the Y0 column and hopefully skip the second function.
The other function (getSeagrassPlotData) should run independantly of the SkipOrNot function.
Challenges
- I can’t seem to get the “skip” variable to move in and out of the function the way I need it to. I’ve tried calling it a global variable (suggested here) and just a regular variable.
- When running the SkipOrNot function on a single image, it works fine. It fails when it is put into the for loop.
- I’ve considered trying to make my variable an Array but don’t entirely understand how that would help.
#@ File(label="Input directory", description="Select the directory with input images", style="directory") inputDir
#@ File(label="Output directory", description="Select the output directory", style="directory") outputDir
x6m=90
x3m=350
function GetCenterish(x1,y1,x2,y2){
makeLine(x1, y1, x2, y2);
run("Line Width...", "line=1");
run("Smoothed Plot Profile", "window=15 pixelunits=false");
Plot.showValues();
Y1 = Table.getColumn("Y1");
indicies_max = Array.findMaxima(Y1, 0.05);
imax = indicies_max[0];
return imax;
}
function getTitleStripExtension(){
t = getTitle();
t = replace(t, ".tif", "");
t = replace(t, ".tiff", "");
t = replace(t, ".jpg", "");
t = replace(t, ".png", "");
return t;
}
function SkipOrNot(x1,y1,x2,y2){
makeLine(x1, y1, x2, y2);
run("Line Width...", "line=1");
run("Smoothed Plot Profile", "window=15 pixelunits=false");
Plot.showValues();
sumY0 = 0;
for (i = 0; i < nResults; i++) {
sumY0 = sumY0+getResult("Y0",i);
};
//print(skip);
if (sumY0>0) {
print("All good, Don't skip ");
skip = "NO";
print(skip);
//return skip;
}else {
//print("Line has 0 -> Skipping");
skip = "YES";
print(skip);
//return skip;
};
return skip;
}
function getSeagrassPlotData(inputDir, outputDir, filename){
//open(inputDir + "\\" + filename);
name = getTitleStripExtension();
//print("Processing "+name);
//if not enough data, skip
//find center of 6m end
y6m = GetCenterish(x6m, 0, x6m, 564);
close();
print(y6m);
//Start xy from 6m
//x = x6m;
//y = y6m;
//Angle of tilt of transducer
y3m = GetCenterish(x3m, 0, x3m, 564);
close();
print(y3m);
dx = x3m - x6m; //x from the two standard lines
dy = y3m - y6m; //calculated y values from the two standard lines
a = dy/dx;
//a = 13*PI/180;
theta = a*(180/PI);
print(theta+" Degrees");
//print("Angle = "+a);
//find the point 600 pix from the 6m point
xa = 600 * cos(a);
x2 = x6m + xa;
ya = 600 * sin(a);
y2 = y6m + ya;
print("Line Between: 0m ("+x2+", "+y2+") and 6m ("+x6m+", "+y6m+")");
makeLine(x2, y2, x6m, y6m);
//run("Set Scale...", "distance=500 known=5 unit=meters");
run("Line Width...", "line=5");
//run("Plot Profile");
profile = getProfile();
for (j=0; j<profile.length; j++) {
setResult("distance.m", j, j/100);
setResult("values", j, profile[j]);
}
updateResults();
//saveAs("Results","D:\DIDSON\\PlotTest.csv");
print("Saving as: "+ outputDir + "\\" + name + ".csv");
saveAs("Results", outputDir + "\\" + name + ".csv");
close();
//close();
}
//Results pane needs to be open before getSeagrassPlotData will accurately plot the line
updateResults();
//Loop through the images in the input directory
setBatchMode(false);
list = getFileList(inputDir);
for(i = 0; i < list.length; i++) {
open(inputDir + "\\" + list[i]);
name = getTitleStripExtension();
print("Processing "+name);
var skip;
skip = SkipOrNot(x6m, 0, x6m, 564);
if (skip == "YES") {
print("Skipping "+name);
close();
continue;
}else {
close();
getSeagrassPlotData(inputDir, outputDir, list[i]);
};
};
setBatchMode(false);
Sample images
Plot5_110222_HF_00006.tif (391.6 KB)
This image would be classified as nonskippable
Plot5_110222_HF_00034.tif (391.6 KB)
this image would be classified as skippable.