|
34 | 34 |
|
35 | 35 | package net.imglib2.algorithm.labeling;
|
36 | 36 |
|
| 37 | +import java.util.Arrays; |
37 | 38 | import java.util.function.LongFunction;
|
38 | 39 | import java.util.function.LongUnaryOperator;
|
39 | 40 | import java.util.function.ToLongBiFunction;
|
|
55 | 56 | import net.imglib2.type.numeric.IntegerType;
|
56 | 57 | import net.imglib2.util.IntervalIndexer;
|
57 | 58 | import net.imglib2.util.Intervals;
|
| 59 | +import net.imglib2.util.Util; |
| 60 | +import net.imglib2.view.ExtendedRandomAccessibleInterval; |
58 | 61 | import net.imglib2.view.Views;
|
| 62 | +import net.imglib2.view.composite.RealComposite; |
59 | 63 |
|
60 | 64 | /**
|
61 | 65 | *
|
@@ -133,7 +137,8 @@ public static < B extends BooleanType< B >, L extends IntegerType< L > > void co
|
133 | 137 | *
|
134 | 138 | * Implementation of connected component analysis that uses
|
135 | 139 | * {@link IntArrayRankedUnionFind} to find sets of pixels that are connected
|
136 |
| - * with respect to a neighborhood ({@code shape}) over a binary mask. {@code mask} |
| 140 | + * with respect to a neighborhood ({@code shape}) over a binary mask. |
| 141 | + * {@code mask} |
137 | 142 | * and {@code labeling} are expected to have equal min and max.
|
138 | 143 | *
|
139 | 144 | * @param mask
|
@@ -313,7 +318,139 @@ private static < B extends BooleanType< B >, L extends IntegerType< L > > void c
|
313 | 318 | }
|
314 | 319 | }
|
315 | 320 | }
|
| 321 | + } |
| 322 | + |
| 323 | + /** |
| 324 | + * Connected components on a regular arbitrary boolean affinity graph. |
| 325 | + * |
| 326 | + * @param <B> |
| 327 | + * boolean type used for affinity scalars |
| 328 | + * @param <C> |
| 329 | + * affinity vector |
| 330 | + * @param <L> |
| 331 | + * label output must fit number of components |
| 332 | + * @param affinities |
| 333 | + * affinity vector for each pixel |
| 334 | + * @param affinityOffsets |
| 335 | + * offset vector for each affinity index |
| 336 | + * @param labeling |
| 337 | + * output |
| 338 | + * @param uf |
| 339 | + * union find |
| 340 | + * @param id |
| 341 | + * id generator for pixels/ locations |
| 342 | + * @param idForSet |
| 343 | + * id generator for components |
| 344 | + */ |
| 345 | + public static < B extends BooleanType< B >, C extends RealComposite< B >, L extends IntegerType< L > > void connectedComponentsOnAffinities( |
| 346 | + final RandomAccessible< C > affinities, |
| 347 | + final long[][] affinityOffsets, |
| 348 | + final RandomAccessibleInterval< L > labeling, |
| 349 | + final UnionFind uf, |
| 350 | + final ToLongBiFunction< Localizable, L > id, |
| 351 | + final LongUnaryOperator idForSet ) |
| 352 | + { |
| 353 | + final ExtendedRandomAccessibleInterval< L, RandomAccessibleInterval< L > > extendedLabeling = Views.extendValue( labeling, Util.getTypeFromInterval( labeling ).createVariable() ); |
316 | 354 |
|
| 355 | + @SuppressWarnings( "unchecked" ) |
| 356 | + final Cursor< L >[] offsetLabelingCursors = new Cursor[ affinityOffsets.length ]; |
| 357 | + Arrays.setAll( offsetLabelingCursors, i -> Views.flatIterable( Views.interval( Views.offset( extendedLabeling, affinityOffsets[ i ] ), labeling ) ).cursor() ); |
| 358 | + |
| 359 | + final Cursor< L > target = Views.flatIterable( labeling ).cursor(); |
| 360 | + final Cursor< C > affinitiesCursor = Views.flatIterable( Views.interval( affinities, labeling ) ).cursor(); |
| 361 | + |
| 362 | + while ( target.hasNext() ) |
| 363 | + { |
| 364 | + final C affinitiesVector = affinitiesCursor.next(); |
| 365 | + final L targetLabel = target.next(); |
| 366 | + final long labelId = uf.findRoot( id.applyAsLong( target, targetLabel ) ); |
| 367 | + targetLabel.setInteger( labelId ); |
| 368 | + |
| 369 | + for ( int i = 0; i < affinityOffsets.length; ++i ) |
| 370 | + { |
| 371 | + offsetLabelingCursors[ i ].fwd(); |
| 372 | + if ( affinitiesVector.get( i ).get() ) |
| 373 | + { |
| 374 | + final long otherLabelId = uf.findRoot( id.applyAsLong( offsetLabelingCursors[ i ], offsetLabelingCursors[ i ].get() ) ); |
| 375 | + if ( labelId != otherLabelId ) |
| 376 | + uf.join( labelId, otherLabelId ); |
| 377 | + } |
| 378 | + } |
| 379 | + } |
| 380 | + uf.relabel( labeling, id, idForSet ); |
317 | 381 | }
|
318 | 382 |
|
| 383 | + /** |
| 384 | + * Connected components on a regular arbitrary boolean affinity graph. |
| 385 | + * Each component gets the id of the first pixel (in flat iteration order) |
| 386 | + * inside the component increased by firstIndex. |
| 387 | + * |
| 388 | + * @param <B> |
| 389 | + * boolean type used for affinity scalars |
| 390 | + * @param <C> |
| 391 | + * affinity vector |
| 392 | + * @param <L> |
| 393 | + * label output must fit number of components |
| 394 | + * @param affinities |
| 395 | + * affinity vector for each pixel |
| 396 | + * @param affinityOffsets |
| 397 | + * offset vector for each affinity index |
| 398 | + * @param labeling |
| 399 | + * output |
| 400 | + * @param firstIndex |
| 401 | + */ |
| 402 | + public static < B extends BooleanType< B >, C extends RealComposite< B >, L extends IntegerType< L > > void connectedComponentsOnAffinities( |
| 403 | + final RandomAccessible< C > affinities, |
| 404 | + final long[][] affinityOffsets, |
| 405 | + final RandomAccessibleInterval< L > labeling, |
| 406 | + final UnionFind uf, |
| 407 | + final long firstIndex ) |
| 408 | + { |
| 409 | + final ExtendedRandomAccessibleInterval< L, RandomAccessibleInterval< L > > extendedLabeling = |
| 410 | + Views.extendValue( labeling, Util.getTypeFromInterval( labeling ).createVariable() ); |
| 411 | + |
| 412 | + /* populate labeling with index */ |
| 413 | + long index = firstIndex; |
| 414 | + for ( final L label : Views.flatIterable( labeling ) ) |
| 415 | + { |
| 416 | + label.setInteger( index ); |
| 417 | + ++index; |
| 418 | + } |
| 419 | + |
| 420 | + @SuppressWarnings( "unchecked" ) |
| 421 | + final Cursor< L >[] offsetLabelingCursors = new Cursor[ affinityOffsets.length ]; |
| 422 | + Arrays.setAll( offsetLabelingCursors, i -> Views.flatIterable( |
| 423 | + Views.interval( |
| 424 | + Views.offset( |
| 425 | + extendedLabeling, |
| 426 | + affinityOffsets[ i ] ), |
| 427 | + labeling ) ) |
| 428 | + .cursor() ); |
| 429 | + |
| 430 | + final Cursor< L > target = Views.flatIterable( labeling ).cursor(); |
| 431 | + final Cursor< C > affinitiesCursor = Views.flatIterable( Views.interval( affinities, labeling ) ).cursor(); |
| 432 | + |
| 433 | + while ( target.hasNext() ) |
| 434 | + { |
| 435 | + final C affinitiesVector = affinitiesCursor.next(); |
| 436 | + final L targetLabel = target.next(); |
| 437 | + long labelId = uf.findRoot( targetLabel.getIntegerLong() ); |
| 438 | + |
| 439 | + for ( int i = 0; i < affinityOffsets.length; ++i ) |
| 440 | + { |
| 441 | + offsetLabelingCursors[ i ].fwd(); |
| 442 | + if ( affinitiesVector.get( i ).get() ) |
| 443 | + { |
| 444 | + final long otherLabelId = uf.findRoot( offsetLabelingCursors[ i ].get().getIntegerLong() ); |
| 445 | + if ( labelId != otherLabelId ) |
| 446 | + { |
| 447 | + uf.join( labelId, otherLabelId ); |
| 448 | + if ( i + 1 < affinityOffsets.length ) |
| 449 | + labelId = uf.findRoot( targetLabel.getIntegerLong() ); |
| 450 | + } |
| 451 | + } |
| 452 | + } |
| 453 | + } |
| 454 | + uf.relabel( labeling ); |
| 455 | + } |
319 | 456 | }
|
0 commit comments