Img <-> BufferedImage conversion

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Img <-> BufferedImage conversion

Brian Schlining

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
[hidden email]
http://imagej.net/mailman/listinfo/imagej-devel
Reply | Threaded
Open this post in threaded view
|

Re: Img <-> BufferedImage conversion

Tobias Pietzsch
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 <[hidden email]> 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
[hidden email]
http://imagej.net/mailman/listinfo/imagej-devel


_______________________________________________
ImageJ-devel mailing list
[hidden email]
http://imagej.net/mailman/listinfo/imagej-devel

signature.asc (465 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Img <-> BufferedImage conversion

Brian Schlining

Hi Tobias (et al),

Thanks for getting back to me.

So it sounds like to go full round trip from ArrayImg[x, y, 3] -> BufferedImage -> ArrayImg[x, y, 3] I’ll need to manually unpack values from the buffered image. Something like:

val arraying = // initialize empty ArrayImg
val cursor = arrayImg.randomAccess()

for (x <- 0 until bufferedImage.getWidth;  
     y <- 0 until bufferedImage.getHeight) {
         
  val rgb: Int = bufferedImage.getRGB
  val b: Array[Byte] = // unpack rbg to components
  for (c <- 0 until 3) {
        cursor.setPosition(x, y, c)
        cursor.get.set(b(c))
  }
}

Does that sound like the correct thing to do?

Thanks for you help

Brian



On August 20, 2014 at 2:10:45 AM, Tobias Pietzsch ([hidden email]) wrote:

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 <[hidden email]> 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
[hidden email]
http://imagej.net/mailman/listinfo/imagej-devel


-- 
Brian Schlining


_______________________________________________
ImageJ-devel mailing list
[hidden email]
http://imagej.net/mailman/listinfo/imagej-devel
Reply | Threaded
Open this post in threaded view
|

Re: Img <-> BufferedImage conversion

Curtis Rueden
Hi Brian,

> One thing I need to do is transform Img (actually ArrayImg) objects to
> BufferedImages.

Note that if what you want is to transform N-dimensional Imgs to _rendered_ BufferedImages (e.g., at specific planes, perhaps composited, and so forth), you can use the Projector and Converter API:


This is how ImageJ2 actually paints Img objects on screen, using CompositeXYProjector and RealLUTConverter.

If, on the other hand, you actually want the BufferedImage objects to contain raw data, it might be helpful for you to describe your use case in more detail. In my experience, BufferedImages are designed for image data intended for blitting to the screen, as opposed to raw scientific image samples. Trying to use them for the latter is likely to cause more problems than it solves.

Regards,
Curtis


On Wed, Aug 20, 2014 at 11:41 AM, Brian Schlining <[hidden email]> wrote:

Hi Tobias (et al),

Thanks for getting back to me.

So it sounds like to go full round trip from ArrayImg[x, y, 3] -> BufferedImage -> ArrayImg[x, y, 3] I’ll need to manually unpack values from the buffered image. Something like:

val arraying = // initialize empty ArrayImg
val cursor = arrayImg.randomAccess()

for (x <- 0 until bufferedImage.getWidth;  
     y <- 0 until bufferedImage.getHeight) {
         
  val rgb: Int = bufferedImage.getRGB
  val b: Array[Byte] = // unpack rbg to components
  for (c <- 0 until 3) {
        cursor.setPosition(x, y, c)
        cursor.get.set(b(c))
  }
}

Does that sound like the correct thing to do?

Thanks for you help

Brian



On August 20, 2014 at 2:10:45 AM, Tobias Pietzsch ([hidden email]) wrote:

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 <[hidden email]> 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
[hidden email]
http://imagej.net/mailman/listinfo/imagej-devel


-- 
Brian Schlining


_______________________________________________
ImageJ-devel mailing list
[hidden email]
http://imagej.net/mailman/listinfo/imagej-devel



_______________________________________________
ImageJ-devel mailing list
[hidden email]
http://imagej.net/mailman/listinfo/imagej-devel
Reply | Threaded
Open this post in threaded view
|

Re: Img <-> BufferedImage conversion

Brian Schlining

Hi Curtis,

One thing I need to do is transform Img (actually ArrayImg) objects to BufferedImages.

Note that if what you want is to transform N-dimensional Imgs to rendered BufferedImages (e.g., at specific planes, perhaps composited, and so forth), you can use the Projector and Converter API:

Yes, for the most part, I want to transform the Img to BufferedImages so that I can draw info onto them. (See attached example). A full round-trip might not be needed, but it would be a nice to have.

https://github.com/imglib/imglib/tree/imglib2–2.0.0-beta–26/core/src/main/java/net/imglib2/display/projector https://github.com/imglib/imglib/tree/imglib2–2.0.0-beta–26/core/src/main/java/net/imglib2/converter

This is how ImageJ2 actually paints Img objects on screen, using CompositeXYProjector and RealLUTConverter.

Can you give me a code example of how this would work? It’s not obvious from the javadocs how to use a Projector and Converter to generate a BufferedImage.

– Brian Schlining


_______________________________________________
ImageJ-devel mailing list
[hidden email]
http://imagej.net/mailman/listinfo/imagej-devel

=?utf-8?Q?filter=5F3-20090430221134.png?= (238K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Img <-> BufferedImage conversion

Curtis Rueden
Hi Brian,

> Can you give me a code example of how this would work? It’s not
> obvious from the javadocs how to use a Projector and Converter to
> generate a BufferedImage.

I whipped one up for you.

Regards,
Curtis


On Wed, Aug 20, 2014 at 1:13 PM, Brian Schlining <[hidden email]> wrote:

Hi Curtis,

One thing I need to do is transform Img (actually ArrayImg) objects to BufferedImages.

Note that if what you want is to transform N-dimensional Imgs to rendered BufferedImages (e.g., at specific planes, perhaps composited, and so forth), you can use the Projector and Converter API:

Yes, for the most part, I want to transform the Img to BufferedImages so that I can draw info onto them. (See attached example). A full round-trip might not be needed, but it would be a nice to have.

https://github.com/imglib/imglib/tree/imglib2–2.0.0-beta–26/core/src/main/java/net/imglib2/display/projector https://github.com/imglib/imglib/tree/imglib2–2.0.0-beta–26/core/src/main/java/net/imglib2/converter

This is how ImageJ2 actually paints Img objects on screen, using CompositeXYProjector and RealLUTConverter.

Can you give me a code example of how this would work? It’s not obvious from the javadocs how to use a Projector and Converter to generate a BufferedImage.

– Brian Schlining



_______________________________________________
ImageJ-devel mailing list
[hidden email]
http://imagej.net/mailman/listinfo/imagej-devel