ImageJ 宏遍历文件夹,一次合并三个图像,在第一次迭代期间跳过一个图像

问题描述 投票:0回答:2

我正在制作一个脚本来将通道合并到 4D 堆栈中。它应该一次在一个文件夹中获取三张图像,合并它们,然后保存合成图像。

我不明白的是,在第一次迭代中,它跳过第三张图像(仅打开前两张),然后从第三张图像开始下一次迭代。所以只有第一次迭代是错误的,但它并不像它一起跳过第一张图像。

// Specify the directory containing the images
dir = getDirectory("Select a directory");
output = dir + "Merged" + File.separator; 
File.makeDirectory(output);

// Get a list of all the files in the directory
list = getFileList(dir);

// Loop through the list of files, three at a time
for (i = 0; i < list.length; i += 3) {
    if (endsWith(list[i], ".tif")) {
        open(dir + list[i]);
    }

    if (i + 1 < list.length) {
        if (endsWith(list[i + 1], ".tif")) {
            open(dir + list[i + 1]);
        }
    }

    if (i + 2 < list.length) {
        if (endsWith(list[i + 2], ".tif")) {
            open(dir + list[i + 2]);
        }
    }
    
    // Get name of the image without channel number
    imageName = substring(getTitle(), 0, (lengthOf(getTitle())-9));
    imageList = getList("image.titles");
    Array.sort(imageList);
    
    // Merge the channels and save
    run("Merge Channels...", "c1=" + imageList[0] + " c2=" + imageList[1] + " c7=" + imageList[2]);
    rename(imageName + "_merged");
    
    saveAs("Tiff", output + getTitle() + ".tif");
    close("*");
}

用打印数组命令和 waitForUser 替换合并通道命令后,我可以看到它打开图像 1、2,提示 waitForUser,打开 3、4、5,提示 waitForUser 等。

我觉得我犯的错误很小,但我似乎找不到它..

for-loop image-processing batch-processing imagej fiji
2个回答
0
投票

我不知道答案是否可能这么简单......但我注意到这里:

//合并通道并保存 run("合并通道...", "c1=" + imageList[0] + " c2=" + imageList[1] + " c7=" + imageList[2]); 重命名(imageName + "_merged");

你已经写了c1、c2,然后是c7...我尝试在我自己的图像上运行这个(也是三个一组),并将c7更改为c3,现在它似乎可以工作了:P


0
投票

可能回答有点晚了,但是,我遇到了只能打开两个文件的问题,并且似乎完全跳过了第一个“打开”命令。

玩了一段时间后,我打开了三个文件,但只有当我列出了 4 个“if ... open”的交互时。 经过一番尝试后,我做了下面详细介绍的小更改,这让它打开了所有三个文件,仅用 3 个打开命令即可对文件进行一些重命名和保存,然后打开列表中的下 3 个文件并重复:

for (i = 0; i < list.length; i +=3) {           //Adjust += number to appropriate number of channels
file = list[i];
if (i < list.length) {
    if (file.endsWith(".tif")) {
        open(dir+file);
    }
}
if (i + 1 < list.length) {
    if (file.endsWith(".tif")) {
        open(dir + list[i + 1]);
    }
}
if (i + 2 < list.length) {
    if (file.endsWith(".tif")) {
        open(dir + list[i + 2]);
    }
}

等等,我猜你想要多少个频道。

我无法说明您的代码对我有多大帮助! 我对斐济宏相当陌生,并且一直在尝试找出如何同时打开多个 tif 文件。

希望这有帮助,再次感谢打开多个文件的代码!

© www.soinside.com 2019 - 2024. All rights reserved.