按特定顺序在文件上调用ImageJ宏

时间:2015-07-16 13:53:46

标签: r macros imagej

我是ImageJ的新手,所以我甚至不确定我应该寻找什么来寻找这个问题的答案。

我在一个文件夹(TIF)中有一堆图像文件。我已经编写了一个宏来将文件分割成RGB通道,然后将它们保存到同一目录中。输出就是这样的:

-File 1-blue.tif
-File 1-green.tif
-File 1-red.tif
-File 2-blue.tif
-File 2-green.tif
...
etc.

接下来我使用JACoP plugin,它带有两个图像文件,并计算输入之间的各种相关系数/分析。对于每个目录,我想执行以下操作:

设置全局设置:

  1. 在阈值选项卡中设置参数
  2. 选择" Pearson系数"和" M1和M2系数"正在分析中执行
  3. 然后对我目录中的每个文件循环执行以下操作:

    1. 将输入文件1设置为文件X(红色),将文件2设置为文件X(蓝色)
    2. 点击分析
    3. 将输入文件1设置为文件X(红色),将文件2设置为文件X(绿色)
    4. 点击分析
    5. 将输入文件1设置为文件x(蓝色),将文件2设置为文件x(绿色)
    6. 点击分析
    7. 最后:

      1. 保存分析输出日志
      2. 问题在于我不知道如何告诉ImageJ如何按顺序调用每个颜色文件。如何指定它以三个块的形式提取文件,分析它们,然后继续下一个三个块?

        我只需要一些基本的帮助来解决一般算法和我可能需要的功能 - 我可以自己编写实际的宏代码。

        编辑:我刚刚想到我可以通过使用R将文件传递给三个一组的ImageJ宏。像这样:

        file_list <- list.files(getwd())
        rgbFiles <- file_list[grep(pattern = "blue|green|red", file_list)]
        rgbFilesSplit <- split(rgbFiles, ceiling(seq_along(rgbFiles)/3))
        

        因此,在设置工作目录后,我只是提取出rgb文件并将它们拆分成一个列表。

        如果这种方法有效,那么我如何在子列表中描述的每个文件上调用ImageJ宏?我已经知道如何使用R system()函数调用宏并指定输入目录,但是如何让ImageJ接收输入文件,这些文件只是从上面的R代码生成的文件名字符串?

1 个答案:

答案 0 :(得分:0)

我在ImageJ mailing list上问了这个问题。用户Joost Willemse在形成我的最终宏时非常有帮助。这是完整的宏:

dir=getDirectory("Choose a Directory");
list = getFileList(dir);
Array.sort(list);

for (i=0; i<list.length; i+=3) {
    open(list[i]);
    blue=getTitle;

    open(list[i+1]);  
    green=getTitle;

    open(list[i+2]);
    red=getTitle;

    run("JACoP ", "imga="+red+" imgb="+blue+" thra=648 thrb=517 pearson mm");
    run("JACoP ", "imga="+red+" imgb="+green+" thra=648 thrb=517 pearson mm");
    run("JACoP ", "imga="+blue+" imgb="+green+" thra=648 thrb=517 pearson mm");

    close(red);
    close(green);
    close(blue);
}

设置目录后,for循环开始。 i+=3让迭代器按三步计数(当我提出问题时,我不知道这是可能的)!现在打开三个图像中的每一个并保存它们的标题。最后,标题通过串联发送到run()函数的字符串部分。然后关闭图像。只要您的列表在开始之前在目录中正确排序,这应该可以正常工作。 确保在JACoP的run()功能中设置阈值

此外,我使用R中的grep()gsub()从日志中去除了系数值。不是最有效的方法,但它可以完成工作。无论您从日志文件中提取什么内容,都可以根据需要修改此代码:

# This function takes the path to the log file. It then removes the Pearson's Coefficent, Manders M1/M2, and thresholded Manders M1/M2. It then gathers them into a table. 
extract <- function(data){
    dat <- read.table(data, header = FALSE, sep = "", fill = TRUE, stringsAsFactors = FALSE)
    dat <- dat[ , "V1"]
    coef <- dat[grep(pattern = "=", dat)]
    coef <- as.numeric(gsub("r=|M1=|M2=", "",coef))
    coef <- split(coef, ceiling(seq_along(coef)/5))
    coef <- do.call(rbind.data.frame, coef)
    names(coef) <- c("r", "M1", "M2", "M1(T)", "M2(T)")
    coef <- cbind(Value = c("Red/Blue", "Red/Green", "Blue/Green"), coef)

    return(coef)
}

请注意,需要修改coef <- as.numeric(gsub("r=|M1=|M2=", "",coef)),具体取决于您从日志文件中提取的内容。同样适用于coef <- split(coef, ceiling(seq_along(coef)/5)) - 将5更改为日志文件报告的内容数。

# Now just split the table into a list for each of the different analysis combinations fed into JACoP. Here I assume you set the output of the extract function to "dat".

output <- split(dat, dat$Value)

输出是输入宏的每个图像的所有分析值的列表除以所分析的不同JACoP颜色通道。例如:

$`Red/Blue`                 
Value       r           M1          M2          M1(T)       M2(T)
Red/Blue    0.743871077 0.395698602 0.963246489 0.513951407 0.700130944
Red/Blue    0.460021089 0.605613993 0.456788982 0.125648321 0.424468211
Red/Blue    0.967115553 0.357528694 0.767577893 0.073250688 0.720399867

$`Red/Green`                
Value       r           M1          M2          M1(T)       M2(T)
Red/Green   0.79367778  0.36556424  0.722980958 0.487698812 0.381559727
Red/Green   0.262211518 0.063695185 0.653330753 0.276610328 0.132548249
Red/Green   0.483240639 0.348516661 0.961846834 0.832706515 0.356203613

$`Blue/Green`                   
Value       r           M1          M2          M1(T)       M2(T)
Blue/Green  0.549159913 0.834823152 0.389143503 0.655878106 0.446664812
Blue/Green  0.144388419 0.844781823 0.534304211 0.79041495  0.844326066
Blue/Green  0.805481028 0.344139017 0.490682901 0.246814106 0.641006611
相关问题