ImageJ奇怪的文件保存行为

时间:2015-06-04 18:49:04

标签: imagej

我写了一个imageJ脚本来着色并合并一系列黑白图像。该脚本保存未合并的彩色图像和合并的彩色图像。当我在调试模式下运行并逐步完成脚本时,一切都运行得很好。然而,当我真实地运行它时,它偶尔会保存一些原始的黑色和白色而不是生成的彩色图像。所有合并的图像看起来都很好。

为什么在调试模式下一切正常但在常规使用期间失败?

以下是我的代码:

// Choose the directory with the images
dir = getDirectory("Choose a Directory ");

// Get a list of everything in the directory
list = getFileList(dir);

// Determine if a composite directory exists.  If not create one.
if (File.exists(dir+"/composite") == 0) {
    File.makeDirectory(dir+"/composite")
}

// Determine if a colored directory exists.  If not create one.
if (File.exists(dir+"/colored") == 0) {
    File.makeDirectory(dir+"/colored")
}

// Close all files currently open to be safe
run("Close All");

// Setup options
setOption("display labels", true);
setBatchMode(false);

// Counter 1 keeps track of if you're on the first or second image of the tumor/vessel pair
count = 1;
// Counter 2 keeps track of the number of pairs in the folder
count2 = 1;
// Default Radio Button State
RadioButtonDefault = "Vessel";
// Set Default SatLevel for Contrast Adjustment
// The contrast adjustment does a histogram equalization.  The Sat Level is a percentage of pixels that are allowed to saturate.  A larger number means more pixels can saturate making the image appear brighter.
satLevelDefault = 2.0;

// For each image in the list
for (i=0; i<list.length; i++) {
    // As long as the name doesn't end with / or .jpg 
    if (endsWith(list[i], ".tif")) {
        // Define the full path to the filename
        fn = list[i];
        path = dir+list[i];
        // Open the file    
            open(path);
        // Create a dialog box but don't show it yet
        Dialog.create("Image Type");
        Dialog.addRadioButtonGroup("Type:", newArray("Vessel", "Tumor"), 1, 2, RadioButtonDefault)
        Dialog.addNumber("Image Brightness Adjustment", satLevelDefault, 2, 4, "(applied only to vessel images)")
        // If it's the first image of the pair ...
        if (count == 1) {
            // Show the dialog box
            Dialog.show();
            // Get the result and put it into a new variable and change the Default Radio Button State for the next time through
            if (Dialog.getRadioButton=="Vessel") {
                imgType = "Vessel";
                RadioButtonDefault = "Tumor";
            } else {
                imgType = "Tumor";
                RadioButtonDefault = "Vessel";
            }
        // If it's the second image of the pair
        } else {
            // And the first image was a vessel assume the next image is a tumor
            if (imgType=="Vessel") {
                imgType="Tumor";
            // otherwise assume the next image is a vessel
            } else {
                imgType="Vessel";
            }
        }
        // Check to see the result of the dialog box input
        // If vessel do this
        if (imgType=="Vessel") {
            // Make image Red
            run("Red");
            // Adjust Brightness
            run("Enhance Contrast...", "saturated="+Dialog.getNumber+" normalize");
            // Strip the .tif off the existing filename to use for the new filename
            fnNewVessel = replace(fn,"\\.tif","");
            // Save as jpg
            saveAs("Jpeg", dir+"/colored/"+ fnNewVessel+"_colored");
            // Get the title of the image for the merge
            vesselTitle = getTitle();
        // Othersie do this ... 
        } else {
            // Make green
            run("Green");
            // Strip the .tif off the existing filename to use for the new filename
            fnNewTumor = replace(fn,"\\.tif","");
            // Save as jpg
            saveAs("Jpeg", dir+"/colored/"+ fnNewTumor+"_colored");
            // Get the title of the image for the merge
            tumorTitle = getTitle();
        }
    // If it's the second in the pair ...
    if (count == 2) {
        // Merge the two images
        run("Merge Channels...", "c1="+vesselTitle+" c2="+tumorTitle+" create");
        // Save as Jpg
        saveAs("Jpeg", dir+"/composite/composite_"+count2);
        // Reset the number within the pair counter
        count = count-1;
        // Increment the number of pairs counter
        count2 = count2+1;
   // Otherwise
    } else {
    // Increment the number within the pair counter
    count += 1;
    }
    }
}

2 个答案:

答案 0 :(得分:0)

不确定为什么我需要这样做,但在 public ActionResult CompleteTask(Guid? id) { if (id.HasValue == false) { return HttpNotFound(); } Task task = _service.GetTask(id.Value); if (task == null) { return HttpNotFound(); } 之前立即添加wait(100)似乎可以解决问题

答案 1 :(得分:0)

此方案中的最佳做法是轮询IJ.macroRunning()。如果宏正在运行,此方法将返回true。我建议使用最终会超时的辅助方法,例如:

/** Run with default timeout of 30 seconds */
public boolean waitForMacro() {
  return waitForMacro(30000);
}

/**
 * @return True if no macro was running. False if a macro runs for longer than
 *         the specified timeOut value.
 */
public boolean waitForMacro(final long timeOut) {
    final long time = System.currentTimeMillis();

    while (IJ.macroRunning()) {
        // Time out after 30 seconds.
        if (System.currentTimeMillis() - time > timeOut) return false;
    }

    return true;
}

每当您使用run()open()newImage()时,请调用其中一种辅助方法。

可能需要更多工作但提供更强大解决方案的另一个方向是使用ImageJ2。然后你可以使用ThreadService运行一些东西,它会返回一个Java Future,然后可以保证执行完成。