-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add image converter for DL4JConverter extension point
- Loading branch information
1 parent
927a0b3
commit 6fd963e
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...me.knip.dl4j/src/org/knime/knip/dl4j/data/convert/ImgPlusValueToDoubleArrayConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.knime.knip.dl4j.data.convert; | ||
|
||
import java.util.Iterator; | ||
|
||
import org.knime.ext.dl4j.base.data.convert.extension.BaseDL4JConverter; | ||
import org.knime.knip.base.data.img.ImgPlusValue; | ||
|
||
import net.imglib2.img.Img; | ||
import net.imglib2.type.numeric.RealType; | ||
|
||
/** | ||
* DL4JConverter that converts a ImgPlusValue to double[]. | ||
* | ||
* @author David Kolb, KNIME.com GmbH | ||
* | ||
* @param <T> | ||
*/ | ||
@SuppressWarnings("rawtypes") | ||
public class ImgPlusValueToDoubleArrayConverter<T extends RealType<T>> | ||
extends BaseDL4JConverter<ImgPlusValue, double[]> { | ||
|
||
/** | ||
* Constructor for class ImgPlusValueToDoubleArrayConverter. | ||
*/ | ||
public ImgPlusValueToDoubleArrayConverter() { | ||
super(ImgPlusValue.class, double[].class, BaseDL4JConverter.DEFAULT_PRIORITY); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} Flattens the image and copies the pixel values to double[]. | ||
*/ | ||
@Override | ||
@SuppressWarnings("unchecked") | ||
public double[] convert(ImgPlusValue source) throws Exception { | ||
Img<T> img = source.getImgPlus(); | ||
Iterator<T> imgIter = img.iterator(); | ||
double[] flattenedImg = new double[(int) calcNumPixels(source.getDimensions())]; | ||
int i = 0; | ||
while (imgIter.hasNext()) { | ||
flattenedImg[i] = imgIter.next().getRealDouble(); | ||
i++; | ||
} | ||
return flattenedImg; | ||
} | ||
|
||
private long calcNumPixels(long[] dims) { | ||
long numPix = 1; | ||
for (long dim : dims) { | ||
numPix *= dim; | ||
} | ||
return numPix; | ||
} | ||
|
||
} |