ImageJ: Export list of histogram for multiple Images - imagej

after hours of trying and searching related topics, I don't get further. I am trying to create a small ImageJ script that splits channels of a picture, saves on .tif version with enhanced contrast (this part works) AND creates a histogram and saves it values as .csv (this part does not work, it doesn't save anything).
I have no idea why it doesn't work so here is my whole code.
input = getDirectory("Input directory");
output = getDirectory("Output directory");
Dialog.create("File type");
Dialog.addString("File suffix: ", ".tif", 5);
Dialog.show();
suffix = Dialog.getString();
processFolder(input);
function processFolder(input) {
list = getFileList(input);
for (i = 0; i < list.length; i++) {
if(File.isDirectory(list[i]))
processFolder("" + input + list[i]);
if(endsWith(list[i], suffix))
processFile(input, output, list[i]);
}
}
function processFile(input, output, filename) {
print("Processing: " + input + filename);
open(input + filename);
run("Split Channels");
selectWindow(filename+" (blue)");
close();
selectWindow(filename+" (green)");
close();
selectWindow(filename+" (red)");
run("Histogram");
getHistogram(values, counts, 256);
saveAs("Results", output + "Histogram of " + filename + ".csv");
close();
saveAs("TIFF", output+ "processed_" + filename);
selectWindow("processed_" + filename);
close();
}
I appreciate every input. Cheers!

not sure you can save it as a csv file using the IJ macro language. However here is some code to do the same thing but save it as a txt file (this example only takes the value from 255)
getStatistics(mean, min, max, std, histogram);
white=histogram[255];
// list2 is the list of images in a for loop - adds image name to results
print(white,",", list2[j]+" smi31");
selectWindow("Log");
saveAs("txt", file1+"Result");
Hope this is helpful :-)

Related

ImageJ batch processing: saving results of each image separately in the csv format

I have around 30 images and use ImageJ macros to analyse the particles (area, circularity, etc.) in the batch. From the macros script that I'm using, I'm not able to save the results separately, I mean one results.csv file per image.
Script:
fileName = getTitle();
run("Set Scale...", "distance=231 known=200 unit=um");
//setTool("rectangle");
run("Select All");
makeRectangle(0, 0, 2560, 1856);
run("Crop");
run("8-bit");
setAutoThreshold("Default");
run("Threshold...");
setThreshold(0, 116);
setOption("BlackBackground", false);
run("Convert to Mask");
//run("Close");
run("Analyze Particles...", "size=80-Infinity display exclude summarize add");
selectWindow("Results"); //activate results table
saveAs("Results", "C:/folder/Results.csv");
In the final line, your script will save a file called Results.csv and then overwrite it when you save the next image. So you need to construct a unique path for the Results for each image.
fileName = getTitle();
run("Set Scale...", "distance=231 known=200 unit=um");
//setTool("rectangle");
run("Select All");
makeRectangle(0, 0, 2560, 1856);
run("Crop");
run("8-bit");
setAutoThreshold("Default");
run("Threshold...");
setThreshold(0, 116);
setOption("BlackBackground", false);
run("Convert to Mask");
//run("Close");
run("Analyze Particles...", "size=80-Infinity display exclude summarize add");
selectWindow("Results"); //activate results table
// construct unique path here using fileName
path = "C:/folder/" + fileName + ".csv";
saveAs("Results", path);

Combine 3 Stacks in ImageJ

Is there a way to "Stack Combine" 3 stacks side by side.
Image>Stacks>Tools>Combine
Supports the combination of 2 stacks, side by side. The two ways around this:
Combine the first 2 then combine the combined image to the third stack
Create a macro that does the above
run("Combine...", "stack1=STAC1_NAME stack2=STACK2_NAME");
run("Combine...", "stack1=[Combined Stacks] stack2=STACK3_NAME");
Is there another way to do this, since, for example combining 20 stacks side-by-side.
Code Snippet added/
//Specify Folders here//
output = "PATH";
combined = "PATH";
original= "PATH";
//Batch Mode for ImageJ
setBatchMode(true);
list = getFileList(input);
for (i = 0; i < list.length; i++) {
combine(original, output, combined, list[i]);
}
setBatchMode(false);
function combine(original, output, combined, filename) {
//Open Outline & Overlay for Combine Stack//
name_outline = filename + "_outline.png";
name_overlay = filename + "_overlay.png";
name_ellipse = filename + "_ellipse.png";
name_original = replace(filename, "_watershed.tif", ".tif");
open(original + name_original);
run("RGB Color");
open(output + name_overlay);
run("RGB Color");
open(output + name_outline);
run("RGB Color");
open(output + name_ellipse);
run("RGB Color");
run("Combine...", "stack1=filename stack2=name_overlay");
rename("combinedstack01");
run("Combine...", "stack1=name_outline stack2=name_ellipse");
rename("combinedstack02");
run("Combine...", "stack1=combinedstack01 stack2=combinedstack02");
saveAs("PNG", combined + filename + "_comb.png");
run("Close All");
}
According to the documentation, no:
http://imagej.net/Stack_Manipulation
http://imagej.net/Stack-slice_Manipulations
However you can easily batch process a whole stack folder with the combine method, see:
http://imagej.net/Batch_Processing
Here an example macro
E.g.(adjust the paths! - stacks are named for this script 'stack1', 'stack2',....):
input = "C:\\Users\\test\\Pictures\\combine";
open("C:\\Users\\test\\Pictures\\combine\\stack1.tif");
rename("combinedStack");
list = getFileList(input);
for (i = 1; i < list.length; i++){
open(list[i]);
title=getTitle();
run("Combine...", "stack1="+title+" stack2=combinedStack");
rename("combinedStack");
}

