Skip to content

Commit f2c1327

Browse files
committed
Opify labeling package
1 parent 51cae70 commit f2c1327

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

src/main/java/net/imglib2/algorithm/labeling/ConnectedComponentAnalysis.java

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public static < T > ToLongBiFunction< Localizable, T > idFromIntervalIndexer( fi
117117
* generalization for higher dimenions over a binary mask. {@code mask} and
118118
* {@code labeling} are expected to have equal min and max.
119119
*
120+
* @implNote op name='connectedComponents', type='org.scijava.function.Computers.Arity1'
120121
* @param mask
121122
* Boolean mask to distinguish foreground ({@code true}) from
122123
* background ({@code false}).
@@ -170,6 +171,44 @@ public static < B extends BooleanType< B >, L extends IntegerType< L > > void co
170171
new StartAtOneIdForNextSet() );
171172
}
172173

174+
/**
175+
*
176+
* Implementation of connected component analysis that uses
177+
* {@link IntArrayRankedUnionFind} to find sets of pixels that are connected
178+
* with respect to a neighborhood ({@code shape}) over a binary mask.
179+
* {@code mask}
180+
* and {@code labeling} are expected to have equal min and max.
181+
* <p>
182+
* This method differs from
183+
* {@link #connectedComponents(RandomAccessibleInterval, RandomAccessibleInterval, Shape)}
184+
* only in that its parameter order is tailored to an Op. The output comes
185+
* last, and the primary input (the input image) comes first.
186+
* </p>
187+
*
188+
* @implNote op name='connectedComponents', type='org.scijava.function.Computers.Arity2'
189+
* @param mask
190+
* Boolean mask to distinguish foreground ({@code true}) from
191+
* background ({@code false}).
192+
* @param shape
193+
* Connectivity of connected components, e.g. 4-neighborhood
194+
* ({@link DiamondShape}), 8-neighborhood
195+
* ({@link RectangleNeighborhood}) and their generalisations for
196+
* higher dimensions.
197+
* @param labeling
198+
* Output parameter to store labeling: background pixels are
199+
* labeled zero, foreground pixels are greater than zero: 1, 2,
200+
* ..., N. Note that initially all pixels are expected to be zero
201+
* as background values will not be written.
202+
*/
203+
public static < B extends BooleanType< B >, L extends IntegerType< L > > void connectedComponents(
204+
final RandomAccessibleInterval< B > mask,
205+
final Shape shape,
206+
final RandomAccessibleInterval< L > labeling)
207+
{
208+
connectedComponents(mask, labeling, shape);
209+
}
210+
211+
173212
/**
174213
*
175214
* Implementation of connected component analysis that uses
@@ -216,6 +255,55 @@ public static < B extends BooleanType< B >, L extends IntegerType< L > > void co
216255
UnionFind.relabel( mask, labeling, uf, idForPixel, idForSet );
217256
}
218257

258+
/**
259+
*
260+
* Implementation of connected component analysis that uses
261+
* {@link UnionFind} to find sets of pixels that are connected with respect
262+
* to a neighborhood ({@code shape}) over a binary mask. {@code mask} and
263+
* {@code labeling} are expected to have equal min and max.
264+
* <p>
265+
* This method differs from
266+
* {@link #connectedComponents(RandomAccessibleInterval, RandomAccessibleInterval, Shape, LongFunction, ToLongBiFunction, LongUnaryOperator)}
267+
* only in that its parameter order is tailored to an Op. The output comes
268+
* last, and the primary input (the input image) comes first.
269+
* </p>
270+
*
271+
* @implNote op name='connectedComponents', type='org.scijava.function.Computers.Arity5'
272+
* @param mask
273+
* Boolean mask to distinguish foreground ({@code true}) from
274+
* background ({@code false}).
275+
* @param shape
276+
* Connectivity of connected components, e.g. 4-neighborhood
277+
* ({@link DiamondShape}), 8-neighborhood
278+
* ({@link RectangleNeighborhood}) and their generalisations for
279+
* higher dimensions.
280+
* @param unionFindFactory
281+
* Creates appropriate {@link UnionFind} data structure for size
282+
* of {@code labeling}, e.g. {@link IntArrayRankedUnionFind} of
283+
* appropriate size.
284+
* @param idForPixel
285+
* Create id from pixel location and value. Multiple calls with
286+
* the same argument should always return the same result.
287+
* @param idForSet
288+
* Create id for a set from the root id of a set. Multiple calls
289+
* with the same argument should always return the same result.
290+
* @param labeling
291+
* Output parameter to store labeling: background pixels are
292+
* labeled zero, foreground pixels are greater than zero: 1, 2,
293+
* ..., N. Note that this is expected to be zero as background
294+
* values will not be written.
295+
*/
296+
public static < B extends BooleanType< B >, L extends IntegerType< L > > void connectedComponents(
297+
final RandomAccessibleInterval< B > mask,
298+
final Shape shape,
299+
final LongFunction< UnionFind > unionFindFactory,
300+
final ToLongBiFunction< Localizable, L > idForPixel,
301+
final LongUnaryOperator idForSet,
302+
final RandomAccessibleInterval< L > labeling)
303+
{
304+
connectedComponents( mask, labeling, shape, unionFindFactory, idForPixel, idForSet );
305+
}
306+
219307
private static < B extends BooleanType< B >, L extends IntegerType< L > > UnionFind makeUnion(
220308
final RandomAccessibleInterval< B > mask,
221309
final RandomAccessibleInterval< L > labeling,

src/main/java/net/imglib2/algorithm/labeling/ConnectedComponents.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,41 @@ public static < T extends IntegerType< T >, L, I extends IntegerType< I > > void
112112
service.shutdown();
113113
}
114114

