Project

General

Profile

Files » Crop_4D_Cells.txt

crop and export cells from big images - Volker Baecker, 05/11/2012 03:26 PM

 
/**
* MRI Crop 4D Cells
*
* Allows to select cells in 4D images that will be loaded as virtual stack using loci-bioformats.
* An output folder that can be a samba share can be specified. The cropped images of the
* cells are written to the output folder in batch mode.
*
* written 2012 by Volker Baecker (INSERM) at Montpellier RIO Imaging (www.mri.cnrs.fr)
*/

var helpURL = "http://dev.mri.cnrs.fr/wiki/imagej-macros/Crop_4D_Cells";
var DIRECTORY = "";
var FILES = newArray(0);
var currentFile = 0;
var LOG_OFF_COMMAND = "shutdown /l /f";
var LOG_OFF = false;

macro "Crop 4D Cells Help [f1]" {
showHelp();
}

macro "First Image [f2]" {
firstImageAction();
}

macro "Previous Image [f3]" {
previousImageAction();
}

macro "Next Image [f4]" {
nextImageAction();
}

macro "Last Image [f5]" {
lastImageAction();
}

macro "Reload Image [f6]" {
reloadImageAction();
}

macro "Add Selection [f7]" {
addSelection();
}

macro "Export Cells [f8]" {
exportCellsAction();
}

macro "Export 4D Cells Options [f9]" {
showIOSettings();
}

macro "Crop 4D Cells Help (f1) Action Tool - C000T4b12?"{
showHelp();
}

macro "First Image (f2) Action Tool - C037T5d13|T9d13<C555" {
firstImageAction();
}

macro "First Image (f2) Action Tool Options" {
showIOSettings();
}

macro "Previous Image (f3) Action Tool - C037T4b12<" {
previousImageAction();
}


macro "Previous Image (f3) Action Tool Options" {
showIOSettings();
}

macro "Next Image (f4) Action Tool - C037T4b12>" {
nextImageAction();
}

macro "Last Image (f5) Action Tool - C037T1d13>T9d13|C555" {
lastImageAction();
}

macro "Last Image (f5) Action Tool Options" {
showIOSettings();
}

macro "Reload Image (f6) Action Tool - C037T4b12?" {
reloadImageAction();
}

macro "Reload Image (f6) Action Tool Options" {
showIOSettings();
}

macro "Add Selection (f7) Action Tool - C037T4b12A" {
addSelection();
}

macro "Add Selection (f7) Action Tool Options" {
showIOSettings();
}

macro "Export Cells (f8) Action Tool - C037T4b12E" {
exportCellsAction();
}


macro "Export Cells (f8) Action Tool Options" {
showLogOffDialog();
}

macro "Logoff Options [f10]" {
showLogOffDialog();
}

function showIOSettings() {
call("fr.cnrs.mri.macro.io.IOSettings.show");
}

function getFiles() {
list = call("fr.cnrs.mri.macro.io.IOSettings.getFileList");
if (list=="none") {
FILES=newArray(0);
return;
}
FILES = split(list, ",");
}

function loadCurrentImage() {
if (nImages>0) close();
file = FILES[currentFile];
run("Bio-Formats Importer", "open=["+file+"] color_mode=Composite view=Hyperstack stack_order=XYCZT use_virtual_stack");
enhanceDisplay();
loadRois(file);
}

function enhanceDisplay() {
Stack.getDimensions(width, height, channels, slices, frames)
for (i=1; i<=channels; i++) {
Stack.setChannel(i);
run("Enhance Contrast", "saturated=0.35");
}
Stack.setChannel(1);
}

function showImageStatus() {
showStatus("Image " + (currentFile+1) + " of " + FILES.length);
}


function loadRois(file) {
roiFile = file + ".zip";
roiManager("Reset");
if (File.exists(roiFile)) {
roiManager("Open", roiFile);
roiManager("Show None");
roiManager("Show All");
}
}

function addSelection() {
run("Add to Manager");
roiManager("Remove Slice Info");
if (roiManager("count")>0) roiManager("Save", FILES[currentFile]+".zip");
}

function showHelp() {
run('URL...', 'url='+helpURL);
}

function firstImageAction() {
getFiles();
if (FILES.length>0) {
if (currentFile!=0) {
currentFile = 0;
loadCurrentImage();
}
}
showImageStatus();
}

function previousImageAction() {
getFiles();
if (FILES.length>0) {
if (currentFile!=0) {
if (currentFile>0) currentFile--;
else currentFile=0;
loadCurrentImage();
}
}
showImageStatus();
}

function nextImageAction() {
getFiles();
if (FILES.length>0) {
if (currentFile!=FILES.length-1) {
if (currentFile<FILES.length-1) currentFile++;
else currentFile = FILES.length-1;
loadCurrentImage();
}
}
showImageStatus();
}

function lastImageAction() {
getFiles();
if (FILES.length>0) {
if (currentFile!=FILES.length-1) {
currentFile = FILES.length-1;
loadCurrentImage();
}
}
showImageStatus();
}

function reloadImageAction() {
getFiles();
if (FILES.length>0) {
if (currentFile<0 || currentFile>=FILES.length) currentFile = 0;
loadCurrentImage();
}
showImageStatus();
}

function exportCellsAction() {
outputFolder = call("fr.cnrs.mri.macro.io.IOSettings.getOutputFolder");
if (outputFolder=="none") {
Dialog.create("WARNING");
Dialog.addMessage("Please select an output folder in the IO Settings");
Dialog.show();
showIOSettings();
return;
}
getFiles();
print("\\Clear");
setBatchMode(true);
getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
print(dayOfMonth + "-" + (month+1) + "-" + year + " " + hour + ":" + minute + ":" + second + "." + msec);
for (i=0; i<FILES.length; i++) {
print("\\Update1:Processing file " + (i+1) + " of " + FILES.length);
print("\\Update2:Loading cells");
currentFile = i;
loadCurrentImage();
numberOfRois = roiManager("count");
channelsString = "";
slicesString = "";
framesString = "";
getDimensions(width, height, channels, slices, frames);
if (channels>1) channelsString = "channels=1-" + channels;
if (slices>1) slicesString = "slices=1-" + slices;
if (frames>1) framesString = "frames=1-" + frames;
imageTitle = File.nameWithoutExtension;
print(FILES[i]);
for (r=0; r< numberOfRois; r++) {
print("\\Update2:Processing cell " + (r+1) +" of "+ numberOfRois);
title = imageTitle + "-cell-" + (r+1) + "a";
print(title);
roiManager("Select", r);
run("Duplicate...", "title=" + title + " duplicate " + channelsString + " " + slicesString + " " + framesString);
run("Bio-Formats Exporter", "save=[" + outputFolder + "/" + title + ".ics]");
close();
}
}
if (nImages>0) close();
print("FINISHED");
getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
print(dayOfMonth + "-" + (month+1) + "-" + year + " " + hour + ":" + minute + ":" + second + "." + msec);
setBatchMode("exit and display");
if (LOG_OFF) exec(LOG_OFF_COMMAND);
}

function showLogOffDialog() {
Dialog.create("Export Cells Options");
Dialog.addCheckbox("log off after processing", LOG_OFF);
Dialog.show();
LOG_OFF = Dialog.getCheckbox();
}
(2-2/3)