-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathBlkAffineFusion.java
More file actions
279 lines (251 loc) · 10.2 KB
/
Copy pathBlkAffineFusion.java
File metadata and controls
279 lines (251 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*-
* #%L
* Software for the reconstruction of multi-view microscopic acquisitions
* like Selective Plane Illumination Microscopy (SPIM) Data.
* %%
* Copyright (C) 2012 - 2024 Multiview Reconstruction developers.
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/
package net.preibisch.mvrecon.process.fusion.blk;
import static net.imglib2.algorithm.blocks.transform.Transform.Interpolation.NEARESTNEIGHBOR;
import static net.imglib2.algorithm.blocks.transform.Transform.Interpolation.NLINEAR;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import mpicbg.models.AffineModel1D;
import mpicbg.spim.data.generic.sequence.BasicImgLoader;
import mpicbg.spim.data.generic.sequence.BasicViewDescription;
import mpicbg.spim.data.generic.sequence.BasicViewSetup;
import mpicbg.spim.data.sequence.ViewId;
import net.imglib2.Dimensions;
import net.imglib2.Interval;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.algorithm.blocks.BlockAlgoUtils;
import net.imglib2.algorithm.blocks.BlockSupplier;
import net.imglib2.algorithm.blocks.ClampType;
import net.imglib2.algorithm.blocks.convert.Convert;
import net.imglib2.algorithm.blocks.transform.Transform;
import net.imglib2.algorithm.blocks.transform.Transform.Interpolation;
import net.imglib2.converter.Converter;
import net.imglib2.converter.RealUnsignedByteConverter;
import net.imglib2.converter.RealUnsignedShortConverter;
import net.imglib2.realtransform.AffineTransform3D;
import net.imglib2.type.NativeType;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.integer.UnsignedByteType;
import net.imglib2.type.numeric.real.FloatType;
import net.imglib2.util.Cast;
import net.imglib2.util.Util;
import net.imglib2.view.Views;
import net.preibisch.legacy.io.IOFunctions;
import net.preibisch.mvrecon.fiji.plugin.fusion.FusionGUI.FusionType;
import net.preibisch.mvrecon.process.downsampling.DownsampleTools;
import net.preibisch.mvrecon.process.fusion.FusionTools;
import net.preibisch.mvrecon.process.fusion.lazy.LazyAffineFusion;
import net.preibisch.mvrecon.process.fusion.lazy.LazyFusionTools;
import net.preibisch.mvrecon.process.interestpointregistration.pairwise.constellation.grouping.Group;
public class BlkAffineFusion
{
public static < T extends RealType< T > & NativeType< T > > RandomAccessibleInterval< T > init(
final Converter< FloatType, T > converter,
final BasicImgLoader imgloader,
final Collection< ? extends ViewId > viewIds,
final Map< ViewId, ? extends AffineTransform3D > viewRegistrations,
final Map< ViewId, ? extends BasicViewDescription< ? > > viewDescriptions,
final FusionType fusionType,
final int interpolationMethod,
final float blendingRange,
final Map< ViewId, AffineModel1D > intensityAdjustments,
final Interval fusionInterval,
final T type,
final int[] blockSize )
{
// go through the views and check if they are all 2-dimensional
final boolean is2d = viewIds.stream()
.map( viewDescriptions::get )
.map( BasicViewDescription::getViewSetup )
.filter( BasicViewSetup::hasSize )
.allMatch( vs -> vs.getSize().dimension( 2 ) == 1 );
if ( !supports( is2d, fusionType, intensityAdjustments ) )
{
IOFunctions.println( "BlkAffineFusion: Fusion method not supported (yet). Falling back to LazyAffineFusion." );
return LazyAffineFusion.init( converter, imgloader, viewIds, viewRegistrations, viewDescriptions, fusionType, interpolationMethod, blendingRange, intensityAdjustments, fusionInterval, type, blockSize );
}
final HashMap< ViewId, Dimensions > viewDimensions = LazyFusionTools.assembleDimensions( viewIds, viewDescriptions );
final Interpolation interpolation = ( interpolationMethod == 1 ) ? NLINEAR : NEARESTNEIGHBOR;
// to be able to use the "lowest ViewId" wins strategy
final List< ? extends ViewId > sortedViewIds = new ArrayList<>( viewIds );
Collections.sort( sortedViewIds );
// Which views to process (use un-altered bounding box and registrations).
// Final filtering happens per Cell.
// Here we just pre-filter everything outside the fusionInterval.
final Overlap overlap = new Overlap(
sortedViewIds,
viewRegistrations,
viewDimensions,
LazyFusionTools.defaultAffineExpansion,
is2d ? 2 : 3 )
.filter( fusionInterval )
.offset( fusionInterval.minAsLongArray() );
final List< BlockSupplier< FloatType > > images = new ArrayList<>( overlap.numViews() );
final List< BlockSupplier< FloatType > > weights = new ArrayList<>( overlap.numViews() );
final List< BlockSupplier< UnsignedByteType > > masks = new ArrayList<>( overlap.numViews() );
for ( final ViewId viewId : overlap.getViewIds() )
{
final AffineTransform3D model = viewRegistrations.get( viewId ).copy();
// this modifies the model so it maps from a smaller image to the global coordinate space,
// which applies for the image itself as well as the weights since they also use the smaller
// input image as reference
final double[] usedDownsampleFactors = new double[ 3 ];
RandomAccessibleInterval inputImg = DownsampleTools.openDownsampled( imgloader, viewId, model, usedDownsampleFactors );
final AffineTransform3D transform = concatenateBoundingBoxOffset( model, fusionInterval );
final BlockSupplier< FloatType > viewBlocks = transformedBlocks(
Cast.unchecked( inputImg ),
transform, interpolation );
images.add( viewBlocks );
// instantiate blending if necessary
final float[] blending = Util.getArrayFromValue( FusionTools.defaultBlendingRange, 3 );
final float[] border = Util.getArrayFromValue( FusionTools.defaultBlendingBorder, 3 );
// adjust both for z-scaling (anisotropy), downsampling, and registrations itself
FusionTools.adjustBlending( viewDimensions.get( viewId ), Group.pvid( viewId ), blending, border, model );
switch ( fusionType )
{
case AVG:
weights.add( Masking.create( inputImg, border, transform ).andThen( Convert.convert( new FloatType() ) ) );
break;
case AVG_BLEND:
weights.add( Blending.create( inputImg, border, blending, transform ) );
break;
case MAX:
case FIRST:
masks.add( Masking.create( inputImg, border, transform ) );
break;
case AVG_CONTENT:
case AVG_BLEND_CONTENT:
default:
// should never happen
throw new IllegalStateException();
}
}
final BlockSupplier< FloatType > floatBlocks;
switch ( fusionType )
{
case AVG:
case AVG_BLEND:
floatBlocks = WeightedAverage.of( images, weights, overlap );
break;
case MAX:
floatBlocks = MaxIntensity.of( images, masks, overlap );
break;
case FIRST:
floatBlocks = FirstWins.of( images, masks, overlap );
break;
case AVG_CONTENT:
case AVG_BLEND_CONTENT:
default:
// should never happen
throw new IllegalStateException();
}
final BlockSupplier< T > blocks = convertToOutputType(
floatBlocks,
converter, type )
.tile( 32 );
return BlockAlgoUtils.cellImg( blocks, fusionInterval.dimensionsAsLongArray(), blockSize );
}
private static < T extends NativeType< T > > BlockSupplier< T > convertToOutputType(
final BlockSupplier< FloatType > floatBlocks,
final Converter< FloatType, T > converter,
final T type )
{
if ( converter == null )
{
return floatBlocks.andThen( Convert.convert( type, ClampType.CLAMP ) );
}
else if ( converter instanceof RealUnsignedByteConverter )
{
final RealUnsignedByteConverter< ? > c = Cast.unchecked( converter );
final double min = c.getMin();
final double max = c.getMax();
final float scale = ( float ) ( 255.0 / ( max - min ) );
final float offset = ( float ) ( -min / ( max - min ) );
return floatBlocks
.andThen( LinearRange.linearRange( scale, offset ) )
.andThen( Convert.convert( type, ClampType.CLAMP ) );
}
else if ( converter instanceof RealUnsignedShortConverter )
{
final RealUnsignedShortConverter< ? > c = Cast.unchecked( converter );
final double min = c.getMin();
final double max = c.getMax();
final float scale = ( float ) ( 65535.0 / ( max - min ) );
final float offset = ( float ) ( -min / ( max - min ) );
return floatBlocks
.andThen( LinearRange.linearRange( scale, offset ) )
.andThen( Convert.convert( type, ClampType.CLAMP ) );
}
else
{
return floatBlocks.andThen( Convert.convert( type, () -> converter ) );
}
}
private static < T extends NativeType< T > > BlockSupplier< FloatType > transformedBlocks(
final RandomAccessibleInterval< T > inputImg,
final AffineTransform3D transform,
final Interpolation interpolation )
{
return BlockSupplier.of( Views.extendBorder( ( inputImg ) ) )
.andThen( Convert.convert( new FloatType() ) )
.andThen( Transform.affine( transform, interpolation ) );
}
private static AffineTransform3D concatenateBoundingBoxOffset(
final AffineTransform3D transformFromSource,
final Interval boundingBoxInTarget )
{
final AffineTransform3D t = new AffineTransform3D();
t.setTranslation(
-boundingBoxInTarget.min( 0 ),
-boundingBoxInTarget.min( 1 ),
-boundingBoxInTarget.min( 2 ) );
t.concatenate( transformFromSource );
return t;
}
private static < T extends RealType< T > & NativeType< T > > boolean supports(
final boolean is2d,
final FusionType fusionType,
final Map< ViewId, AffineModel1D > intensityAdjustments )
{
if ( is2d )
return false; // TODO
switch ( fusionType )
{
case AVG_BLEND:
case FIRST:
case MAX:
case AVG:
break;
case AVG_CONTENT:
case AVG_BLEND_CONTENT:
return false; // TODO
}
if ( intensityAdjustments != null )
return false; // TODO
return true;
}
}