[Insight-users] Import/Export Java images
sumit shah
sshah at mednet.ucla.edu
Tue May 17 12:34:59 EDT 2005
Hi,
I've pasted some Java code that I use. It may not compile as is, but
should give you a head start.
Here is an example of how to get an itkImageF3 from a stack of
BufferedImages:
public itkImageF3 getITK(Stack<BufferedImage> is, double voxsize[]) {
itkImportImageFilterF3_Pointer importer =
itkImportImageFilterF3.itkImportImageFilterF3_New();
itkIndex3 start = new itkIndex3();
itkSize3 size = new itkSize3();
itkImageRegion3 region = new itkImageRegion3();
itkPointD3 origin = new itkPointD3();
BufferedImage secondToLast = is.elementAt(is.size() - 2), last =
is.lastElement();
int x = last.getWidth();
int y = last.getHeight();
int z = is.size();
size.SetElement(0, x);
size.SetElement(1, y);
size.SetElement(2, z);
start.SetElement(0, 0);
start.SetElement(1, 0);
start.SetElement(2, 0);
region.SetSize(size);
region.SetIndex(start);
double[] pix = new double[2];
itkVectorD3 spacing = new itkVectorD3();
spacing.SetElement(0, voxSize[0]);
spacing.SetElement(1, voxSize[1]);
spacing.SetElement(2, voxSize[2]);
importer.SetSpacing(spacing);
origin.SetElement(0, 0);
origin.SetElement(1, 0);
origin.SetElement(2, 0);
importer.SetOrigin(origin);
importer.SetRegion(region);
buf = SwigExtras.new_FArray(x * y * z);
float f[] = new float[x*y];
int i = 0;
itkProg = 0;
for(BufferedImage b:is) {
Raster r = b.getData();
f = r.getSamples(0, 0, x, y, 0, f);
for(int j = 0; j < f.length; j++) {
SwigExtras.FArray_setitem(buf, i, f[i]);
i++;
}
}
importer.SetImportPointer(buf, x * y * x, false);
itkProg = 0;
importer.Update();
return importer.GetOutput();
// don't forget to call SwigExtras.delete_FArray(buf); when you are
done using it!!!
}
Here is an example to do an affine transform on an image (tarITK
variable which is an object of type itkImageF3):
final itkResampleImageFilterF3F3_Pointer resample =
itkResampleImageFilterF3F3.itkResampleImageFilterF3F3_New();
final itkLinearInterpolateImageFunctionF3D_Pointer interpolator =
itkLinearInterpolateImageFunctionF3D.itkLinearInterpolateImageFunctionF3
D_New();
final itkAffineTransform3_Pointer transform =
itkAffineTransform3.itkAffineTransform3_New();
final itkArrayD parameters = new itkArrayD(12);
for(int i = 0; i < fp.length; i++) parameters.SetElement
(i, fp[i]);
transform.SetParameters(parameters);
resample.SetTransform(transform.GetPointer());
resample.SetInterpolator(interpolator.GetPointer());
resample.SetInput(tarITK);
resample.SetSize(tarITK.GetLargestPossibleRegion
().GetSize());
resample.SetOutputOrigin(tarITK.GetOrigin());
resample.SetOutputSpacing(tarITK.GetSpacing());
resample.SetDefaultPixelValue(-32768);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
tp.setMaximum(100);
tp.setValue(0);
}
}
);
class CommandIterationUpdate extends itkJavaCommand {
public void Execute() {
updateTask.val = (int)(100*resample.GetProgress());
SwingUtilities.invokeLater(updateTask);
}
}
CommandIterationUpdate p = new CommandIterationUpdate();
resample.AddObserver(new itkProgressEvent(), p);
resample.Update();
On May 17, 2005, at 2:09 AM, Stefan Lindenau wrote:
> Hi,
>
> currently I am trying to use the ITK with the Java Wrapping. For my
> applications I need to import/export itk images into Java Images.
>
> Although I have found out that there should be a mechanism to
> import/export itk images to Java images (http://public.kitware.com/
> pipermail/insight-users/2004-June/008887.html). I did not find out
> how to use these mechanisms.
>
> Do you have any documentation for the Java Wrapping or a starting
> point?
>
> Thank you,
> Stefan
> _______________________________________________
> Insight-users mailing list
> Insight-users at itk.org
> http://www.itk.org/mailman/listinfo/insight-users
>
More information about the Insight-users
mailing list