How to batch process ImageJ macro within a specific folder within multiple directories - imagej

I have a nested file structure where a parent folder contains multiple folders of different types of data. I am using an ImageJ macro script to batch process all of the image files within one of those folders. I currently need to process each folder separately, but I would like to batch process over the folders. I have looked up some batch processing of multiple folders, but it appears that the code is processing all folders and files within all of the folders. I only need to process one folder within each directory (all named the same). The images come from the instrument without any metadata, so the files are saved as such to separate the experiments, where all data for the experiment is contained within the parent folder. Also, I have two different scripts that I need to run, one after the other. It would be great if I could merge those, but I am don't know how to do that either.
An example of the structure is:
Experiment1/variable1/processed
Experiment1/variable2/processed
I am currently running my macro on each of the "processed" folders individually. I would like to batch each "processed" folder within each of the "variable" folders.
Any help would be greatly appreciated, I am really new to coding and am just really trying to learn and automate as much as possible.
Thank you!

Did you try the batch processing scripts you came across? Reading the batch processing example which is provided with ImageJ leads me to believe it would work for your example. If you haven't tested it, you should do so (you can put in a command like "print(list[i])" in the place of your actual macro while you test that you've got the file finding section working.
To merge two different scripts, the simplest option would be to make them individual functions. i.e.:
// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
list = getFileList(input);
list = Array.sort(list);
for (i = 0; i < list.length; i++) {
if(File.isDirectory(input + File.separator + list[i]))
processFolder(input + File.separator + list[i]);
if(endsWith(list[i], suffix))
processFile(input, output, list[i]);
processOtherWay(input, output, list[i]);
}
}
function processFile(input, output, file) {
// Do the processing here by adding your own code.
// Leave the print statements until things work, then remove them.
print("Processing: " + input + File.separator + file);
print("Saving to: " + output);
}
function processOtherWay(input, output, file) {
// Do the processing here by adding your own code.
// Leave the print statements until things work, then remove them.
print("Processing: " + input + File.separator + file);
print("Saving to: " + output);
}
If the goal isn't to run them on the exact same image, then again make them standalone functions, and have the folder sorting section of the script be in two parts, one for function 1, one for function 2.

You can always just take the code you have and nest it in another for loop or two.
numVariables = ;//number of folders of interest
for(i = 1; i <= numVariables; i++) //here i starts at 1 because you indicated that is the first folder of interest, but this could be any number
{
openPath = "Experiment1/variable" + i + "/processed";
files = getFileList(openPath);
for(count = 0; count < files.length; count++) //here count should start at 0 in order to index through the folder properly (otherwise it won't start at the first file in the folder)
{
//all your other code, etc.
}
}
That should just about do it I think.

Related

Having two dependent actions in a single rule

Edit: The example below did actually work, I misinterpreted the output the compiler gave me. The answer may still be helpful to some.
Is there a way for an action in a rule to generate a file that is consumed by a later action in that same rule?
E.g.:
def _example_rule_impl(ctx):
thefile = ctx.actions.declare_file("required_file.json")
ctx.actions.write(
output = thefile,
content = "CONTENT",
)
args = ctx.actions.args()
args.add("--config", thefile)
ctx.actions.run(
inputs = ctx.files.srcs + ctx.files.deps + [thefile],
outputs = outs,
arguments = [args],
progress_message = "Compiling...",
executable = ctx.executable._compiler,
)
The main problem with this seems to be, that all action outputs seem to be written to bazel-out, but the run action requires the generated file to be written next to the srcs and deps files in the execroot for it to work. Is there a way to have an action write to the execroot or is this not the correct approach?
Actions taking the outputs of other actions as inputs is a very typical thing to do, and should basically just work as you've set it up. Bazel takes care of handling input and output files, e.g., on linux using a lot of symlinks, mounting things in sandboxes, uploading and downloading files for remote execution, etc.
See
https://docs.bazel.build/versions/main/output_directories.html
Also, regarding this line:
inputs = ctx.files.srcs + ctx.files.deps + [thefile],
Depending on what you need to do, you may want to use depsets for performance reasons. See https://docs.bazel.build/versions/main/skylark/depsets.html
and in particular at the end https://docs.bazel.build/versions/main/skylark/depsets.html#performance

Calculating surface and storing as Excel file

I'm new to imageJ and I am trying to analyze several images:
I have a code that can analyze color threshold for a set of images in a directory and store them separately:
input = "/m_3/ImageJ/test_folder/";
output = "/m_3/ImageJ/finished2/";
function action(input, output, filename) {
open(input + filename);
run("Set Scale...", "distance=872 known=9 pixel=1 unit=cm");
run("Color Threshold...");
// Color Thresholder 1.48v
// Autogenerated macro, single images only!
.
.
.
// Colour Thresholding-------------
saveAs("Jpeg", output + filename);
close();
}
setBatchMode(true);
list = getFileList(input);
for (i = 0; i < list.length; i++)
action(input, output, list[i]);
setBatchMode(false);
Now I want to calculate the area of the newly saved images and this should work with the function measure..
run("Measure");
How can I store the calculations in .xls or .csv -files?
Is it also possible to calculate the area of all the files in one directory and store the results in just one .xls or .csv -file?
Have a look at the explanation how to apply a common operation to a complete directory on the Fiji wiki. You can open each of the newly save images, set a threshold, measure, and close each image as follows:
open("/path/to/your/image.jpg");
setAutoThreshold("Default");
run("Measure");
close();
and paste that code into the Process Folder template (Templates > IJ1 Macro > Process Folder) of the script editor.
The results table can afterwards be saved as an .xls file via File > Save As... or:
saveAs("Results", "/path/to/your/file.xls");

