为什么我的for循环不增加增量?

时间:2020-03-05 20:54:49

标签: loops for-loop imagej

我正在imagej中编写宏,以对文件夹中的照片进行批处理分析。当我运行宏时,代码似乎只运行一次,而没有移动到下一个文件。我相信这是我的代码块中的问题,而不是实际的for语句。如果有人能指出造成这种情况的原因,我将不胜感激。代码复制如下。 for循环从第23行开始,它运行的函数在第32行。

Dialog.create("Our Batch Analysis");    //providing choises about analysis 
Dialog.addNumber("Min Size", 1000);
Dialog.addNumber("Max Size", 9999999);
Dialog.addCheckbox("View Obect Outlines", false);

smoothArray=newArray("Mean...", "Median...", "None");
Dialog.addChoice("Smooth Filter", smoothArray, "Mean");

Dialog.show();

our_min=Dialog.getNumber();     //assigning the collected values to variables to be used in analyzeImage
our_max=Dialog.getNumber();
our_outlines=Dialog.getCheckbox();
our_smooth=Dialog.getChoice();

our_dir=getDirectory("Choose Source Directory");    //Choosing the folder to analyze 
our_list=getFileList(our_dir);


for(i=0; i<our_list.length; i++)    //cycling through images in folder
{
if(endsWith(our_list[i],".jpg"))        //only analyzing images no text files
{
open(our_dir + our_list[i]);
analyseImage();
}
}

analyseImage();     //declaring function 
function analyseImage()
{
origTitle= getTitle () ;    //creating a callable title 
run("Duplicate...", "title=duplicate");  //making a duplicate file to work with without affecting original
run("Gaussian Blur...", "sigma=2");
if(our_smooth!="None")      //making smoothing conditional
{run(our_smooth, "radius=2");
setAutoThreshold("Default");        //setting a threshold to differentiate background
setThreshold(0, 127); 
setOption("BlackBackground", true);
run("Convert to Mask");     //turning the image binary
run("Watershed");
run("Set Measurements...", "area mean shate display redirect=[" + origTitle + "] decimal =2");

if(our_outlines==true)
{
run("Analyze Particles...", "size=" +our_min+"-"+our_max+" circularity =0.80-1.00 show=Outlines display exclude");  
rename(origTitle+"-outlines");
selectWindow(origTitle+"-outlines");
close();
}
else
{
run("Analyze Particles...", "size=" +our_min+"-"+our_max+" circularity =0.80-1.00 show=Nothing display exclude");   //counting how many particles exist based on given parameters
}

selectImage(origTitle); //closing unwanted tabs
close();    
selectImage("duplicate");
close();
}

1 个答案:

答案 0 :(得分:0)

该函数未完成,因此for循环没有增加。您可以在函数的末尾添加return语句,以告知循环该函数已完成。我还提高了代码的可读性。请注意,不需要额外调用analyseImage()

Dialog.create("Our Batch Analysis");    //providing choices about analysis 
Dialog.addNumber("Min Size", 1000);
Dialog.addNumber("Max Size", 9999999);
Dialog.addCheckbox("View Obect Outlines", false);

smoothArray=newArray("Mean...", "Median...", "None");
Dialog.addChoice("Smooth Filter", smoothArray, "Mean");

Dialog.show();

our_min=Dialog.getNumber();     //assigning the collected values to variables to be used in analyzeImage
our_max=Dialog.getNumber();
our_outlines=Dialog.getCheckbox();
our_smooth=Dialog.getChoice();

our_dir=getDirectory("Choose Source Directory");    //Choosing the folder to analyze 
our_list=getFileList(our_dir);

//cycling through images in folder
for(i=0; i<our_list.length; i++) {
    //only analyzing images no text files
    if(endsWith(our_list[i],".jpg"))    {
        open(our_dir + our_list[i]);
        analyseImage();
    }
}

//analyseImage();     //declaring function 
function analyseImage() {
    origTitle= getTitle () ;    //creating a callable title 
    run("Duplicate...", "title=duplicate");  //making a duplicate file to work with without affecting original
    run("Gaussian Blur...", "sigma=2");
    if(our_smooth!="None")      //making smoothing conditional
        {run(our_smooth, "radius=2");
    setAutoThreshold("Default");        //setting a threshold to differentiate background
    setThreshold(0, 127); 
    setOption("BlackBackground", true);
    run("Convert to Mask");     //turning the image binary
    run("Watershed");
    run("Set Measurements...", "area mean shate display redirect=[" + origTitle + "] decimal =2");

    if(our_outlines==true)  {
        run("Analyze Particles...", "size=" +our_min+"-"+our_max+" circularity =0.80-1.00 show=Outlines display exclude");  
        rename(origTitle+"-outlines");
        selectWindow(origTitle+"-outlines");
        close();
    } else {
        run("Analyze Particles...", "size=" +our_min+"-"+our_max+" circularity =0.80-1.00 show=Nothing display exclude");   //counting how many particles exist based on given parameters
    }

    selectImage(origTitle); //closing unwanted tabs
    close();    
    selectImage("duplicate");
    close();
    return 0;
}
相关问题