简体   繁体   English

ImageJ宏(Rhino)中奇怪的Javascript循环行为

[英]Strange Javascript loop behavior in ImageJ macro (Rhino)

The loop 循环

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

gives output 提供输出

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

However, uncommenting imageRows = 6; 但是,取消注释的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));
    }

gives the expected: 给出预期:

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 uses the Rhino engine to run Javascript macros. ImageJ使用Rhino引擎运行Javascript宏。

NOTE: The above loop is nested inside another loop. 注意:上面的循环嵌套在另一个循环内。 But for debugging purposes, I commented out all other lines in the outer loop. 但是出于调试目的,我注释掉了外循环中的所有其他行。

Edit: For what it's worth, I x'd out rest of the outer loop to: 编辑:对于它的价值,我将外循环的其余部分移至:

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));
    } 

} 

and exactly same behavior. 和完全相同的行为

Steven Lacks led me in the right direction. 史蒂文·拉克斯(Steven Lacks)带领我朝着正确的方向前进。 imageRows was not indeed a valid number. imageRows确实不是有效数字。 I was reading it in from a file: 我正在从文件中读取它:

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

When I checked the type: 当我检查类型时:

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

I got object . 我有object

When I changed to: 当我更改为:

var imageRows = parseInt(dataLine[1]);

it worked. 有效。

Writing Javascript macros in ImageJ is surprisingly tricky. 在ImageJ中编写Javascript宏出奇的棘手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM