ijpb / MorphoLibJ

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

Dilate and shrink labels #41

Closed tischi closed 1 year ago

tischi commented 3 years ago

@dlegland @iarganda

I am 100% sure I asked this before but I cannot find it.

  1. Can one dilate or shrink labels in a label mask image by a defined amount of pixels using MLJ?
  2. What is the point of the Expand Labels command? I am getting a larger image with the labels more spread out. Looks interesting but what is the use case?
dlegland commented 3 years ago

Hi!

  1. there is no specific method or plugin. You can use morphological dilation / erosion, or more directly morphological closing, but you can get problems if you have adjacent labels (dilation will keep the label with the largest value, which is not always appropriate). A better way would be to iterate on labels, create the binary version, compute morphological closing, and then combine the results. It may still be possible to mix labels in some cases, I guess...
  2. The idea was to facilitate the visualisation of 3D segmentations with the 3D viewer. By expanding the labels, you can see not only the labels close to the boundary of the organ, but also some labels inside. Well, 3D vizualization of 3D segmentation can still be improved!

best, David

tischi commented 3 years ago

OK, then I will use this solution: https://forum.image.sc/t/clij-dilate-binary/35231/24 I think it would be nice though to have this functionality also directly within MorpholibJ. Shall I leave the issue thus open?

dlegland commented 3 years ago

Hi, well, I do not totally get the difference between the solution you mention and a "simple" dilation, or max-filter... Does it manages the case where two labels overlap after dilation? In that case, it is not planned to implement it within MorphoLibJ. so I think you can close.

tischi commented 3 years ago

Does it manages the case where two labels overlap after dilation?

Yes, because it only dilates into regions where the pixels were zero to start with.

dlegland commented 3 years ago

ok, got it! so maybe that is more feasible than what I was thinking to! I'll add on the TODO list, but I won't have time before a while...

VolkerH commented 3 years ago

Just saw this issue here. Was hoping that expand labels would do exactly what @tischi wanted.

It would be nice to have something like that in Fiji, and I think it would fit into MorpholibJ. See also the related discussion here: https://forum.image.sc/t/equivalent-to-cellprofilers-identifysecondaryobjects-distance-n-in-fiji/39146/4

tischi commented 3 years ago

Pressure is rising ;-)

VolkerH commented 3 years ago

For me, this is mainly of interest if users want a workflow in Fiji.

Note that the Python implementation from Cellprofiler uses a particular implementation of the distance transform that keeps track of the coordinates for the pixels with the closest distance.

dlegland commented 3 years ago

I think the feature would be useful to many, so it is of interest. Thanks for reporting the CellProfiler implementation, I will surely need to have a look. Keeping coordinates of closest pixels looks good also.

tischi commented 3 years ago

Yeah! Thanks for looking into this!!!

VolkerH commented 3 years ago

Yeah! Thanks for looking into this!!!

+1 some more reading (but this is based on the CellProfiler implementation, with the additional improvement that it works in nD, not just 2D): https://github.com/scikit-image/scikit-image/blob/master/skimage/segmentation/_expand_labels.py

tischi commented 3 years ago

@dlegland Just another poke from my side as we would love to have this feature soon-ish for running a course 😄

dlegland commented 3 years ago

Hi @tischi and @VolkerH ! I just made a pre-release that contains a "Dilate Labels" plugins. Tested on 2D and 3D images, hopefully this corresponds to what was discussed!

Still compatible with java 1.6, by the way (I use it for old version of 3D viewer).

Best, David

tischi commented 3 years ago

@dlegland Tested with 2D images and it is beautiful:

image

Could you please cut a release to and ship it with the update site? I need it for a course soon! Thanks ❤️

dlegland commented 3 years ago

ok, try to do it this week...

VolkerH commented 3 years ago

Missed the Github notification in March but saw this now. Thank you for implementing this missing feature. I had asked about this a whille ago on image.sc. I just updated the thread there to point to your solution here https://forum.image.sc/t/equivalent-to-cellprofilers-identifysecondaryobjects-distance-n-in-fiji/39146/21?u=volkerh

dlegland commented 3 years ago

Released!!!! I updated IJPB site as well. @VolkerH thanks for the link! It's hard to follow all the threads...

tischi commented 2 years ago

@dlegland Thanks again! I was hoping that putting a negative number into the Dilate Labels would shrink (erode) the labels but it does not do that. Could that be implemented? Or is there already some other way of shrinking them?

dlegland commented 2 years ago

Hi @tischi ! No, the radius is expected to be positive. I think that the coding of erosion for labels (taking care of neighbors) should not be that complicated. Maybe it is time for some refactoring and cleaning up of the label morphological processing part....

tischi commented 2 years ago

OK! Should we re-open this issue then?

dlegland commented 2 years ago

done! I'm on travel this week (in Heidelberg, BTW...), but hope to have a look soon!

lacan commented 2 years ago

Hello, I would also be tempted to have an "Erode Labels" command. A typical use case is to clean up the labels with an opening filter, so this would stop me from needing to go through ROIs.

Looking forward and thanks for MorphoLibJ

tischi commented 2 years ago

@lacan

For me: erode == shrink

For removing small labels based on their volume you can use Label Images > Label Size Filtering

dlegland commented 2 years ago

Hi,

I also agree that erode is similar to shrink.

For the opening, the filtering by size is an option, indeed! Otherwise, morphological opening rely on erosion, and for this I was not totally sure which behaviour should be expected for touching labels. I was first thinking on erosion from either the background or another label, but finally it is maybe better to erode ONLY from background pixels. Example:

0 0 0 0 0 0 0 0 0       0 0 0 0 0 0 0 0 0 
0 1 1 1 1 0 0 0 0       0 0 0 0 0 0 0 0 0
0 1 1 1 1 2 2 2 0       0 0 1 1 1 0 0 0 0
0 1 1 1 1 2 2 2 0  =>   0 0 1 1 1 2 2 0 0    
0 1 1 1 1 2 2 2 0       0 0 1 1 1 2 2 0 0    
0 1 1 1 1 2 2 2 0       0 0 1 1 1 0 0 0 0
0 1 1 1 1 0 0 0 0       0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0       0 0 0 0 0 0 0 0 0 

prefered over:

0 0 0 0 0 0 0 0 0       0 0 0 0 0 0 0 0 0 
0 1 1 1 1 0 0 0 0       0 0 0 0 0 0 0 0 0
0 1 1 1 1 2 2 2 0       0 0 1 1 0 0 0 0 0
0 1 1 1 1 2 2 2 0  =>   0 0 1 1 0 0 2 0 0    
0 1 1 1 1 2 2 2 0       0 0 1 1 0 0 2 0 0    
0 1 1 1 1 2 2 2 0       0 0 1 1 0 0 0 0 0
0 1 1 1 1 0 0 0 0       0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0       0 0 0 0 0 0 0 0 0 

That way we have a consistent behavior with dilation: pixels/voxels are modified if they are close to background. If they are close to another label, there are not modified. Then, result of opening/closing will change only at boundary with background.

ok for this behaviour?

dlegland commented 2 years ago

Hi @tischi and @lacan ; I have juste released the pre-version https://github.com/ijpb/MorphoLibJ/releases/tag/v1.5.1-LabelMorphologicalFilters : it contains a new plugin "Label Morphological Filter" that can apply dilation, erosion, closing and opening! can be 2D or 3D.

Need more refactoring than expected, but seems to work...

best, David