Search code examples
javaimage-processingmacrosprofileimagej

Automating the measurement of two profiles of a colour merged image in FIJI (ImageJ)


I've been attempting to write a macro that measures and saves (as a .csv) the pixel intensity profile along a linear ROI of two images merged in the red and green colour channels. The output file would ideally be a single .csv with the x-axis values in one column and the two y-axis values in two additional columns.

The image in question is a stack and the ROI will be user defined every time which both seem to compound issues.

I started by using the BAR "Multichannel Plot Profile" plugin which gives me exactly what I need (plot and .csv format) but I'm struggling when it comes to automating the saving of the data in raw form since you can't macro record clicking the "Data" > "Save Data..." buttons on the plot screen.

I'm aware there are solutions using getProfile() but this only samples the first colour channel. I'm unsure how to modify this to measure two y-axis values in separate channels.

I've tried automatic channel splitter and measuring both channels individually but there are issues with the ROI when the composite is split. Plus this is an image stack which channel splitter doesn't like.

Any help appreciated. Cheers


Solution

  • Below please find an ImageJ demo-macro that generates a table showing in the first column the selection distances, in the next two columns the selection values in the red and green channel of the first slice of the stack and in the following columns the red/green selection values of the remaining slices. The table can finally be saved in csv-format by specifying the directory.

    (The demo requires an open internet connection in order to load the demo stack.)

    //imagej-macro "profilesOfComposite" (Herbie G., 13. March 2024)
    requires("1.54i");
    run("Confocal Series"); // load demo stack
    makeLine(199,179,199,219); // demo selection
    Stack.getDimensions(na,na,na,sl,na);
    Stack.getUnits(xyU,na,na,na,vU);
    p=getProfile();
    d=Array.getSequence(p.length);
    toScaled(d,newArray(0));
    tbl="Profiles";
    Table.create(tbl);
    Table.setColumn("Distance ["+xyU+"]",d,tbl);
    for (j=1;j<=sl;j++) {
       Stack.setSlice(j);
       Stack.setChannel(1);
       p=getProfile();
       Table.setColumn("Red #"+j,p,tbl);
       Stack.setChannel(2);
       p=getProfile();
       Table.setColumn("Green #"+j,p,tbl);
    }
    Table.save("");
    exit();
    //imagej-macro "profilesOfComposite" (Herbie G., 13. March 2024)