Skip to content

Commit 1d114ca

Browse files
awalter17ctrueden
authored andcommitted
Add method to add masks to labelings with default label and test
1 parent d7dfd6a commit 1d114ca

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

src/main/java/net/imglib2/roi/Masks.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,44 @@ public static < T, I extends IntegerType< I > > void addMasksToLabeling( final L
442442
}
443443
}
444444

445+
/**
446+
* Adds any {@link LabeledMaskInterval}s in the list to the provided
447+
* {@link ImgLabeling}. If a given {@link MaskInterval} is a
448+
* {@link CompositeMaskPredicate}, this method will recurse through the
449+
* children looking for {@link LabeledMaskInterval}s. And if one is found,
450+
* its label will be added to the {@code ImgLabeling} only at the locations
451+
* in the root/parent {@code CompositeMaskPredicate}. If a given
452+
* {@link MaskInterval} or part of a {@code CompositeMaskPredicate} does not
453+
* contain a label, the given default label will be set at those locations.
454+
*
455+
* @param masks
456+
* list of potential {@link MaskInterval}s to add
457+
* @param labeling
458+
* {@link ImgLabeling} to add labels/masks to
459+
* @param defaultLabel
460+
* the label to assign to pixels inside the given
461+
* {@link MaskInterval}s that do not already have labels (i.e.
462+
* not part of a {@link LabeledMaskInterval}
463+
*/
464+
@SuppressWarnings( "unchecked" )
465+
public static < T, I extends IntegerType< I > > void addMasksToLabeling( final List< MaskInterval > masks, final ImgLabeling< T, I > labeling, final T defaultLabel )
466+
{
467+
addMasksToLabeling( masks, labeling, ( Class< T > ) defaultLabel.getClass() );
468+
469+
final RandomAccess< LabelingType< T > > ra = labeling.randomAccess();
470+
for ( final MaskInterval mi : masks )
471+
{
472+
final Cursor< Void > c = Regions.iterable( Masks.toRandomAccessibleInterval( mi ) ).cursor();
473+
while ( c.hasNext() )
474+
{
475+
c.next();
476+
ra.setPosition( c );
477+
if ( ra.get().isEmpty() )
478+
ra.get().add( defaultLabel );
479+
}
480+
}
481+
}
482+
445483
/*
446484
* Empty Masks
447485
* ===============================================================

src/test/java/net/imglib2/roi/mask/integer/MaskToLabelingTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,60 @@ public void testDifferentDimensions()
335335
assertTrue( c.next().isEmpty() );
336336
}
337337

338+
@Test
339+
public void testDefaultLabelEntireMask()
340+
{
341+
final MaskInterval m = createBox( new long[] { 10, 10 }, new long[] { 20, 20 } );
342+
final Img< IntType > indexImg = ArrayImgs.ints( 21, 21 );
343+
final ImgLabeling< String, IntType > imgLabeling = new ImgLabeling<>( indexImg );
344+
final String defaultLabel = "default";
345+
Masks.addMasksToLabeling( Collections.singletonList( m ), imgLabeling, defaultLabel );
346+
347+
final Cursor< LabelingType< String > > c = imgLabeling.cursor();
348+
while ( c.hasNext() )
349+
{
350+
final LabelingType< String > labels = c.next();
351+
if ( m.test( c ) )
352+
{
353+
assertTrue( labels.contains( defaultLabel ) );
354+
assertEquals( 1, labels.size() );
355+
}
356+
else
357+
assertTrue( labels.isEmpty() );
358+
}
359+
}
360+
361+
@Test
362+
public void testDefaultLabelPartialMask()
363+
{
364+
final LabeledMaskInterval< String > labeledSphere = new LabeledMaskInterval<>( createSphere( new long[] { 10, 10 }, 5 ), "label" );
365+
final MaskInterval sphere = createSphere( new long[] { 15, 13 }, 4 );
366+
final MaskInterval or = labeledSphere.or( sphere );
367+
final MaskInterval minus = sphere.minus( labeledSphere );
368+
final String defaultLabel = "default";
369+
final Img< IntType > indexImg = ArrayImgs.ints( 20, 20 );
370+
final ImgLabeling< String, IntType > imgLabeling = new ImgLabeling<>( indexImg );
371+
Masks.addMasksToLabeling( Collections.singletonList( or ), imgLabeling, defaultLabel );
372+
373+
final Cursor< LabelingType< String > > c = imgLabeling.cursor();
374+
while ( c.hasNext() )
375+
{
376+
final LabelingType< String > labels = c.next();
377+
if ( labeledSphere.test( c ) )
378+
{
379+
assertTrue( labels.contains( labeledSphere.getLabel() ) );
380+
assertEquals( 1, labels.size() );
381+
}
382+
else if ( minus.test( c ) )
383+
{
384+
assertTrue( labels.contains( defaultLabel ) );
385+
assertEquals( 1, labels.size() );
386+
}
387+
else
388+
assertTrue( labels.isEmpty() );
389+
}
390+
}
391+
338392
// -- Helper methods --
339393

340394
private static MaskInterval createBox( final long[] min, final long[] max )

0 commit comments

Comments
 (0)