Project

General

Profile

Files » MRI_NDPI_Tools.txt

Volker Baecker, 07/04/2012 03:39 PM

 
/**
* MRI NDPI Tools
*
* written 2011-2012 by Volker Baecker (INSERM) at Montpellier RIO Imaging (www.mri.cnrs.fr)
*/

var helpURL = "http://dev.mri.cnrs.fr/wiki/imagej-macros/NDPI_Tools"
var rows =1;
var columns = 4;
var lowerThreshold = 0;
var upperThreshold = 249;
var splitCommand="/"+getDirectory("macros")+"bin"+"\\"+"ndpisplit";

var CLOSE_SESSION_AFTER_CONVERT = false;
var CLOSE_SESSION_AFTER_SPLIT = false;
var LOG_OFF_COMMAND = "shutdown /l /f";

macro "Unused Tool - C037" { }

macro "MRI Skin Tools Help Action Tool -C000T4b12?"{
run('URL...', 'url='+helpURL);
}

macro "Convert NDPI Images Action Tool - C000T4b12c" {
print ("Convert NDPI Images started");
dir = getDirectory("Choose a Directory");
files = getFileList(dir);
first = true;
nrOfImages = lengthOf(files);
for (i=0; i<nrOfImages; i++) {
file = files[i];
path = dir + file;
if (endsWith(file, ".ndpi")) {
if (first) {
print("Reading dimensions of available images, please wait...");
first = false;
resolutions = call("loci.formats.in.NDPIReader.getResolutionString", path);
Dialog.create("Select a resolution");
choices = split(resolutions, "\n");
Dialog.addChoice("resolution", choices, choices[2]);
Dialog.show();
resolution = Dialog.getChoice();
components = split(resolution, ":");
resolutionIndex = components[0];
}
out = dir + "tif";
if (!File.exists(out)) {
File.makeDirectory(out);
}
print("\\Update:converting image " + (i+1) + "/" + nrOfImages);
res = call("loci.formats.in.NDPIExporter.exportAsTif", path, out, resolutionIndex);
}
}
print ("Convert NDPI Images finished");
if (CLOSE_SESSION_AFTER_CONVERT) exec(LOG_OFF_COMMAND);
}

macro "Convert NDPI Images Action Tool Options" {
Dialog.create("Convert NDPI Images tool options");
Dialog.addCheckbox("close session when finished", CLOSE_SESSION_AFTER_CONVERT);
Dialog.show();
CLOSE_SESSION_AFTER_CONVERT = Dialog.getCheckbox();
}

macro "Select cuts Action Tool - C000T4b12s" {
width = getWidth();
height = getHeight();
deltaX = height / (2*rows);
deltaY = width / (2*columns);
roiManager("Reset");
run("8-bit");
setThreshold(lowerThreshold,upperThreshold);
for (row = 1; row<=rows; row++) {
for (column=1; column<=columns; column++) {
y = ((2 * row) - 1) * deltaX;
x = ((2*column) -1) * deltaY;
v = getPixel(x,y);
print("v="+v);
while(v>upperThreshold || v<lowerThreshold) {
x--;
v = getPixel(x,y);
print("left");
}
print("x="+x+", y="+y);
doWand(x, y);
roiManager("Add");
}
}
resetThreshold();
}

macro "Select cuts Action Tool Options" {
Dialog.create("Select cuts tool options");
Dialog.addNumber("rows", rows);
Dialog.addNumber("columns", columns);
Dialog.show();
rows = Dialog.getNumber();
columns= Dialog.getNumber();
}

macro "Export Selections Action Tool - C000T4b12e" {
dir = getInfo("image.directory");
file = getInfo("image.filename");
path = dir+file;
outPath = dir + "cut/";
if (!File.exists(outPath)) {
File.makeDirectory(outPath);
}
comp = split(file, ".");
titleWithoutExt = comp[0];
ext = comp[1];
numberOfROIs = roiManager("count");
for (i=0; i<numberOfROIs; i++) {
roiManager("Select", i);
title = titleWithoutExt + "-" + (i+1) + ext;
run("Duplicate...", "title=" + title);
run("Select None");
saveAs("Tiff", outPath + title);
close();
}
}

macro "Unused Tool - C037" { }

macro "Split NDPI Images Batch Action Tool - C000T4b12s" {
print ("Split NDPI Images started");

dir = getDirectory("Choose a Directory");
files = getFileList(dir);
first = true;
nrOfImages = lengthOf(files);
for (i=0; i<nrOfImages; i++) {
file = files[i];
path = dir + file;
if (endsWith(file, ".ndpi")) {
out = dir + "tif";
if (!File.exists(out)) {
File.makeDirectory(out);
}
print("\\Update:splitting image " + (i+1) + "/" + nrOfImages);
exec (splitCommand, "-mn", "\""+path+"\"");

}
}
print ("Split NDPI Images finished");
if (CLOSE_SESSION_AFTER_SPLIT) exec(LOG_OFF_COMMAND);
}

macro "Split NDPI Images Batch Action Tool Options" {
Dialog.create("Split NDPI Images Batch tool options");
Dialog.addCheckbox("close session when finished", CLOSE_SESSION_AFTER_SPLIT);
Dialog.show();
CLOSE_SESSION_AFTER_SPLIT = Dialog.getCheckbox();
}

macro "NDPI selection export Action Tool - C000T4b12n" {
MAGNIFICATION = "20";
Z = "0";
dir = getInfo("image.directory");
parentDir = File.getParent(dir);
name =File.nameWithoutExtension;
mosaic = getMosaicImages(name, parentDir, MAGNIFICATION, Z);
getSelectionBounds(roiX, roiY, roiWidth, roiHeight);
imageWidth = getWidth();
imageHeight = getHeight();
for (i=0; i<mosaic.length; i++) {
print(mosaic[i]);
}
}

function getMosaicImages(name, dir, magnification, z) {
files = getFileList(dir);
filter = name+"_x"+magnification+"_z"+z+"_";
hits = 0;
for (i=0; i<files.length; i++) {
file = files[i];
if (startsWith(file, filter)) hits++;
}
resultFiles = newArray(hits);
counter = 0;
for (i=0; i<files.length; i++) {
file = files[i];
if (startsWith(file, filter)) {
resultFiles[counter]=file;
counter++;
}
}
return resultFiles;
}
(1-1/2)