Skip to content

Commit

Permalink
add image converter for DL4JConverter extension point
Browse files Browse the repository at this point in the history
  • Loading branch information
Treiblesschorle committed Nov 23, 2016
1 parent 927a0b3 commit 6fd963e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
6 changes: 6 additions & 0 deletions org.knime.knip.dl4j/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,11 @@
path="/labs/deeplearning/">
</category>
</extension>
<extension
point="org.knime.ext.dl4j.base.DL4JConverter">
<DL4JConverter
converterClass="org.knime.knip.dl4j.data.convert.ImgPlusValueToDoubleArrayConverter">
</DL4JConverter>
</extension>

</plugin>
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;
}

}

0 comments on commit 6fd963e

Please sign in to comment.