ImageJ Macro Fragments
Some notes on ImageJ macro
Getting the number of Cores
<sxh js> th = eval(“script”,“Runtime.getRuntime().availableProcessors();”); print(th); </sxh>
Loading ROIs from a file in Internet
shell command “curl” should be available in your local machine. See here for installation.
<sxh js> run(“Blobs (25K)”); path = getDirectory('imagej')+“RoiSet.zip”; print(path) exec(“curl”, “-LJO”, “https://github.com/mutterer/weird/raw/master/RoiSet.zip”, “–output”, path); roiManager(“Open”,path); roiManager(“Show All”); File.delete(path); </sxh>
Getting Pixel Coordinates within a Polygon ROI
<sxh js> Roi.getContainedPoints(xps, yps); for(i=0; i<xps.length; i++) {
xpos = xps[i]; ypos = yps[i]; pixval = getPixel(xpos, ypos); print("("+ xpos + ", " + ypos + ") ", pixval);
} </sxh>
Ways of getting the file basename
<sxh js> filepath = “/my/path/to/great_image.tif”;
Method 1, string indexing basename = substring(filepath, lastIndexOf(filepath, “/”)+1, lastIndexOf(filepath, “.”)); print(“Method 1: ”, basename); Method 2, regular expression basename = replace(filepath, “(.*\\/)(.*)\\..{3,5}$", "$2”); print(“Method 2: ”, basename); … in this case, it's also possible to get the parent directry just with a small modification parent = replace(filepath, “(.*\\/)(.*)\\..{3,5}$”, “$1”); print(“…. parent: ”, parent); Method 3, use a specific command from ver. 1.52r basename = File.getNameWithoutExtension(filepath); print(“Method 3: ”, basename); … it's also possible to get the parent directory with a command. parent = File.getDirectory(filepath); print(“…. parent: ”, parent);
</sxh>
Convert black pixels to white in RGB image
<sxh js> for (j =0; j<getHeight();j++){
for (i=0; i<getWidth();i++){ pix = getPixel(i, j); sum = sumRGB(pix); if (sum == 0){ setPixel(i, j, 0xffffff); } }
}
function sumRGB(RGBpixval){
sum = getR(pix) + getG(pix) + getB(pix); return sum;
}
function getR(RGBpixval){
rf = (RGBpixval & 0xff0000)>>16; return rf;
} function getG(RGBpixval){
gf = (RGBpixval & 0x00ff00)>>8; return gf;
} function getB(RGBpixval){
bf = (RGBpixval & 0x0000ff); return bf;
} </sxh>