A question on image type converter

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

A question on image type converter

Daan Zhu
Dear ImageJ Users,
I need to write a program to simulate "ImageJ type converter".
My Images are signed 12-bit image and I need to convert to 8-bit (1
byte) gray image. ImageJ's output is exactly my wanted output.
Unfortunately I don't really know who ImageJ works. I investigated
ImageJ's source code.
------------------------------
ImagePlus imp = new ij.io.Opener imageOpener.openImage("Img12Bit.tif");
java.awt.Image img = ip.createImage();
BufferedImage bi = new BufferedImage(img.getWidth(null),
img.getHeight(null), BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g2 = bi.createGraphics();
g2.drawImage(img, 0, 0, null);
g2.dispose();
ImageIO.write(bi, "jpg", new File("Img8Bit.jpg"));
-------------------------------

It looks simple. But I can not understand how BufferedImage, Graphics2D works
pixel by pixel? Could any people kindly advise me?
If any people are interested to this question, I would like to send my
test program with sample image. Thank you.

Regards,

Daan
Reply | Threaded
Open this post in threaded view
|

Re: A question on image type converter

Albert Cardona
Daan Zhu,

ImageJ makes it much easier for you:

ImagePlus imp = new ij.io.Opener imageOpener.openImage("Img12Bit.tif");
imp.setProcessor(imp.getTitle(), imp.getProcessor().convertToByte(true));
new FileSaver(imp).saveAsJpeg("Img12Bit.jpg");


The paths may have to be absolute for you to control them properly.

Also: to save as jpeg, you don't need to call convertToByte at all.
ImageJ's jpeg saver will convert your image to an RGB in any case
(with scaling as well) and then save that to jpeg.
So the above need only be two lines, not three.


Albert

--
Albert Cardona
http://www.mcdb.ucla.edu/Research/Hartenstein/acardona