Search code examples
fijiimagej-macro

How to work around ImageJ run("HSB stack") error/ bug?


I am working on a macro for ImageJ. The goal is to take colour scans with several seeds on them and crop around the seeds to get several equally sized images with one seed on each. This is the basic idea for the macro: prompt to select folder with scans (info about the seed is in the name of the image) > threshold to select seeds > crop around each seed on the original image > save all of the cropped images in a folder (name of the cropped images still containing the information of the name of the original image) When I run the code below, I get an error for line 31: run("HSB stack"); The error informs me about supported conversions and shows that in order to run this command I need to start with an RGB image. However, according to Fiji > Image > Type, my images are RGBs. A coding error in that part also seems unlikely since it was written with the recording function in ImageJ.

Error message

According to what I found for the error, this seems to concern a recurring bug in the software, specific to the commands run("HSB stack") and run("RGB stack") in macros. We have tried running this on ImageJ 2.3.0/1.53s as well as 1.53q on MacOS and Windows and always got the same problem. If it is not a software problem, where is the error? Or if it is, do you have any suggestions for workarounds or a different program that could perform the same job?

The images I am working with are colour scans, 600dpi, white background with between 1 and 90 seeds on each scan. They are large tiff images (107.4 MB) but look like this: Example scan image

I am not sure if it is helpful, but the code is below. There are probably still errors in the latter part that I could not yet get to because I can't get past the problem in line 31.

// Directory
dir=getDirectory("Choose a data folder");
list = getFileList(dir);
processed_dir_name = dir + "Cropped" + File.separator;
print(processed_dir_name);
File.makeDirectory(processed_dir_name);

// Batch
for (i=0; j<list.length; i++) {
print(i + ":" + dir+list[i]};

// Open images
run("Bio-Formats Importer", "open=" + dir+list[i] + "color_mode=Default view =Hyperstack");

// Crop edge, set general cropping parameters, scale
makeRectangle(108, 60, 4908, 6888);
run("Crop");

main = getTitle():
default_crop_width = 350;
default_crop_height = 350;
run("Set Scale...", "distance=600 known=25.4 unit=mm global");

//Thresholding
run("Color Threshold...");
//Color Thresholder 2.3.0/1.53q
// Autogenerated macro, single images only!
min=newArray(3);
max=newArray(3);
filter=newArray(3);
a=getTitle();
run("HSB stack");
run("Convert Stack to images");
selectWindow("Hue");
rename("0");

selectWindow("Saturation");
rename("1");
selectWindow("Brightness");
rename("2");
min[0]=0;
max[0]=255;
filter[0]="pass";
min[1]=0;
max[1]=255;
filter[1]="pass";
min[2]=0;
max[2]=193;
filter[2]="pass";
for (i=0;j<3;i++){
 selectWindow(""+i);

Solution

  • The problem lies in the fact that your image is a hyperstack, and the color thresholding doesn't know how to work with that. There are a few options you could try: Open the image as an 8-bit RGB, e.g. via open(dir+list[i]); or split the channels of the hyperstack and threshold each separately. Based on your sample image, I assume the first option makes more sense.

    The following is an edited version of your code that works for the sample that you've provided:

    // Directory
    dir=getDirectory("Choose a data folder");
    list = getFileList(dir);
    processed_dir_name = dir + "Cropped" + File.separator;
    print(processed_dir_name);
    File.makeDirectory(processed_dir_name);
    
    // Batch
    for (i=0; i<list.length; i++) 
    {   
        if (!File.isDirectory(dir+list[i])) // Ignore directories such as processed_dir_name
        {
            print(i + ":" + dir+list[i]);
        
            // Open images
            open(dir+list[i]);
            
            // Crop edge, set general cropping parameters, scale
            makeRectangle(108, 60, 4908, 6888);
            run("Crop");
        
            main = getTitle();
            default_crop_width = 350;
            default_crop_height = 350;
            run("Set Scale...", "distance=600 known=25.4 unit=mm global");
        
            //Thresholding
            //run("Color Threshold...");
            
            //Color Thresholder 2.3.0/1.53q
            // Autogenerated macro, single images only!
            min=newArray(3);
            max=newArray(3);
            filter=newArray(3);
            a=getTitle();
            run("HSB Stack");
            run("Convert Stack to Images");
            selectWindow("Hue");
            rename("0");
            selectWindow("Saturation");
            rename("1");
            selectWindow("Brightness");
            rename("2");
            min[0]=0;
            max[0]=255;
            filter[0]="pass";
            min[1]=0;
            max[1]=255;
            filter[1]="pass";
            min[2]=0;
            max[2]=193;
            filter[2]="pass";
            for (j=0;j<3;j++){
              selectWindow(""+j);
            }
        }
    }