ijpb / MorphoLibJ

Collection of mathematical morphology methods and plugins for ImageJ
http://imagej.net/MorphoLibJ
GNU Lesser General Public License v3.0
98 stars 49 forks source link

Feature Request: Keep/Remove Largest Region in 2D for stacks #53

Open agclark12 opened 3 years ago

agclark12 commented 3 years ago

I often deal with stacks of time series data, where I am segmenting and cleaning segmentations for each frame sequentially. If I would like to keep or remove a largest region for each slice, MorphoLibJ automatically does the labeling in 3D. It would be nice if one could choose to do this in 2D slice-by-slice for a timeseries. That would save a lot of work having to go through the stack and pull out individual frames to do this. Thanks!

dlegland commented 3 years ago

Hi,

Yes, the library currently uses the connectivity withj dimension corresponding to that of the input image. I am not a big fan of providing too much choices for "low-level" operators; the design strategy is rather to build workflows / encapsulate into macros or more general plugins. Maybe you can build such a macro?

Anyway, a trick could be to allow 2D connectivities for 3D arrays. Regions in consecutive slices/frames would therefore have distinct labels. We can think about it.

agclark12 commented 3 years ago

Sure. I understand. I already have already written macros that do this for my work flows. I was just thinking about this while putting together a new work flow since there doesn't seem to be an easy way to do this for a time series aside from using a macro. I thought the output from a timeseries may be somewhat confusing for someone who is not so familiar with connected component analysis.

Maybe it could at least be included in the documentation that the image dimensionality is used for determining the connectivity. Otherwise, as you mention, an option to use 2D connectivity for 3D arrays would be a nice solution. Or, if the stack is a time series, it could use 2D connectivity, while if it is a z-series it could use 3D. I think that might make sense in terms of what people want out of this function considering their input data. Although, as one sometimes loses track of whether their slices are considered time or z-slices, it could also be confusing. I guess there is probably not an ideal solution for all users. Anyway, thanks! I use this package a lot!

iarganda commented 3 years ago

I actually wrote some scripts to do that and I also wonder if it would be worth it for some plugins to include the "Apply to stack" option in 2D as in regular 2D filters in ImageJ...

dlegland commented 3 years ago

Hmm, yes, for 2D processing, we could envision adding such an option! We can also add the option only in the case of a 3D input image.

agclark12 commented 3 years ago

Having such a standard "Apply to Stack" option would also make the scripting much easier because you can just iterate through the stack and run it on each slice instead of having to pull out slices one by one and make a new output stack. For anyone who stumbles on this thread and needs a way to do this in the macro, here is a small example to make this work for a stack where you only want 2D connectivity for each slice (e.g. time series):

//makes a new image with same dimensions as the original
rename("stk");
getDimensions(width, height, channels, slices, frames);
newImage("new", "8-bit", width, height, slices);

//cleans up segmentation for each frame
for (i=1;i<slices+1;i++) {

    selectWindow("stk");
    setSlice(i);
    run("Duplicate...", " ");
    rename("curr");
    run("Keep Largest Region");
    run("Fill Holes", "");
    run("Select All");
    run("Copy");
    selectWindow("new");
    setSlice(i);
    run("Paste");
    run("Select None");
    close("curr");
    close("curr-largest");

}