Login  Register

RE: Batch splitting of stacks and saving

Posted by cmparkes on Dec 01, 2014; 5:01pm
URL: http://imagej.273.s1.nabble.com/Batch-splitting-of-stacks-and-saving-tp4578247p5010710.html

Thank you so much for your help Christine! The code you helped me with works beautifully. I added the suggested section about setting the threshold measurements but I need to try and save the 'measurements' which I take from the images as they are being created and saved into their subfolders. I have tried to add a section of code to change the name of the result in the result window to the name of the file it came from and to keep the results window open and save it as a text file at the end. 

I've attached my updated code. I've highlighted in Red where I have made additions


----------------begining of code-----------------------


dir = getDirectory("Choose a Directory to PROCESS");     
    list = getFileList(dir);                                                     
dir2 = getDirectory("Choose a Directory for SAVING the Total Cell Image");   
dir3 = getDirectory("Choose a Directory for SAVING the GFP Cell Image"); 

 setBatchMode(true);               
    for (f=0; f<list.length; f++) {
        path = dir+list[f];                       
 print(path);                                     
        showProgress(f, list.length);     
        if (!endsWith(path,"/")) open(path); 
if (nImages>=1) {                                         
  if (endsWith(path,"f")) {
   
   t=getTitle();                                   
   s=lastIndexOf(t, '.');                           
   ts=substring(t, 0,s); 
  
  
  ts2= t +'total.tif';                                 
  ts3= t +'gfp.tif';                                                           

//open("FOLDER NAMEf");      {
run("Split Channels"); 
selectWindow(t+"(blue)");

//run("Brightness/Contrast..."); 
setMinAndMax(0, 50); 
//run("Close"); 
//run("Enhance Contrast...", "saturated=2 normalize"); 
//run("Find Edges"); 
run("Set Measurements...", "area standard area_fraction limit redirect=None decimal=1");
name(getTitle()
selectWindow(name)
setAutoThreshold("Default dark");
//run("Threshold..."); 
setThreshold(0, 193); 
//run("Measure");  

data - newArray(""+getResult("area_standard")+"", ""+getResult("area_fraction")+"");
}
selectWindow(t + " (blue)"); 
   rename(ts2);                                       
   saveAs("Tiff",  dir2 + ts2 + ".tif");         
   run("Close");                                   
      
selectWindow(t + " (green)");           
   rename(ts3);                                       
   saveAs("Tiff",  dir3 + ts3 + ".tif");         
   run("Close");           

 selectWindow("Results"); 
 dotIndex = "s"; 
 if (dotIndex!=-1); 
 path = substring(path, 0, dotIndex); // remove extension 
 save(path+"-results.txt");
 close() 


run("Close All");                               

       }                                 
  } 
}

--------------------end of code-----------------


However whenever I run the code I get this error message: No window with *first file title in the process folder*.tif(blue)" found. Which was not seen before.

The additional bits of code were added to:-

1) Name the file from which the measurements were collected

2) Create a new results table with the area and area fraction results

3) Code telling the macro what to measure

4) Renaming the files and the destination files

If you could look over my additions, that would be most helpful. Also do you have any literature that you suggest I read to get more of an understanding of how to write these kind of macros?

Best Wishes

Christina Parkes


Miss Christina Parkes
Department of Women's and Children's Health
University of Liverpool






Date: Wed, 26 Nov 2014 12:25:33 -0800
From: [hidden email]
To: [hidden email]
Subject: Re: Batch splitting of stacks and saving

Hello Christina,

This code should do what you want, holding the name of the file under the variable t and then using ImageJ's habit of naming split images "original name (blue)", etc. to select the image to process.  Just set up the two folders for saving the blue and the green images before you run the macro so that you can select them when the macro asks for them.  It will only ask for the folders once before batch processing all.  

May I suggest that between yoursetThreshold command and your Measure command you insert some code to tell the macro what you want to measure and whether or not you want the measurements limited to the thresholded area (record under Analyze --> Set Measurements).  

Kind Regards,
Christine

---------------------begin code-----------------

dir = getDirectory("Choose a Directory to PROCESS");    
    list = getFileList(dir);                                                    
dir2 = getDirectory("Choose a Directory for SAVING the BLUE Image");  
dir3 = getDirectory("Choose a Directory for SAVING the GREEN Image");


 setBatchMode(true);              
    for (f=0; f<list.length; f++) {
        path = dir+list[f];                      
 print(path);                                    
        showProgress(f, list.length);    
        if (!endsWith(path,"/")) open(path);
if (nImages>=1) {                                        
  if (endsWith(path,"f")) {

   t=getTitle();                                      
   s=lastIndexOf(t, '.');                          
   ts=substring(t, 0,s);
 
 
  ts2= t +'blue image.tif';                                
  ts3= t +'green image.tif';                                                          

//open("FOLDER NAMEf");
run("Split Channels");
selectWindow(t + " (blue)");

//run("Brightness/Contrast...");
setMinAndMax(0, 50);
//run("Close");
run("Enhance Contrast...", "saturated=2 normalize");
run("Find Edges");
setAutoThreshold("Default dark");
//run("Threshold...");
setThreshold(0, 193);
run("Measure");

   rename(ts2);                                      
   saveAs("Tiff",  dir2 + ts2 + ".tif");        
   run("Close");                                  
     
selectWindow(t + " (green)");          
   rename(ts3);                                      
   saveAs("Tiff",  dir3 + ts3 + ".tif");        
   run("Close");          

run("Close All");                              

       }                                
  }
}      

---------------------end code----------------
--------------------------------------------
Christine Labno, Ph.D.
Asst. Technical Director
Light Microscopy Core
University of Chicago
Office of Shared Research Facilities
KCBD 1250 900 E. 57th St.
(773) 834-9040 (phone)

________________________________________
From: ImageJ Interest Group [[hidden email]] on behalf of cmparkes [[hidden email]]
Sent: Wednesday, November 26, 2014 9:21 AM
To: [hidden email]
Subject: Re: Batch splitting of stacks and saving

Hello I am having a similar problem.

I have Images which I want to split into the three colour channels. I have
worked out the commands that I would like to be performed on the green and
blue images once they have been split, but when it comes to putting it into
a batch command process I don't know how to change my code so that it will
apply to all of the images in the directory I want to use and select the
blue image to do the following steps with.

This is what I would like to happen to the BLUE images:
open("FOLDER NAMEf");
run("Split Channels");
selectWindow[THIS IS WHERE I THINK I NEED TO PUT THE FILE NAME OF THE SPLIT
WINDOW???]

//run("Brightness/Contrast...");
setMinAndMax(0, 50);
run("Close");
run("Enhance Contrast...", "saturated=2 normalize");
run("Find Edges");
setAutoThreshold("Default dark");
//run("Threshold...");
setThreshold(0, 193);
run("Measure");
I would then like the Green and Blue images to be saved to separate
directorys and discard the red image.


I am not a programmer so any help would be greatly appreciated.

Thanks,

Christina



--
View this message in context: http://imagej.1557.x6.nabble.com/Batch-splitting-of-stacks-and-saving-tp4578247p5010643.html
Sent from the ImageJ mailing list archive at Nabble.com.

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html



If you reply to this email, your message will be added to the discussion below:
http://imagej.1557.x6.nabble.com/Batch-splitting-of-stacks-and-saving-tp4578247p5010645.html
To unsubscribe from Batch splitting of stacks and saving, click here.
NAML