Displaying an xray image

I'm trying to take an x-ray image (pixel values range from

0 - 65535 [16 bit] black -> white) and display it in a java window. I really have no clue where to start with this. So I'll explain what I have and maybe someone can help me out.

Given an image (size 1024x1024, these pixels will be read in as a byte array, so the size of the array will be 1048576), display this image to the screen using a scaling factor. The factor in this case will probably be the median of the whole image. This will give it a uniform look, or at least a look resembling an x-ray and not just black and white dots or lines. Remember, each pixel can have a value between 0 and 65535.

I'm looking for a gradient effect. If my factor is set to 10000, then values from 0 to 10000 will go from black to white. Anything over 10000 would be considered white.

If I had a factor of 65000 and most of my values were in the 0 - 1500 range, then the image would be black. In the same example, if I picked a factor of 0, then the image would be white except for those values of 0, which would be black.

Thank you

D

[1146 byte] By [DarrickWhite] at [2007-9-26 8:39:03]
# 1

You can use Java 2D to create a BufferedImage object from array of shorts. Here is an example that creates a grayscale image:

public static BufferedImage createImage(int imageWidth, int imageHeight, int imageDepth, short data[]){

ComponentColorModel ccm = new ComponentColorModel(

ColorSpace.getInstance(ColorSpace.CS_GRAY),

new int[] {imageDepth}, false, false,

Transparency.OPAQUE,

DataBuffer.TYPE_USHORT);

ComponentSampleModel csm = new ComponentSampleModel(

DataBuffer.TYPE_USHORT,

imageWidth, imageHeight,1,imageWidth, new int[] {0});

DataBuffer dataBuf = new DataBufferUShort((short[])data, imageWidth);

WritableRaster wr = Raster.createWritableRaster(csm, dataBuf, new Point(0,0));

Hashtable ht = new Hashtable();

ht.put("owner", "Lawrence Rodrigues");

return new BufferedImage(ccm, wr, true, ht);

}

The DataBuffer object can be constructed for different data types, including byte, short and int. Change the above method to suit your needs. JAI and JDK 1.4 (currently in beta) support float and double data types, but I don't think you need them. Change imageDepth appropriately to suit the range of your data.

Once you create your BufferedImage, apply different operations. Java 2D supports operations like rescale, lookup. See RescaleOp and LookupOp classes in the java.awt.image package. You can also manipulate the image geometrically using the AffineTransform and AffineTransformOp classes. Once you have a BufferedImage, you can display is on a JPanel.

To get the gradient effect, you can probably use JAI which has a large number of operators, including "Threshold" and "Clamp."

I have a few medical imaging related examples at http://cseng.aw.com/book/0,3828,0201700743,00.html. Download the source code and look at Chapter 8 and Chapter 13.

larryhr at 2007-7-1 19:25:08 > top of Java-index,Security,Cryptography...
# 2
Pretty cool. Can you answer me this, why does it take so long for the image to be displayed. I tried both an applet and application window. In both cases, the window pops up, but the image takes 10 seconds or so to show up?Thanks
DarrickWhite at 2007-7-1 19:25:08 > top of Java-index,Security,Cryptography...
# 3

It is hard to answer your question without looking at the code. I remember a couple of similar posts in the Java 2D or JAI lists. You can search the archives of these lists to see whether anyone posted a solution.

Also, you may want to look at the Java 2D FAQ at

http://java.sun.com/products/java-media/2D/forDevelopers/java2dfaq.html.

It does provide some hints to improve performance (such as creating a compatible BufferedImage for your screen).

larryhr at 2007-7-1 19:25:08 > top of Java-index,Security,Cryptography...