Imagej macro to save data generated by results and log pages into the same end excel file

I have created a macro that generates results both in the log and results pages. I need a macro that saves both the results and log files into one big excel file and is organized by image name.
input=getDirectory("Choose Source Directory ");
list = getFileList(input);
for (i = 0; i < list.length; i++)
rootangle(input, list[i]);
function angle (input,filename){
open (input + filename);
setTool("angle");
for (i = 0; i < 5; i++){
waitForUser("Select Angle Points");
run("Set Measurements...", " display redirect=None decimal=3");
//run("Measure");
}
setTool("multipoint");
waitForUser("Count");
run("Set Measurements...", " display redirect=None decimal=3");
run("Measure");
}
setTool("line");
waitForUser("Measure");
run("Set Measurements...", " display redirect=None decimal=3");
run("Measure");
}
setTool("freehand");
waitForUser("Distance");
run("Set Measurements...", " display redirect=None decimal=3");
run("Measure");
}
setTool("polyline");
waitForUser("Draw");
run("Fit Spline", "straighten");
getSelectionCoordinates(x, y);
for (i=0; i<x.length; i++)
print(i+" "+x[i]+" "+y[i]);
}
dir=getDirectory("image");
name = "Results";
index = lastIndexOf(name, "\\");
if (index!=-1) name = substring(name, 0, index);
name = name + ".xls"; ///can change xls to csv, txt, etc.
saveAs("Measurements", dir+name);
close();
}
run("Clear Results");
In this macro the polyline generates many many x,y coordinates for each image that may be bulky for the excel file. These coordinates can be listed on one line in the results excel file.
Is there a way you can add manual annotation to the label in the results page?
Also, is there a way you can add pose measurement functions? Like divide the numbers by 2, etc before creating the excel file?
I wrote a macro which saves results of ROI multimeasures to the log window. Since I only wanted specific results (the highest three Raw Integrated Densities), I had my code use getResult("Column Label", row) to get the results out, manipulate them, and then print to the log using print("stuff to print"). At the end, I saved the log as a csv, which one can open in excel. Just use commas to separate

How to get threshold value used by auto threshold Plugin

I have the following code, where I read images from directory and use ImageJ Auto Threshold plugin to segment my images.
dir = getDirectory("path");
list = getFileList(dir);
for (i=0; i<list.length; i++)
{
if (endsWith(list[i], ".tif"))
{
open(dir + list[i]);
run("8-bit");
run("Gaussian Blur...", "sigma=2");
setAutoThreshold("Otsu dark");
run("Convert to Mask");
saveAs("TIFF", dir+list[i]);
close();
}
}
I would like to get the threshold value using "Otsu dark" method, and modify that value (e.g. scale it by a factor) and apply it to my images for segmentation.
In an ImageJ macro, use the getThreshold(lower,upper) and setThreshold(lower,upper) methods (here's the documentation).
Your code would look like this then:
dir = getDirectory("path");
list = getFileList(dir);
factor = 1.5;
for (i=0; i<list.length; i++)
{
if (endsWith(list[i], ".tif"))
{
open(dir + list[i]);
run("8-bit");
run("Gaussian Blur...", "sigma=2");
setAutoThreshold("Otsu dark");
getThreshold(lower,upper);
setThreshold(lower,upper*factor);
run("Convert to Mask");
saveAs("TIFF", dir+list[i]);
close();
}
}
If you plan to do more complicated things, consider using another scripting language like the ones provided by Fiji.

How to convert textbox and label to strings properly

I have this code
FileStream fs = new FileStream("Scores.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.Write("Name: " + name_box.Text + " Time " + label1.Text);
sw.Close();
which is simple the label1 is assgined to a Timer tick as in the folowing
private void timer1_Tick(object sender, EventArgs e)
{
// Format and display the TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
label1.Text = elapsedTime;
}
now when I open the Text File I found the following results
Name: Tony Time 00:00:06.67Text: Time [System.Windows.Forms.Timer], Interval: 100
which are perfect but what is ( Text: Time [System.Windows.Forms.Timer], Interval: 100)
I don't want that to appear in the txt
thanx in advance
You probably have the following line somewhere else in your code:
label1.Text = timer1.ToString();
You should write-click the word label1 in your code, then click Find All References to see what else you're doing with it.
By the way, instead of creating a stream, you should use File.WriteAllText, like this:
File.WriteAllText("Scores.txt", "Name: " + name_box.Text + " Time " + label1.Text);
You are using FileMode.OpenOrCreate in the constructor. This does not erase the previous contents of the file. I suspect that if you delete the file and then try running your program again, you won't see any of that extra stuff.
I suggest either using FileMode.Create or FileMode.Append. Use the first if you want to overwrite the results, the second if you want to... well, append.

Resources