Hi,
i am trying to get a slice from any axis like axial, sagital or coronal from dicom volume,
i already made it with python, but due to python limitations i migrate to java.
this is how i load the volume:
public static org.itk.simple.Image loadLocalFolder (Stage stage) throws Exception {
String dicomFolderPath = LocalLoader.selectFolder(stage);
ImageSeriesReader reader = new ImageSeriesReader();
VectorString dicomNames = ImageSeriesReader.getGDCMSeriesFileNames(dicomFolderPath);
reader.setFileNames(dicomNames);
Image readerResult = reader.execute();
return readerResult;
}
this is how i get the slice:
public static Image axisSlice (Image volume, long[] sliceSize, int[] sliceIndex) {
VectorUInt32 vectorSize = new VectorUInt32(sliceSize);
VectorInt32 vectorIndex = new VectorInt32(sliceIndex);
return SimpleITK.extract(volume, vectorSize, vectorIndex);
}
with the result of axisSlice, i’m trying to display it in my application with javaFX.
So i made a function that transform the extractedImage in a byte array:
public static byte[] volumeToByteArray (Image volume) {
ByteBuffer imageDataBuffer = volume.getBufferAsByteBuffer();
byte[] imageByteArray = new byte[imageDataBuffer.remaining()];
imageDataBuffer.get(imageByteArray);
return imageByteArray;
}
from here things starts getting a little weird.
example: if the extracted image size is 512x512, im expeting volumeToByteArray return a 262.144 byte array lenght. But it is returning twice as much, so the returned byte array has 524.288 lenght.
A variable of type byte can have a value between -128 and 127, so it has a range of 256 possible values.
As dicom files only have black and white colors, my understanding is that each byte should represent an RGB grayscale pixel. So RGB(0, 0, 0) is totally black, and RGB(255, 255, 255) is totally white.
You can see this by typing “colorpicker” in google:
i understand that this is not a javafx community, but trying to create the images from byte array data result in unsatisfactory results, maybe meaning that the byte array data isnt correct. I can only open a question on stackoverflow now.
The fact that converting the image to a byte array is returning a vector with twice the expected size can fit here, can anyone help me?
java created image from a dicom folder, from axisSlice(volume, {512, 512, 0}, {0, 0, 10}):
python created image from same dicom folder, from axisSlice(volume, {512, 512, 0}, {0, 0, 10}):
java image generator code:
public static WritableImage volumeToImage (Image volume) {
byte[] imageByteArray = Slicer.volumeToByteArray(volume);
int imageHeight = volume.getSize().get(0).intValue();
int imageWidth = volume.getSize().get(1).intValue();
WritableImage writableImage = new WritableImage(imageWidth, imageHeight);
PixelWriter pixelWriter = writableImage.getPixelWriter();
int line;
int column;
int maxIndex = imageHeight*imageWidth;
for (int index = 0; index < maxIndex; index++) {
line = (index/imageWidth);
column = index - line*imageWidth;
int grayIntensity = imageByteArray[index] + 128;
pixelWriter.setColor(line, column, javafx.scene.paint.Color.rgb(grayIntensity, grayIntensity, grayIntensity));
}
return writableImage;
}
Image extractedImage = Slicer.axisSlice(volume.getVolume(), new long[]{512, 512, 0}, new int[]{0, 0, 10});
WritableImage writableImage = Slicer.volumeToImage(volume.getVolume());
Viewport.imageView.setImage(writableImage);