ImageJ JavaScript,我如何循环浏览目录

ImageJ JavaScript, how do I cycle through directories?

本文关键字:循环 浏览 JavaScript 何循环 ImageJ      更新时间:2023-09-26

我正在尝试创建一个脚本,该脚本将循环浏览目录并打开图像并对其进行分析等。

所以我想将类似下面的脚本的东西转换为循环;

//sets the pixel height and width
height = 15; 
width = 15;
//navigates to the folder and opens the image
IJ.run("Raw...", "open=[C:''Users''Documents''Ru''simulations_Ru''Batch1_sc''1 Atoms''15x15_stem_image00001_total_df_00001.bin] image=[64-bit Real] width=15 height=15 offset=0 number=1 gap=0");

imp = IJ.getImage();
//selects a region of interest I want to make measurements from
imp.setRoi(0, 0, width, height);

//makes the measurement
IJ.run(imp, "Measure", "");

//closes the image
imp.close();

我想知道的是我如何将其转换为循环(而不必为每个文件夹键入命令),以便它遍历特定文件夹的数量

我是如何阅读它的,问题更多的是关于找出如何迭代目录中的文件。几个月前我一直在寻找这个,发现很难获得正确的信息,因为关于 JavaScript 的大多数东西都在处理在浏览器中运行,并且根本无法访问"本地"文件系统......

无论如何,在 ImageJ 中,这可以通过以下 JavaScript 代码实现:

importClass(Packages.java.io.File);
var foo = File('/tmp/');
dirlist = foo.list();
for (var i = 0; i < dirlist.length; i++) {
    print(dirlist[i]);
}

因此,在"for"循环中,您可以打开文件,处理步骤并将结果写入新文件。

height = 15; 
width = 15;
var cmds = //cmds is a shortage of commands
[
     "open=[C:''Users''Documents''Ru''simulations_Ru''Batch1_sc''1 Atoms''15x15_stem_image00001_total_df_00001.bin] image=[64-bit Real] width=15 height=15 offset=0 number=1 gap=0",
     "another command",
     "and so on..."
];
for(var i in cmds)
{
  IJ.run("Raw...", cmds[i]);

  imp = IJ.getImage();
  imp.setRoi(0, 0, width, height);
  IJ.run(imp, "Measure", "");
  imp.close();
}