2626 * POSSIBILITY OF SUCH DAMAGE.
2727 * #L%
2828 */
29- package org .mastodon .mamut .io . importer . labelimage ;
29+ package org .mastodon .mamut .util ;
3030
3131import bdv .viewer .Source ;
3232import bdv .viewer .SourceAndConverter ;
4848import org .mastodon .mamut .model .Model ;
4949import org .mastodon .mamut .model .ModelGraph ;
5050import org .mastodon .mamut .model .Spot ;
51- import org .mastodon .mamut .util .LineageTreeUtils ;
5251import org .mastodon .views .bdv .SharedBigDataViewerData ;
5352import org .scijava .app .StatusService ;
5453import org .slf4j .Logger ;
@@ -132,7 +131,7 @@ static void createSpotsFromLabelImage( final IntFunction< RandomAccessibleInterv
132131 * @param scaleFactor the scale factor to use for the ellipsoid. 1 means 2.2σ and is the default.
133132 * @return the number of spots created.
134133 */
135- private static int createSpotsForFrame ( final ModelGraph graph , final RandomAccessibleInterval < RealType < ? > > frame ,
134+ public static int createSpotsForFrame ( final ModelGraph graph , final RandomAccessibleInterval < ? extends RealType < ? > > frame ,
136135 final int frameId , final AffineTransform3D transform , final double scaleFactor )
137136 {
138137 logger .debug ( "Computing mean, covariance of all labels at frame {}" , frameId );
@@ -164,11 +163,11 @@ private static int createSpotsForFrame( final ModelGraph graph, final RandomAcce
164163 * @return A pair of values (min, max) that represent the minimum and maximum pixel values in the image
165164 * @author Noam Dori
166165 */
167- private static Pair < Integer , Integer > getPixelValueInterval ( final RandomAccessibleInterval < RealType < ? > > frame )
166+ private static Pair < Integer , Integer > getPixelValueInterval ( final RandomAccessibleInterval < ? extends RealType < ? > > frame )
168167 {
169168 int min = Integer .MAX_VALUE ;
170169 int max = Integer .MIN_VALUE ;
171- Cursor < RealType < ? > > cursor = Views . iterable ( frame ) .cursor ();
170+ Cursor < ? extends RealType < ? > > cursor = frame .cursor ();
172171 while ( cursor .hasNext () )
173172 {
174173 int val = ( int ) cursor .next ().getRealDouble ();
@@ -199,58 +198,69 @@ private static int createSpotsFromFrameLabels( final ModelGraph graph, final int
199198 final AffineTransform3D transform , final double scaleFactor )
200199 {
201200 int count = 0 ;
202- // combine the sums into mean and covariance matrices, then add the corresponding spot
203- for ( final Label label : labels )
201+ final ReentrantReadWriteLock lock = graph .getLock ();
202+ lock .writeLock ().lock ();
203+ final Spot ref = graph .vertexRef ();
204+ try
204205 {
205- // skip labels that are not present in the image or do not have at least 1 pixel
206- if ( label == null || label .numPixels < 1 )
207- continue ;
208- double [] mean = label .covariances .getMeans ();
209- double [][] cov ;
210- if ( label .numPixels == 1 )
211- cov = new double [ mean .length ][ mean .length ];
212- else
213- cov = label .covariances .get ();
214- for ( int i = 0 ; i < cov .length ; i ++ )
215- cov [ i ][ i ] += SINGLE_PIXEL_COVARIANCE ;
216- if ( mean .length == 2 ) // NB: 2D case, add a third dimension with 0 covariance
206+ // combine the sums into mean and covariance matrices, then add the corresponding spot
207+ for ( final Label label : labels )
217208 {
218- mean = new double [] { mean [ 0 ], mean [ 1 ], 0 };
219- cov = new double [][] {
220- { cov [ 0 ][ 0 ], cov [ 0 ][ 1 ], 0 },
221- { cov [ 1 ][ 0 ], cov [ 1 ][ 1 ], 0 },
222- { 0 , 0 , 1 }
209+ // skip labels that are not present in the image or do not have at least 1 pixel
210+ if ( label == null || label .numPixels < 1 )
211+ continue ;
212+ double [] mean = label .covariances .getMeans ();
213+ double [][] cov ;
214+ if ( label .numPixels == 1 )
215+ cov = new double [ mean .length ][ mean .length ];
216+ else
217+ cov = label .covariances .get ();
218+ for ( int i = 0 ; i < cov .length ; i ++ )
219+ cov [ i ][ i ] += SINGLE_PIXEL_COVARIANCE ;
220+ if ( mean .length == 2 ) // NB: 2D case, add a third dimension with 0 covariance
221+ {
222+ mean = new double [] { mean [ 0 ], mean [ 1 ], 0 };
223+ cov = new double [][] {
224+ { cov [ 0 ][ 0 ], cov [ 0 ][ 1 ], 0 },
225+ { cov [ 1 ][ 0 ], cov [ 1 ][ 1 ], 0 },
226+ { 0 , 0 , 1 }
227+ };
228+ }
229+ // transform ellipsoid center to mastodon coordinate system
230+ transform .apply ( mean , mean );
231+ // scale ellipsoid axes to desired factor
232+ scale ( cov , scaleFactor );
233+
234+ // transform ellipsoid axes to mastodon coordinate system
235+ double [][] transformMatrix = new double [ 3 ][ 4 ];
236+ transform .toMatrix ( transformMatrix );
237+ double [][] matrix3x3 = {
238+ { transformMatrix [ 0 ][ 0 ], transformMatrix [ 0 ][ 1 ], transformMatrix [ 0 ][ 2 ] },
239+ { transformMatrix [ 1 ][ 0 ], transformMatrix [ 1 ][ 1 ], transformMatrix [ 1 ][ 2 ] },
240+ { transformMatrix [ 2 ][ 0 ], transformMatrix [ 2 ][ 1 ], transformMatrix [ 2 ][ 2 ] }
223241 };
242+ double [][] temp = new double [ 3 ][ 3 ];
243+ double [][] covTransformed = new double [ 3 ][ 3 ];
244+ LinAlgHelpers .mult ( matrix3x3 , cov , temp );
245+ LinAlgHelpers .multABT ( temp , matrix3x3 , covTransformed );
246+
247+ try
248+ {
249+ Spot spot = graph .addVertex ( ref ).init ( frameId , mean , covTransformed );
250+ spot .setLabel ( String .valueOf ( label .value ) );
251+ count ++;
252+ }
253+ catch ( Exception e )
254+ {
255+ logger .trace ( "Could not add vertex to graph. Mean: {}, Covariance: {}" , Arrays .toString ( mean ),
256+ Arrays .deepToString ( covTransformed ) );
257+ }
224258 }
225- // transform ellipsoid center to mastodon coordinate system
226- transform .apply ( mean , mean );
227- // scale ellipsoid axes to desired factor
228- scale ( cov , scaleFactor );
229-
230- // transform ellipsoid axes to mastodon coordinate system
231- double [][] transformMatrix = new double [ 3 ][ 4 ];
232- transform .toMatrix ( transformMatrix );
233- double [][] matrix3x3 = {
234- { transformMatrix [ 0 ][ 0 ], transformMatrix [ 0 ][ 1 ], transformMatrix [ 0 ][ 2 ] },
235- { transformMatrix [ 1 ][ 0 ], transformMatrix [ 1 ][ 1 ], transformMatrix [ 1 ][ 2 ] },
236- { transformMatrix [ 2 ][ 0 ], transformMatrix [ 2 ][ 1 ], transformMatrix [ 2 ][ 2 ] }
237- };
238- double [][] temp = new double [ 3 ][ 3 ];
239- double [][] covTransformed = new double [ 3 ][ 3 ];
240- LinAlgHelpers .mult ( matrix3x3 , cov , temp );
241- LinAlgHelpers .multABT ( temp , matrix3x3 , covTransformed );
242-
243- try
244- {
245- Spot spot = graph .addVertex ().init ( frameId , mean , covTransformed );
246- spot .setLabel ( String .valueOf ( label .value ) );
247- count ++;
248- }
249- catch ( Exception e )
250- {
251- logger .trace ( "Could not add vertex to graph. Mean: {}, Covariance: {}" , Arrays .toString ( mean ),
252- Arrays .deepToString ( covTransformed ) );
253- }
259+ }
260+ finally
261+ {
262+ lock .writeLock ().unlock ();
263+ graph .releaseRef ( ref );
254264 }
255265 logger .debug ( "Added {} spot(s) to frame {}" , count , frameId );
256266 return count ;
@@ -262,12 +272,12 @@ private static int createSpotsFromFrameLabels( final ModelGraph graph, final int
262272 * @param minimumLabelValue the minimum value of the pixels in the image.
263273 * @param numLabels the number of labels in the frame.
264274 */
265- private static Label [] extractLabelsFromFrame ( final RandomAccessibleInterval < RealType < ? > > frame , int minimumLabelValue ,
275+ private static Label [] extractLabelsFromFrame ( final RandomAccessibleInterval < ? extends RealType < ? > > frame , int minimumLabelValue ,
266276 int numLabels )
267277 {
268278 Label [] labels = new Label [ numLabels ];
269279 // read all pixels of the picture to sum everything up
270- Cursor < RealType < ? > > cursor = Views . iterable ( frame ) .cursor ();
280+ Cursor < ? extends RealType < ? > > cursor = frame .cursor ();
271281 int [] pixel = new int [ cursor .numDimensions () ];
272282 while ( cursor .hasNext () )
273283 {
0 commit comments