Skip to content

Commit

Permalink
Add method to add masks to labelings with default label and test
Browse files Browse the repository at this point in the history
  • Loading branch information
awalter17 committed Jan 25, 2019
1 parent 33428c3 commit b637c1b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/main/java/net/imglib2/roi/Masks.java
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,44 @@ public static < T, I extends IntegerType< I > > void addMasksToLabeling( final L
}
}

/**
* Adds any {@link LabeledMaskInterval}s in the list to the provided
* {@link ImgLabeling}. If a given {@link MaskInterval} is a
* {@link CompositeMaskPredicate}, this method will recurse through the
* children looking for {@link LabeledMaskInterval}s. And if one is found,
* its label will be added to the {@code ImgLabeling} only at the locations
* in the root/parent {@code CompositeMaskPredicate}. If a given
* {@link MaskInterval} or part of a {@code CompositeMaskPredicate} does not
* contain a label, the given default label will be set at those locations.
*
* @param masks
* list of potential {@link MaskInterval}s to add
* @param labeling
* {@link ImgLabeling} to add labels/masks to
* @param defaultLabel
* the label to assign to pixels inside the given
* {@link MaskInterval}s that do not already have labels (i.e.
* not part of a {@link LabeledMaskInterval}
*/
@SuppressWarnings( "unchecked" )
public static < T, I extends IntegerType< I > > void addMasksToLabeling( final List< MaskInterval > masks, final ImgLabeling< T, I > labeling, final T defaultLabel )
{
addMasksToLabeling( masks, labeling, ( Class< T > ) defaultLabel.getClass() );

final RandomAccess< LabelingType< T > > ra = labeling.randomAccess();
for ( final MaskInterval mi : masks )
{
final Cursor< Void > c = Regions.iterable( Masks.toRandomAccessibleInterval( mi ) ).cursor();
while ( c.hasNext() )
{
c.next();
ra.setPosition( c );
if ( ra.get().isEmpty() )
ra.get().add( defaultLabel );
}
}
}

/*
* Empty Masks
* ===============================================================
Expand Down
54 changes: 54 additions & 0 deletions src/test/java/net/imglib2/roi/mask/integer/MaskToLabelingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,60 @@ public void testDifferentDimensions()
assertTrue( c.next().isEmpty() );
}

@Test
public void testDefaultLabelEntireMask()
{
final MaskInterval m = createBox( new long[] { 10, 10 }, new long[] { 20, 20 } );
final Img< IntType > indexImg = ArrayImgs.ints( 21, 21 );
final ImgLabeling< String, IntType > imgLabeling = new ImgLabeling<>( indexImg );
final String defaultLabel = "default";
Masks.addMasksToLabeling( Collections.singletonList( m ), imgLabeling, defaultLabel );

final Cursor< LabelingType< String > > c = imgLabeling.cursor();
while ( c.hasNext() )
{
final LabelingType< String > labels = c.next();
if ( m.test( c ) )
{
assertTrue( labels.contains( defaultLabel ) );
assertEquals( 1, labels.size() );
}
else
assertTrue( labels.isEmpty() );
}
}

@Test
public void testDefaultLabelPartialMask()
{
final LabeledMaskInterval< String > labeledSphere = new LabeledMaskInterval<>( createSphere( new long[] { 10, 10 }, 5 ), "label" );
final MaskInterval sphere = createSphere( new long[] { 15, 13 }, 4 );
final MaskInterval or = labeledSphere.or( sphere );
final MaskInterval minus = sphere.minus( labeledSphere );
final String defaultLabel = "default";
final Img< IntType > indexImg = ArrayImgs.ints( 20, 20 );
final ImgLabeling< String, IntType > imgLabeling = new ImgLabeling<>( indexImg );
Masks.addMasksToLabeling( Collections.singletonList( or ), imgLabeling, defaultLabel );

final Cursor< LabelingType< String > > c = imgLabeling.cursor();
while ( c.hasNext() )
{
final LabelingType< String > labels = c.next();
if ( labeledSphere.test( c ) )
{
assertTrue( labels.contains( labeledSphere.getLabel() ) );
assertEquals( 1, labels.size() );
}
else if ( minus.test( c ) )
{
assertTrue( labels.contains( defaultLabel ) );
assertEquals( 1, labels.size() );
}
else
assertTrue( labels.isEmpty() );
}
}

// -- Helper methods --

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

0 comments on commit b637c1b

Please sign in to comment.