<div dir="ltr">Hi Brian,<div><br></div><div><div>> One thing I need to do is transform Img (actually ArrayImg) objects to</div><div>> BufferedImages.</div></div><div><br></div><div>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:</div>
<div><br></div><div><a href="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/display/projector</a><br>
</div><div><a href="https://github.com/imglib/imglib/tree/imglib2-2.0.0-beta-26/core/src/main/java/net/imglib2/converter">https://github.com/imglib/imglib/tree/imglib2-2.0.0-beta-26/core/src/main/java/net/imglib2/converter</a><br>
</div><div><br></div><div>This is how ImageJ2 actually paints Img objects on screen, using CompositeXYProjector and RealLUTConverter.<br></div><div><br></div><div>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.</div>
<div><br></div><div>Regards,<br>Curtis</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Aug 20, 2014 at 11:41 AM, Brian Schlining <span dir="ltr"><<a href="mailto:bschlining@gmail.com" target="_blank">bschlining@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><p>Hi Tobias (et al),</p>
<p>Thanks for getting back to me.</p>
<p>So it sounds like to go full round trip from <code>ArrayImg[x, y, 3] -> BufferedImage -> ArrayImg[x, y, 3]</code> I’ll need to manually unpack values from the buffered image. Something like:</p>
<pre><code>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))
}
}
</code></pre>
<p>Does that sound like the correct thing to do?</p>
<p>Thanks for you help</p>
<p>Brian</p>
<p></p></div><div><div><div class="h5"><div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto"><br></div> <br><p style="color:#000">On August 20, 2014 at 2:10:45 AM, Tobias Pietzsch (<a href="mailto:pietzsch@mpi-cbg.de" target="_blank">pietzsch@mpi-cbg.de</a>) wrote:</p>
</div></div><blockquote type="cite"><span><div style="word-wrap:break-word"><div></div><div><div><div class="h5">
Hi Brian,
<div><br></div>
<div>BufferedImage is always 2D, so you cannot have a BufferedImage
with dimensions (height, width, 3).</div>
<div><br></div>
<div>If you know that you have an ArrayImg and you know the pixel
Type, you can get to the underlying primitive array, for
example</div>
<div>
<div style="margin:0px;font-size:11px;font-family:Monaco">
<span style="color:rgb(147,26,104)">byte</span>[] array = (
<span style="color:#931a68">byte</span>[] ) ( (
ArrayDataAccess< ? > ) img.update( <span style="color:#931a68">null</span> ) ).getCurrentStorageArray()</div>
</div>
<div>if you know that img is a UnsignedByteType ArrayImg. Then you
wrap that in a BufferedImage.</div>
<div><br></div>
<div>You can do the same thing the other way around: Get the
primitive array from the BufferedImage and wrap it in an
ArrayImg.</div>
<div><br></div>
<div>Essentially the code you found does that for you for the
standard PixelTypes (UnsignedByteType, ARGBType, etc )...</div>
<div><br></div>
<div>best regards,</div>
<div>Tobias</div>
<div><br>
<div>
<div>On 20 Aug 2014, at 00:20, Brian Schlining <<a href="mailto:bschlining@gmail.com" target="_blank">bschlining@gmail.com</a>>
wrote:</div>
<br>
<blockquote type="cite">
<div style="font-family:Helvetica,Arial;padding:1em;margin:auto;background-color:rgb(254,254,254);font-size:13px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word">
<div>
<p style="margin:15px 0px">Hi All,</p>
<p style="margin:15px 0px">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).</p>
<p style="margin:15px 0px"><strong>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!!</strong><span> </span>Here’s the method I found:</p>
<pre style="margin:15px 0px;font-family:Monaco;font-size:10pt;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(248,248,248);color:inherit;border:1px solid rgb(204,204,204);overflow:auto;padding:4px 8px">
<code style="font-family:Monaco;font-size:10pt;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;background-color:rgb(248,248,248);color:inherit;border:0px;margin:0px;padding:0px">// Scala code, sorry Java folks.
import net.imglib2.img.display.imagej.ImageJFunctions
val imagePlus = ImageJFunctions.wrap(img, "")
val bufferedImage = imagePlus.getBufferedImage
</code>
</pre>
<p style="margin:15px 0px"><strong>How do I convert the buffered
image back to an Img object?</strong><span> </span>I saw this code at<span> </span><a href="https://github.com/imglib/imglib/blob/imglib2-2.0.0-beta-21/scripting/src/main/java/net/imglib2/script/bufferedimag/BufferedImageImg.java" style="color:rgb(65,131,196);background-color:inherit;text-decoration:none" target="_blank">https://github.com/imglib/imglib/…/BufferedImageImg.java</a>,
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.</p>
<p style="margin:15px 0px">Thanks!</p>
<p style="margin:15px 0px">Brian</p>
</div>
<div>
<div style="font-family:Helvetica,Arial;font-size:13px;margin:0px">
<br></div>
<br>
<div>
<div style="font-family:helvetica,arial;font-size:13px">
-- <br>
Brian Schlining<br></div>
</div>
</div>
<div>
<div style="margin:15px 0px"><br></div>
</div>
_______________________________________________<br>
ImageJ-devel mailing list<br>
<a href="mailto:ImageJ-devel@imagej.net" style="color:rgb(65,131,196);background-color:inherit;text-decoration:none" target="_blank">
ImageJ-devel@imagej.net</a><br>
<a href="http://imagej.net/mailman/listinfo/imagej-devel" style="color:rgb(65,131,196);background-color:inherit;text-decoration:none" target="_blank">
http://imagej.net/mailman/listinfo/imagej-devel</a><br></div>
</blockquote>
</div>
<br></div>
</div></div><hr></div></div></span></blockquote><span class="HOEnZb"><font color="#888888"> <div><div style="font-family:helvetica,arial;font-size:13px">-- <br>Brian Schlining<br></div></div></font></span></div><div><p></p>
</div></div><br>_______________________________________________<br>
ImageJ-devel mailing list<br>
<a href="mailto:ImageJ-devel@imagej.net">ImageJ-devel@imagej.net</a><br>
<a href="http://imagej.net/mailman/listinfo/imagej-devel" target="_blank">http://imagej.net/mailman/listinfo/imagej-devel</a><br>
<br></blockquote></div><br></div>