115+
/**
116+
* Label all connected components in the given input image. In the output
117+
* image, all background pixels will be labeled to {} and foreground
118+
* components labeled as {1}, {2}, {3}, etc. where 1, 2, 3 are labels
119+
* returned by {@code labelGenerator.next()}. {@code labelGenerator.next()}
120+
* is called exactly <em>n</em> times if the input contains
121+
* <em>n</em> connected components.
122+
* <p>
123+
* This method differs from
124+
* {@link #labelAllConnectedComponents(RandomAccessible, ImgLabeling, Iterator, StructuringElement)}
125+
* only in that its parameter order is tailored to an Op. The output comes
126+
* last, and the primary input (the input image) comes first.
127+
* </p>
128+
*
129+
* @implNote op name='labelAllConnectedComponents', type='org.scijava.function.Computers.Arity3'
130+
* @param input
131+
* input image with pixels != 0 belonging to foreground.
132+
* @param labelGenerator
133+
* produces labels for the connected components.
134+
* @param se
135+
* structuring element to use. 8-connected or 4-connected
136+
* (respectively n-dimensional analog)
137+
* @param labeling
138+
* output labeling in which the connected components will be
139+
* labeled.
140+
*/
141+
public static < T extends IntegerType< T >, L, I extends IntegerType< I > > void labelAllConnectedComponents(
142+
final RandomAccessible< T > input,
143+
final Iterator< L > labelGenerator,
144+
final StructuringElement se,
145+
final ImgLabeling< L, I > labeling)
146+
{
147+
labelAllConnectedComponents( input, labeling, labelGenerator, se );
148+
}
149+
115150
/**
116151
* Label all connected components in the given input image. In the output
117152
* image, all background pixels will be labeled to {} and foreground
@@ -154,6 +189,44 @@ public static < T extends IntegerType< T >, L, I extends IntegerType< I > > void
154189
labeling.getMapping().setLabelSets( labelSets );
155190
}
156191

192+
/**
193+
* Label all connected components in the given input image. In the output
194+
* image, all background pixels will be labeled to {} and foreground
195+
* components labeled as {1}, {2}, {3}, etc. where 1, 2, 3 are labels
196+
* returned by {@code labelGenerator.next()}. {@code labelGenerator.next()}
197+
* is called exactly <em>n</em> times if the input contains
198+
* <em>n</em> connected components.
199+
* <p>
200+
* This method differs from
201+
* {@link #labelAllConnectedComponents(RandomAccessible, ImgLabeling, Iterator, StructuringElement, ExecutorService)}
202+
* only in that its parameter order is tailored to an Op. The output comes
203+
* last, and the primary input (the input image) comes first.
204+
* </p>
205+
*
206+
* @implNote op name='labelAllConnectedComponents', type='org.scijava.function.Computers.Arity4'
207+
* @param input
208+
* input image with pixels != 0 belonging to foreground.
209+
* @param labelGenerator
210+
* produces labels for the connected components.
211+
* @param se
212+
* structuring element to use. 8-connected or 4-connected
213+
* (respectively n-dimensional analog)
214+
* @param service
215+
* service providing threads for multi-threading
216+
* @param labeling
217+
* output labeling in which the connected components will be
218+
* labeled.
219+
*/
220+
public static < T extends IntegerType< T >, L, I extends IntegerType< I > > void labelAllConnectedComponents(
221+
final RandomAccessible< T > input,
222+
final Iterator< L > labelGenerator,
223+
final StructuringElement se,
224+
final ExecutorService service,
225+
final ImgLabeling< L, I > labeling)
226+
{
227+
labelAllConnectedComponents( input, labeling, labelGenerator, se, service );
228+
}
229+
157230
/**
158231
* "Label" all connected components in the given input image. In the output
159232
* image, all background pixels will be set to 0 and foreground components

0 commit comments

Comments
 (0)