39
39
import java .util .Arrays ;
40
40
import java .util .Collections ;
41
41
import java .util .Iterator ;
42
- import java .util .List ;
43
42
import java .util .Set ;
44
- import java .util .concurrent .ExecutionException ;
45
43
import java .util .concurrent .ExecutorService ;
46
- import java .util .concurrent .Executors ;
47
- import java .util .concurrent .Future ;
48
44
49
45
import net .imglib2 .Cursor ;
50
46
import net .imglib2 .FinalInterval ;
51
47
import net .imglib2 .RandomAccess ;
52
48
import net .imglib2 .RandomAccessible ;
53
49
import net .imglib2 .RandomAccessibleInterval ;
54
50
import net .imglib2 .iterator .IntervalIterator ;
51
+ import net .imglib2 .parallel .Parallelization ;
55
52
import net .imglib2 .roi .labeling .ImgLabeling ;
56
53
import net .imglib2 .type .numeric .IntegerType ;
57
54
import net .imglib2 .view .Views ;
@@ -99,17 +96,19 @@ public CollectNeighborLabelsFactory getFactory()
99
96
* @param se
100
97
* structuring element to use. 8-connected or 4-connected
101
98
* (respectively n-dimensional analog)
99
+ * @param service
100
+ * service providing threads for multi-threading
102
101
*/
103
102
public static < T extends IntegerType < T >, L , I extends IntegerType < I > > void labelAllConnectedComponents (
104
103
final RandomAccessible < T > input ,
105
104
final ImgLabeling < L , I > labeling ,
106
105
final Iterator < L > labelGenerator ,
107
- final StructuringElement se )
106
+ final StructuringElement se ,
107
+ final ExecutorService service )
108
108
{
109
- final int numThreads = Runtime .getRuntime ().availableProcessors ();
110
- final ExecutorService service = Executors .newFixedThreadPool ( numThreads );
111
- labelAllConnectedComponents ( input , labeling , labelGenerator , se , service );
112
- service .shutdown ();
109
+ Parallelization .runWithExecutor ( service ,
110
+ () -> labelAllConnectedComponents ( input , labeling , labelGenerator , se )
111
+ );
113
112
}
114
113
115
114
/**
@@ -130,21 +129,18 @@ public static < T extends IntegerType< T >, L, I extends IntegerType< I > > void
130
129
* @param se
131
130
* structuring element to use. 8-connected or 4-connected
132
131
* (respectively n-dimensional analog)
133
- * @param service
134
- * service providing threads for multi-threading
135
132
*/
136
133
public static < T extends IntegerType < T >, L , I extends IntegerType < I > > void labelAllConnectedComponents (
137
134
final RandomAccessible < T > input ,
138
135
final ImgLabeling < L , I > labeling ,
139
136
final Iterator < L > labelGenerator ,
140
- final StructuringElement se ,
141
- final ExecutorService service )
137
+ final StructuringElement se )
142
138
{
143
139
final RandomAccessibleInterval < I > output = labeling .getIndexImg ();
144
140
for ( final I i : Views .iterable ( output ) )
145
141
i .setZero ();
146
142
147
- final int numLabels = labelAllConnectedComponents ( input , output , se , service ) + 1 ;
143
+ final int numLabels = labelAllConnectedComponents ( input , output , se ) + 1 ;
148
144
149
145
final ArrayList < Set < L > > labelSets = new ArrayList <>();
150
146
labelSets .add ( Collections .emptySet () );
@@ -161,7 +157,6 @@ public static < T extends IntegerType< T >, L, I extends IntegerType< I > > void
161
157
*
162
158
* <p>
163
159
* <em>Note, that the {@code output} image must be cleared to 0!</em>
164
- * </p>
165
160
*
166
161
* @param input
167
162
* input image with pixels > 0 belonging to foreground.
@@ -170,19 +165,20 @@ public static < T extends IntegerType< T >, L, I extends IntegerType< I > > void
170
165
* @param se
171
166
* structuring element to use. 8-connected or 4-connected
172
167
* (respectively n-dimensional analog)
168
+ * @param service
169
+ * service providing threads for multi-threading
173
170
* @return the number of connected components (that is, the highest value
174
171
* occurring in the output image.
175
172
*/
176
173
public static < T extends IntegerType < T >, L extends IntegerType < L > > int labelAllConnectedComponents (
177
174
final RandomAccessible < T > input ,
178
175
final RandomAccessibleInterval < L > output ,
179
- final StructuringElement se )
176
+ final StructuringElement se ,
177
+ final ExecutorService service )
180
178
{
181
- final int numThreads = Runtime .getRuntime ().availableProcessors ();
182
- final ExecutorService service = Executors .newFixedThreadPool ( numThreads );
183
- final int result = labelAllConnectedComponents ( input , output , se , service );
184
- service .shutdown ();
185
- return result ;
179
+ return Parallelization .runWithExecutor ( service ,
180
+ () -> labelAllConnectedComponents ( input , output , se )
181
+ );
186
182
}
187
183
188
184
/**
@@ -192,6 +188,7 @@ public static < T extends IntegerType< T >, L extends IntegerType< L > > int lab
192
188
*
193
189
* <p>
194
190
* <em>Note, that the {@code output} image must be cleared to 0!</em>
191
+ * </p>
195
192
*
196
193
* @param input
197
194
* input image with pixels > 0 belonging to foreground.
@@ -200,16 +197,13 @@ public static < T extends IntegerType< T >, L extends IntegerType< L > > int lab
200
197
* @param se
201
198
* structuring element to use. 8-connected or 4-connected
202
199
* (respectively n-dimensional analog)
203
- * @param service
204
- * service providing threads for multi-threading
205
200
* @return the number of connected components (that is, the highest value
206
201
* occurring in the output image.
207
202
*/
208
203
public static < T extends IntegerType < T >, L extends IntegerType < L > > int labelAllConnectedComponents (
209
204
final RandomAccessible < T > input ,
210
205
final RandomAccessibleInterval < L > output ,
211
- final StructuringElement se ,
212
- final ExecutorService service )
206
+ final StructuringElement se )
213
207
{
214
208
final int n = output .numDimensions ();
215
209
final int splitDim = n - 1 ;
@@ -234,37 +228,14 @@ public static < T extends IntegerType< T >, L extends IntegerType< L > > int lab
234
228
min [ splitDim ] += taskSize ;
235
229
}
236
230
237
- final ArrayList < Future < ? > > futures = new ArrayList < Future < ? > >();
238
- for ( final Fragment < T , L > fragment : fragments )
239
- {
240
- futures .add ( service .submit ( new Runnable ()
241
- {
242
- @ Override
243
- public void run ()
244
- {
245
- fragment .mark ();
246
- }
247
- } ) );
248
- }
249
- getAllFutures ( futures );
231
+ Parallelization .getTaskExecutor ().forEach ( Arrays .asList ( fragments ), Fragment ::mark );
250
232
251
233
final TIntArrayList merged = mergeCanonicalLists ( fragments );
252
234
for ( int i = 1 ; i < numTasks ; ++i )
253
235
fragments [ i ].linkToPreviousFragment ( fragments [ i - 1 ], merged );
254
236
final int numComponents = splitCanonicalLists ( fragments , merged );
255
237
256
- for ( final Fragment < T , L > fragment : fragments )
257
- {
258
- futures .add ( service .submit ( new Runnable ()
259
- {
260
- @ Override
261
- public void run ()
262
- {
263
- fragment .relabel ();
264
- }
265
- } ) );
266
- }
267
- getAllFutures ( futures );
238
+ Parallelization .getTaskExecutor ().forEach ( Arrays .asList ( fragments ), Fragment ::relabel );
268
239
269
240
return numComponents ;
270
241
}
@@ -481,26 +452,6 @@ private static < T extends IntegerType< T >, L extends IntegerType< L > > int sp
481
452
return nextLabel - 1 ;
482
453
}
483
454
484
- private static void getAllFutures ( final List < Future < ? > > futures )
485
- {
486
- for ( final Future < ? > future : futures )
487
- {
488
- try
489
- {
490
- future .get ();
491
- }
492
- catch ( final InterruptedException e )
493
- {
494
- e .printStackTrace ();
495
- }
496
- catch ( final ExecutionException e )
497
- {
498
- e .printStackTrace ();
499
- }
500
- }
501
- futures .clear ();
502
- }
503
-
504
455
private static interface CollectNeighborLabels < L extends IntegerType < L > >
505
456
{
506
457
public void collect ( RandomAccess < L > la , final TIntArrayList neighborLabels , final long [] labelsMin , final long [] labelsMax );
0 commit comments