So after some more work, I figured out that's it definitely the setCharArray method that's not working.<div><br></div><div>After testing I have realized that this works but is very slow (too slow to be usable)</div>
<div><br></div><div><div><div>private void setCharArray(vtkImageData image, char[] array) {</div><div> vtkCharArray ca = new vtkCharArray();</div><div> ca.Initialize();</div><div> ca.SetNumberOfValues(array.length);</div>
<div> //ca.SetJavaArray(array);</div><div> int i=0;</div><div> for (char data : array) {</div><div> ca.SetValue(i, data);</div><div> ++i;</div><div> }</div><div> ca.Modified();</div>
<div> ca.SetName("ImageScalars");</div><div> image.GetPointData().SetScalars(ca);</div><div> image.Update();</div><div>}</div></div><div><br></div><div>and this does not work (but it is fast about it's not working)</div>
<div><br></div><div><div><div>private void setCharArray(vtkImageData image, char[] array) {</div><div> vtkCharArray ca = new vtkCharArray();</div><div> ca.Initialize();</div><div> ca.SetNumberOfValues(array.length);</div>
<div> ca.SetJavaArray(array);</div><div> /*int i=0;</div><div> for (char data : array) {</div><div> ca.SetValue(i, data);</div><div> ++i;</div><div> }*/</div><div> ca.Modified();</div>
<div> ca.SetName("ImageScalars");</div><div> image.GetPointData().SetScalars(ca);</div><div> image.Update();</div><div> }</div></div></div><div><br></div><div>Why isn't SetJavaArray() working?</div>
<br><div class="gmail_quote">On Mon, Mar 28, 2011 at 2:17 PM, David Gobbi <span dir="ltr"><<a href="mailto:david.gobbi@gmail.com">david.gobbi@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
I can't see anything that is obviously wrong, but my experience<br>
with Java is limited.<br>
<font color="#888888"><br>
- David<br>
</font><div><div></div><div class="h5"><br>
<br>
On Mon, Mar 28, 2011 at 2:42 PM, Jonathan Morra <<a href="mailto:jonmorra@gmail.com">jonmorra@gmail.com</a>> wrote:<br>
> After playing around a little bit, I have come up with the following that's<br>
> not currently working. Could you please advise me why this isn't working.<br>
> Thanks<br>
> put() {<br>
> // Organ data contains a list of char[]<br>
> final char[] storedCharArray = organData.get(imageNumber);<br>
> final char[] binaryCharArray = getCharArray(binaryOrgan);<br>
> int i=0;<br>
> for (char binaryData : binaryCharArray) {<br>
> char storedData = storedCharArray[i];<br>
> if (binaryData == 0)<br>
> storedData &= ~(1 << bitNumber);<br>
> else<br>
> storedData |= (1 << bitNumber);<br>
> storedCharArray[i] = storedData;<br>
> ++i;<br>
> }<br>
> get() {<br>
> vtkImageData binaryImage = // Get blank image of correct dimensions,<br>
> orientation, spacing, etc.<br>
> final char[] storedCharArray = organData.get(imageNumber);<br>
> final char[] binaryCharArray = getCharArray(binaryImage);<br>
> int i=0;<br>
> for (char storedData : storedCharArray) {<br>
> binaryCharArray[i] = (char)((storedData >> bitNumber) & 1);<br>
> ++i;<br>
> }<br>
> setCharArray(binaryImage, binaryCharArray);<br>
> private void setCharArray(vtkImageData image, char[] array) {<br>
><br>
> ((vtkCharArray)image.GetPointData().GetScalars()).SetJavaArray(array);<br>
> }<br>
> private char[] getCharArray(vtkImageData image) {<br>
> vtkPointData points = image.GetPointData();<br>
> vtkCharArray ca = (vtkCharArray)points.GetScalars();<br>
> char[] chars = ca.GetJavaArray();<br>
> return chars;<br>
> }<br>
><br>
> On Fri, Mar 25, 2011 at 12:48 PM, David Gobbi <<a href="mailto:david.gobbi@gmail.com">david.gobbi@gmail.com</a>> wrote:<br>
>><br>
>> In python it is possible to access the VTK data arrays directly via<br>
>> python's buffer interface, so that's what I use when I want to quickly<br>
>> move pixel data back and forth from python to VTK. It allows me to<br>
>> use a VTK array (including the one that the image uses to store its<br>
>> pixels) as if it was a native python array.<br>
>><br>
>> I have a feeling that the only way you will be able to rapidly do VTK<br>
>> pixel operations of any kind from Java, is if someone adds a similar<br>
>> feature to the Java wrappers.<br>
>><br>
>> - David<br>
>><br>
>><br>
>> On Fri, Mar 25, 2011 at 1:31 PM, Jonathan Morra <<a href="mailto:jonmorra@gmail.com">jonmorra@gmail.com</a>><br>
>> wrote:<br>
>> > Your understanding is correct, however I cannot<br>
>> > use GetScalarComponentAsDouble and SetScalarComponentAsDouble because<br>
>> > they<br>
>> > are way too slow in Java. Do you have any other suggestions for how to<br>
>> > do<br>
>> > this or will I have to write it myself in C++?<br>
>> ><br>
>> > On Fri, Mar 25, 2011 at 12:29 PM, David Gobbi <<a href="mailto:david.gobbi@gmail.com">david.gobbi@gmail.com</a>><br>
>> > wrote:<br>
>> >><br>
>> >> Hi Jonathan,<br>
>> >><br>
>> >> If I understand correctly, you have several binary images with exactly<br>
>> >> the same dimensions. And you want to collapse them into a single<br>
>> >> image, by storing a bitfield in the pixels of that image. So:<br>
>> >><br>
>> >> To set bit "i" in pixel "xyz" (C++ code):<br>
>> >> int a = int(image.GetScalarComponentAsDouble(x, y, z, 0));<br>
>> >> a |= (1 << i);<br>
>> >> image.SetScalarComponentAsDouble(x, y, z, a);<br>
>> >><br>
>> >> To clear bit "i" in pixel "xyz":<br>
>> >> int a = int(image.GetScalarComponentAsDouble(x, y, z, 0));<br>
>> >> a &= ~(1 << i);<br>
>> >> image.SetScalarComponentAsDouble(x, y, z, a);<br>
>> >><br>
>> >> To test bit "i" in pixel "xyz":<br>
>> >> int a = int(image.GetScalarComponentAsDouble(x, y, z, 0));<br>
>> >> return ((a >> i) & 1);<br>
>> >><br>
>> >> But I'm not sure if this is what you are trying to do. I didn't fully<br>
>> >> understand your pseudocode.<br>
>> >><br>
>> >> - David<br>
>> >><br>
>> >><br>
>> >><br>
>> >> On Fri, Mar 25, 2011 at 1:08 PM, Jonathan Morra <<a href="mailto:jonmorra@gmail.com">jonmorra@gmail.com</a>><br>
>> >> wrote:<br>
>> >> > I'm in Java and storing a large number of binary vtkImageData's. I<br>
>> >> > know<br>
>> >> > this is inefficient, and am searching for a better way to store them.<br>
>> >> > Right<br>
>> >> > now I have a hash of the vtkImageData's and a get/put function.<br>
>> >> > Basically I<br>
>> >> > want to mimic this get/put but with better storage. I was thinking<br>
>> >> > one<br>
>> >> > way<br>
>> >> > to do this is to use bitwise logical operations to store the<br>
>> >> > information<br>
>> >> > in<br>
>> >> > the binary masks. For instance, if we have 2 binary images, then we<br>
>> >> > could<br>
>> >> > store that information in 1 vtkImageData using the following pseudo<br>
>> >> > code.<br>
>> >> > private static final int IMAGE1_BIT_CHANNEL = 0;<br>
>> >> > private static final int IMAGE2_BIT_CHANNEL = 1;<br>
>> >> > private vtkImageData storedImage;<br>
>> >> > vtkImageData get(String image) {<br>
>> >> > int channel = image eq "image1" ? IMAGE1_BIT_CHANNEL :<br>
>> >> > IMAGE2_BIT_CHANNEL;<br>
>> >> > vtkImageData return = new vtkImageData();<br>
>> >> > foreach (pixel in storedImage)<br>
>> >> > if (pixel at bit channel)<br>
>> >> > return[pixel] = 1;<br>
>> >> > else<br>
>> >> > return[pixel] = 0;<br>
>> >> > return return;<br>
>> >> > }<br>
>> >> > void put(String image, vtkImageData binaryImage) {<br>
>> >> > int channel = image eq "image1" ? IMAGE1_BIT_CHANNEL :<br>
>> >> > IMAGE2_BIT_CHANNEL;<br>
>> >> > foreach (pixel in binaryImage)<br>
>> >> > if (pixel)<br>
>> >> > storedImage at bit in channel = 1;<br>
>> >> > else<br>
>> >> > storedImage at bit in channel = 0;<br>
>> >> > }<br>
>> >> > This could be extended easily for 8 channels for a char image for<br>
>> >> > instance.<br>
>> >> > This operation would have to be very fast though cause it is done<br>
>> >> > often<br>
>> >> > on<br>
>> >> > the UI thread.<br>
>> >> > 1. Is there a way to do this in VTK with Java?<br>
>> >> > 2. Is this the best scheme for accomplishing my goal?<br>
>> >> > 3. Is there a better scheme for doing this?<br>
>> >> > Thanks.<br>
>> >> > _______________________________________________<br>
>> >> > Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
>> >> ><br>
>> >> > Visit other Kitware open-source projects at<br>
>> >> > <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
>> >> ><br>
>> >> > Please keep messages on-topic and check the VTK FAQ at:<br>
>> >> > <a href="http://www.vtk.org/Wiki/VTK_FAQ" target="_blank">http://www.vtk.org/Wiki/VTK_FAQ</a><br>
>> >> ><br>
>> >> > Follow this link to subscribe/unsubscribe:<br>
>> >> > <a href="http://www.vtk.org/mailman/listinfo/vtkusers" target="_blank">http://www.vtk.org/mailman/listinfo/vtkusers</a><br>
>> >> ><br>
>> >> ><br>
>> ><br>
>> ><br>
><br>
><br>
</div></div></blockquote></div><br></div>