Measure Surface/Perimeter in imglib2

imglib2

#1

@imagejan @haesleinhuepf

Is there a ready-to-use imglib2 function to measure the “HyperSurface” (2D=Perimeter, 3D=Surface, 4D=??) of a LabelRegion? Ideally, I would like to use it in a code snippet like this:

final LabelRegions< Integer > labelRegions = new LabelRegions<>( imgLabeling );
	for ( LabelRegion labelRegion : labelRegions )
	{
		// measure labelRegion Perimeter...
	}

BTW: I am not being puristic here but happy to use ops :slight_smile:


#2

There’s multiple ways measure the perimeter of a 2D region: for example, you can simply count the contour pixels (which requires a definition of the contour, specifying 4-connected or 8-connected elements), or – to have a more realistic estimate – somehow fit a polygon or spline between the raster positions of the pixel grid… and analogous in 3D and higher dimensions, of course, where you need to build a Mesh before being able to measure its surface.


There’s a few things available in Ops (maybe some of the actual algorithm implementations will migrate to imglib2-algorithm in the future):

  • geom.contour:

    Polygon2D out  <=  Contour(RandomAccessibleInterval in, boolean useJacobs)
    
  • morphology.outline:

    RandomAccessibleInterval out?  <=  Outline(RandomAccessibleInterval out?, RandomAccessibleInterval in1, Boolean in2)
    

and then, if you just want to get the size of the boundary as a single number:

Others might know more, or can chime in with some practical experience…


#3

Great! Thanks so much! And also a biiiig thanks to the people implementing all of this!!!

Of course, one short comment :wink:

final RandomAccessibleInterval outline = opService.morphology().outline( Views.zeroMin( mask ), true );
final Polygon2D contour = opService.geom().contour( mask, true );
final double boundarySize = opService.geom().boundarySize( contour ).getRealDouble();

The outline only works with Views.zeroMin() whereas the contour also works with RAIs with a non-zero offset.

In an ideal world opService.morphology().outline() would not crash for non-zero offsets but return an outline RAI that has the same offset as the input RAI. This becomes important when working with BigImageData where small RAIs can be crops of a bigger one and you want to keep track of their position in the bigger one.

…and is there also a function to generate a Mesh?


#4

Would you submit an issue for this, please? :slight_smile:

I forgot to mention the geom.convexHull op that returns a Mesh. See this issue where I posted some minimal usage example:


#5

done :slight_smile: