[ImageJ-devel] Img <-> BufferedImage conversion

Tobias Pietzsch pietzsch at mpi-cbg.de
Wed Aug 20 04:10:44 CDT 2014


Hi Brian,

BufferedImage is always 2D, so you cannot have a BufferedImage with dimensions (height, width, 3).

If you know that you have an ArrayImg and you know the pixel Type, you can get to the underlying primitive array, for example
byte[] array = ( byte[] ) ( ( ArrayDataAccess< ? > ) img.update( null ) ).getCurrentStorageArray()
if you know that img is a UnsignedByteType ArrayImg. Then you wrap that in a BufferedImage.

You can do the same thing the other way around: Get the primitive array from the BufferedImage and wrap it in an ArrayImg.

Essentially the code you found does that for you for the standard PixelTypes (UnsignedByteType, ARGBType, etc )...

best regards,
Tobias

On 20 Aug 2014, at 00:20, Brian Schlining <bschlining at gmail.com> wrote:

> Hi All,
> 
> I’m trying to use imglib2 for some image processing. One thing I need to do is transform Img (actually ArrayImg) objects to BufferedImages. I’d also like to be able to transform those BufferedImages back into ArrayImg objects. For the most part I’m currently working with just PNG and JPEG (i.e. ARGB).
> 
> I think I found a way to convert an Img to BufferedImage, but I’m not sure this is the recommended method. So if anyone has a better recommendation, please let me know!! Here’s the method I found:
> 
> // Scala code, sorry Java folks.
> 
> import net.imglib2.img.display.imagej.ImageJFunctions
> 
> val imagePlus = ImageJFunctions.wrap(img, "")
> val bufferedImage = imagePlus.getBufferedImage
> 
> How do I convert the buffered image back to an Img object? I saw this code at https://github.com/imglib/imglib/…/BufferedImageImg.java, but the resulting ArrayImg has dimensions of (height, width, 1) instead of the expected dimensions of (heigh, width, 3), so it doesn’t appear to be doing the right thing.
> 
> Thanks!
> 
> Brian
> 
> 
> 
> -- 
> Brian Schlining
> 
> _______________________________________________
> ImageJ-devel mailing list
> ImageJ-devel at imagej.net
> http://imagej.net/mailman/listinfo/imagej-devel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://imagej.net/pipermail/imagej-devel/attachments/20140820/ed3ac6c5/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 455 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://imagej.net/pipermail/imagej-devel/attachments/20140820/ed3ac6c5/attachment.pgp>


More information about the ImageJ-devel mailing list