Skip to content

Commit 0eefb73

Browse files
committed
Util: add method to convert images of RealType to double[]
1 parent a4ea14d commit 0eefb73

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

src/main/java/net/imglib2/util/Util.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,21 @@
4545
import net.imglib2.RealLocalizable;
4646
import net.imglib2.RealRandomAccess;
4747
import net.imglib2.RealRandomAccessible;
48+
import net.imglib2.img.Img;
4849
import net.imglib2.img.ImgFactory;
4950
import net.imglib2.img.array.ArrayImg;
5051
import net.imglib2.img.array.ArrayImgFactory;
5152
import net.imglib2.img.cell.CellImgFactory;
5253
import net.imglib2.img.list.ListImgFactory;
5354
import net.imglib2.type.NativeType;
5455
import net.imglib2.type.Type;
56+
import net.imglib2.type.numeric.RealType;
5557
import net.imglib2.type.operators.ValueEquals;
5658
import net.imglib2.view.Views;
5759

5860
import java.util.List;
5961
import java.util.function.BiPredicate;
62+
import java.util.stream.StreamSupport;
6063

6164
/**
6265
* A collection of general-purpose utility methods for working with ImgLib2 data
@@ -1003,4 +1006,30 @@ final static public void max( final double[] a, final double[] b )
10031006
if ( b[ i ] > a[ i ] )
10041007
a[ i ] = b[ i ];
10051008
}
1009+
1010+
/**
1011+
* Returns the content of {@code Iterable<RealType>} as array of doubles.
1012+
*/
1013+
public static double[] asDoubleArray( Iterable< ? extends RealType< ? > > iterable )
1014+
{
1015+
return StreamSupport.stream( iterable.spliterator(), false ).mapToDouble( RealType::getRealDouble ).toArray();
1016+
}
1017+
1018+
/**
1019+
* Returns the pixels of an RandomAccessibleInterval of RealType as array of doubles.
1020+
* The pixels are sorted in flat iteration order.
1021+
*/
1022+
public static double[] asDoubleArray( RandomAccessibleInterval< ? extends RealType< ? > > rai )
1023+
{
1024+
return asDoubleArray( Views.flatIterable( rai ) );
1025+
}
1026+
1027+
/**
1028+
* Returns the pixels of an image of RealType as array of doubles.
1029+
* The pixels are sorted in flat iteration order.
1030+
*/
1031+
public static double[] asDoubleArray( Img< ? extends RealType< ? > > image )
1032+
{
1033+
return asDoubleArray( ( RandomAccessibleInterval< ? extends RealType< ? > > ) image );
1034+
}
10061035
}

src/test/java/net/imglib2/util/UtilTest.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@
3434

3535
package net.imglib2.util;
3636

37-
import static junit.framework.TestCase.assertFalse;
38-
import static org.junit.Assert.assertEquals;
39-
import static org.junit.Assert.assertTrue;
40-
4137
import net.imglib2.FinalInterval;
4238
import net.imglib2.RealLocalizable;
4339
import net.imglib2.RealPoint;
@@ -49,14 +45,18 @@
4945
import net.imglib2.img.list.ListImgFactory;
5046
import net.imglib2.type.logic.BitType;
5147
import net.imglib2.type.logic.BoolType;
52-
5348
import net.imglib2.type.numeric.RealType;
5449
import net.imglib2.type.numeric.integer.IntType;
5550
import net.imglib2.type.numeric.real.DoubleType;
5651
import org.junit.Test;
5752

5853
import java.util.function.BiPredicate;
5954

55+
import static junit.framework.TestCase.assertFalse;
56+
import static org.junit.Assert.assertArrayEquals;
57+
import static org.junit.Assert.assertEquals;
58+
import static org.junit.Assert.assertTrue;
59+
6060
public class UtilTest
6161
{
6262

@@ -178,4 +178,13 @@ private Img<DoubleType > doublesImage( double... pixels )
178178
{
179179
return ArrayImgs.doubles( pixels, pixels.length );
180180
}
181+
182+
@Test
183+
public void testAsDoubleArray()
184+
{
185+
double[] expected = { 1, 2, 3, 4 };
186+
Img< DoubleType > img = ArrayImgs.doubles( expected, 2, 2 );
187+
double[] result = Util.asDoubleArray( img );
188+
assertArrayEquals( expected, result, 0.0 );
189+
}
181190
}

0 commit comments

Comments
 (0)