CopyBooks,.cpy files,WTX Design Studio

I am working on WTX design Studio with copybooks,i have a copybook but i dont have any corresponding sample input regarding that .cpy file .
Are there any means to generate a sample text file from copy book rather than writing the text document manually?
As bill said there are lots of ways depending on the tools available:
Ask for sample file
There are packages that can generate data. As I do not work at your site I do not know if any have been installed. They tend to be expensive though
Write a Cobol program (I presume the source is a mainframe) so it would have to be done on the mainframe.
I presume the source is a Mainframe, get on the Mainframe and use FileAid (or FileMaster or whatever they have) to setup the file. FileAid and its ilk let you edit files with a Cobol copybook.
Use the RecordEditor to create the file. You can import Cobol Copybooks into the RecordEditor and then use them to edit Cobol-Data files.
Use a RecordEditor macro to generate a file;
Write a Java / jython / JRuby program with Cobol interface package (have a look on sourceforge)
There are a lot of other possibilities, as I do not know what software or skills you have I can not really advise.
RecordEditor Macro to generate some numeric data:
/******************************************************************
* Purpose: RecordEditor Example Macroto generate numeric data for a file
*
* It is best to run this script from a Single Record Screen rather than
* a Table screen
*
*******************************************************************/
var rec = layout.getRecord(0)
var lines = RecordEditorData.view.createLines(20)
for (lineNo = 0; lineNo < 20; lineNo++) {
print(lineNo);
for (i=0; i < rec.getFieldCount(); i++) {
try {
lines[lineNo].getFieldValue(0, i).set(lineNo * 100 + i)
} catch(err) {
lines[lineNo].getFieldValue(0, i).set(i % 10)
}
}
}
RecordEditorData.view.addLines(-1,1, lines)
Output from the Macro:

Opencv Reading Multiple images from Multiple folder

I have a folder which contain 100 or more sub folders and each of them contain many images.(Caltech Database)
I have problem with how to read them from the different folders ?
I want to store them as a single matrix with stacking columms of each image.
Use Boost Filesystem for C++. You can load all files in a directory and pass the file location to OpenCV in a string.
string folder = "../images/";
vector<string> imageFileLocations;
namespace fs = boost::filesystem;
vec v;
copy(fs::directory_iterator(folder), fs::directory_iterator(), back_inserter(v));
sort(v.begin(), v.end());
for (vec::const_iterator it(v.begin()); it != v.end(); ++it) {
if (fs::is_regular_file(*it)) {
string location = it->string();
imageFileLocations.push_back(location);
}
}
You'll have to add something recursive to be able to go into other folders. You can do that by checking if the iteraotr is at a file or a folder. See the Boost website for examples.

How to write a simple .txt content processor in XNA?

I don't really understand how Content importer/processor works in XNA.
I need to read a text file (Content/levels/level1.txt) of the form:
x x
x x
x x
where x's are just integers, into an int[,] array.
Any tips on writting a SIMPLE .txt importer??? By searching google/msdn I only found .x/.fbx file importer examples. And they seem too complicated.
Do you actually need to process the text file? If not, then you can probably skip most of the content pipeline.
Something like:
string filename = "Content/TextFiles/sometext.txt";
string path = Path.Combine(StorageContainer.TitleLocation, filename);
string lineOfText;
StreamReader sr = new StreamReader(path);
while ((lineOfText = sr.ReadLine()) != null)
{
// do something
}
Also, be sure to set the "Build Action" to "None" and the "Copy to Output Directory" to "Copy if newer" on the text files you've added. This tells the content pipeline not to compile the text file but rather copy it to the output directory for use as is.
I got this (more or less) from the RacingGame sample provided by Microsoft. It foregoes much of the content pipeline and simply loads and processes text files (XML) for much of its level data.
XNA 4.0 uses
System.IO.Stream stream = TitleContainer.OpenStream("tilename.txt");
See http://msdn.microsoft.com/en-us/library/bb199094.aspx and also http://blogs.msdn.com/b/shawnhar/archive/2010/12/09/reading-files-in-xna-game-studio-4-0.aspx
There doesn't seem to be a lot of info out there, but this blog post does indicate how you can load .txt files through code using XNA.
Hopefully this can help you get the file into memory, from there it should be straightforward to parse it in any way you like.
XNA 3.0 - Reading Text Files on the Xbox
http://www.ziggyware.com/readarticle.php?article_id=69 is probably a good place to start. It covers creating a basic content processor.

Resources