Skip to content

Commit 51c0920

Browse files
Merge pull request #131 from mastodon-sc/modify-label-image-utils
Modify label image utils
2 parents 326335c + e8a5f5e commit 51c0920

9 files changed

Lines changed: 73 additions & 64 deletions

File tree

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
steps:
1818
- uses: actions/checkout@v2
1919
- name: Set up Java
20-
uses: actions/setup-java@v2
20+
uses: actions/setup-java@v4
2121
with:
2222
java-version: '8'
2323
distribution: 'zulu'

.github/workflows/sonarcloud.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ jobs:
4040
steps:
4141
- uses: actions/checkout@v2
4242
- name: Set up JDK 17
43-
uses: actions/setup-java@v2
43+
uses: actions/setup-java@v4
4444
with:
4545
java-version: '17'
4646
distribution: 'zulu'
4747
cache: 'maven'
48-
4948
- name: Jacoco Report and SonarCloud Analysis
5049
env:
5150
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information

src/main/java/org/mastodon/mamut/io/importer/labelimage/ui/ImportSpotsFromBdvChannelView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
import bdv.viewer.SourceAndConverter;
3232
import org.mastodon.mamut.ProjectModel;
33-
import org.mastodon.mamut.io.importer.labelimage.LabelImageUtils;
33+
import org.mastodon.mamut.util.LabelImageUtils;
3434
import org.scijava.ItemVisibility;
3535
import org.scijava.command.Command;
3636
import org.scijava.command.DynamicCommand;

src/main/java/org/mastodon/mamut/io/importer/labelimage/ui/ImportSpotsFromImgPlusView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
import net.imagej.ImgPlus;
3232
import org.mastodon.mamut.ProjectModel;
33-
import org.mastodon.mamut.io.importer.labelimage.LabelImageUtils;
33+
import org.mastodon.mamut.util.LabelImageUtils;
3434
import org.scijava.ItemIO;
3535
import org.scijava.ItemVisibility;
3636
import org.scijava.command.Command;

src/main/java/org/mastodon/mamut/io/importer/labelimage/LabelImageUtils.java renamed to src/main/java/org/mastodon/mamut/util/LabelImageUtils.java

Lines changed: 65 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* POSSIBILITY OF SUCH DAMAGE.
2727
* #L%
2828
*/
29-
package org.mastodon.mamut.io.importer.labelimage;
29+
package org.mastodon.mamut.util;
3030

3131
import bdv.viewer.Source;
3232
import bdv.viewer.SourceAndConverter;
@@ -48,7 +48,6 @@
4848
import org.mastodon.mamut.model.Model;
4949
import org.mastodon.mamut.model.ModelGraph;
5050
import org.mastodon.mamut.model.Spot;
51-
import org.mastodon.mamut.util.LineageTreeUtils;
5251
import org.mastodon.views.bdv.SharedBigDataViewerData;
5352
import org.scijava.app.StatusService;
5453
import 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
{

src/test/java/org/mastodon/mamut/io/importer/labelimage/util/ComputeMeanAndVarianceDemo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import net.imglib2.util.Pair;
3939
import net.imglib2.util.StopWatch;
4040
import org.mastodon.mamut.ProjectModel;
41-
import org.mastodon.mamut.io.importer.labelimage.LabelImageUtils;
41+
import org.mastodon.mamut.util.LabelImageUtils;
4242
import org.mastodon.mamut.model.Model;
4343
import org.scijava.Context;
4444

src/test/java/org/mastodon/mamut/io/importer/labelimage/util/DemoUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import net.imglib2.view.Views;
4646
import org.mastodon.mamut.ProjectModel;
4747
import org.mastodon.mamut.io.ProjectSaver;
48-
import org.mastodon.mamut.io.importer.labelimage.LabelImageUtils;
48+
import org.mastodon.mamut.util.LabelImageUtils;
4949
import org.mastodon.mamut.io.importer.labelimage.math.CovarianceMatrix;
5050
import org.mastodon.mamut.io.importer.labelimage.math.MeansVector;
5151
import org.mastodon.mamut.io.project.MamutProject;

src/test/java/org/mastodon/mamut/io/importer/labelimage/util/SmallLabelDemo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import net.imglib2.img.array.ArrayImgs;
3535
import net.imglib2.type.numeric.real.FloatType;
3636
import org.mastodon.mamut.ProjectModel;
37-
import org.mastodon.mamut.io.importer.labelimage.LabelImageUtils;
37+
import org.mastodon.mamut.util.LabelImageUtils;
3838
import org.mastodon.mamut.model.Model;
3939
import org.scijava.Context;
4040

src/test/java/org/mastodon/mamut/io/importer/labelimage/LabelImageUtilsTest.java renamed to src/test/java/org/mastodon/mamut/util/LabelImageUtilsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* POSSIBILITY OF SUCH DAMAGE.
2727
* #L%
2828
*/
29-
package org.mastodon.mamut.io.importer.labelimage;
29+
package org.mastodon.mamut.util;
3030

3131
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
3232
import static org.junit.jupiter.api.Assertions.assertEquals;

0 commit comments

Comments
 (0)