【问题标题】:Strange Javascript loop behavior in ImageJ macro (Rhino)ImageJ 宏(Rhino)中奇怪的 Javascript 循环行为
【发布时间】:2015-12-16 19:15:50
【问题描述】:

循环

   //imageRows = 6;
    print("imageRows: " + imageRows);
    for (var gridY = 1 ; gridY < imageRows + 1 ; gridY++)
    {
        print("imageRows: " + imageRows + " gridY: " + gridY + " gridY < imageRows + 1: " + (gridY < imageRows + 1));
    } 

给出输出

imageRows: 6
imageRows: 6 gridY: 1 gridY < imageRows + 1: true
imageRows: 6 gridY: 2 gridY < imageRows + 1: true
imageRows: 6 gridY: 3 gridY < imageRows + 1: true
imageRows: 6 gridY: 4 gridY < imageRows + 1: true
imageRows: 6 gridY: 5 gridY < imageRows + 1: true
imageRows: 6 gridY: 6 gridY < imageRows + 1: true
imageRows: 6 gridY: 7 gridY < imageRows + 1: true
imageRows: 6 gridY: 8 gridY < imageRows + 1: true
imageRows: 6 gridY: 9 gridY < imageRows + 1: true
    .....
imageRows: 6 gridY: 59 gridY < imageRows + 1: true
imageRows: 6 gridY: 60 gridY < imageRows + 1: true

但是,取消注释 imageRows = 6;

    imageRows = 6;
    print("imageRows: " + imageRows);
    for (var gridY = 1 ; gridY < imageRows + 1 ; gridY++)
    {
        print("imageRows: " + imageRows + " gridY: " + gridY + " gridY < imageRows + 1: " + (gridY < imageRows + 1));
    }

给出预期:

imageRows: 6
imageRows: 6 gridY: 1 gridY < imageRows + 1: true
imageRows: 6 gridY: 2 gridY < imageRows + 1: true
imageRows: 6 gridY: 3 gridY < imageRows + 1: true
imageRows: 6 gridY: 4 gridY < imageRows + 1: true
imageRows: 6 gridY: 5 gridY < imageRows + 1: true
imageRows: 6 gridY: 6 gridY < imageRows + 1: true

ImageJ 使用 Rhino 引擎运行 Javascript 宏。

注意:上面的循环嵌套在另一个循环中。但出于调试目的,我注释掉了外循环中的所有其他行。

编辑:为了它的价值,我将外部循环的其余部分改成:

numImages = 1;
for (var imageNumber = 1 ; imageNumber < numImages + 1 ; imageNumber++)
{

    imageRows = 6;
    print("imageRows: " + imageRows);
    for (var gridY = 1 ; gridY < imageRows + 1 ; gridY++)
    {
        print("imageRows: " + imageRows + " gridY: " + gridY + " gridY < imageRows + 1: " + (gridY < imageRows + 1));
    } 

} 

和完全相同的行为。

【问题讨论】:

    标签: javascript for-loop rhino imagej imagej-macro


    【解决方案1】:

    Steven Lacks 带领我朝着正确的方向前进。 imageRows 确实不是有效数字。我正在从文件中读取它:

    importClass(Packages.ij.IJ);
    ....
    var gridConfigurationRawData = IJ.openAsString(folder + IMAGE_GRID_CONFIGURATION_FILENAME); 
    var arrayOfData = gridConfigurationRawData.split("\n");
    ....
    var imageRows = dataLine[1];
    

    当我检查类型时:

    print("typeof imageRows: " + (typeof imageRows));
    

    我收到了object

    当我改为:

    var imageRows = parseInt(dataLine[1]);
    

    成功了。

    在 ImageJ 中编写 Javascript 宏非常棘手。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多