【问题标题】:Calling an ImageJ macro on files in a specific order以特定顺序对文件调用 ImageJ 宏
【发布时间】:2015-07-16 13:53:46
【问题描述】:

我是 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 系数”来执行

然后为我目录中的每个文件循环以下内容:

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

最后:

  1. 保存分析输出日志

问题是我不知道如何告诉 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 代码生成的文件名字符串?

【问题讨论】:

    标签: r macros imagej


    【解决方案1】:

    我在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() 函数的字符串部分。然后图像被关闭。只要您的列表在开始之前在目录中正确排序,就可以正常工作。 确保在run() 函数中为 JACoP 设置阈值!!

    此外,我在 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 &lt;- as.numeric(gsub("r=|M1=|M2=", "",coef)) 需要根据您从日志文件中提取的内容进行修改。 coef &lt;- 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-10
      • 2016-12-23
      • 1970-01-01
      相关资源
      最近更新 更多