[vtkusers] bit wise operations for vtkImageData

David Gobbi david.gobbi at gmail.com
Fri Mar 25 15:29:09 EDT 2011


Hi Jonathan,

If I understand correctly, you have several binary images with exactly
the same dimensions.  And you want to collapse them into a single
image, by storing a bitfield in the pixels of that image.  So:

To set bit "i" in pixel "xyz" (C++ code):
int a = int(image.GetScalarComponentAsDouble(x, y, z, 0));
a |= (1 << i);
image.SetScalarComponentAsDouble(x, y, z, a);

To clear bit "i" in pixel "xyz":
int a = int(image.GetScalarComponentAsDouble(x, y, z, 0));
a &= ~(1 << i);
image.SetScalarComponentAsDouble(x, y, z, a);

To test bit "i" in pixel "xyz":
int a = int(image.GetScalarComponentAsDouble(x, y, z, 0));
return ((a >> i) & 1);

But I'm not sure if this is what you are trying to do.  I didn't fully
understand your pseudocode.

 - David



On Fri, Mar 25, 2011 at 1:08 PM, Jonathan Morra <jonmorra at gmail.com> wrote:
> I'm in Java and storing a large number of binary vtkImageData's.  I know
> this is inefficient, and am searching for a better way to store them.  Right
> now I have a hash of the vtkImageData's and a get/put function.  Basically I
> want to mimic this get/put but with better storage.  I was thinking one way
> to do this is to use bitwise logical operations to store the information in
> the binary masks.  For instance, if we have 2 binary images, then we could
> store that information in 1 vtkImageData using the following pseudo code.
> private static final int IMAGE1_BIT_CHANNEL = 0;
> private static final int IMAGE2_BIT_CHANNEL = 1;
> private vtkImageData storedImage;
> vtkImageData get(String image)  {
>     int channel = image eq "image1" ? IMAGE1_BIT_CHANNEL :
> IMAGE2_BIT_CHANNEL;
>     vtkImageData return = new vtkImageData();
>     foreach (pixel in storedImage)
>         if (pixel at bit channel)
>             return[pixel] = 1;
>         else
>             return[pixel] = 0;
>     return return;
> }
> void put(String image, vtkImageData binaryImage) {
>     int channel = image eq "image1" ? IMAGE1_BIT_CHANNEL :
> IMAGE2_BIT_CHANNEL;
>     foreach (pixel in binaryImage)
>         if (pixel)
>             storedImage at bit in channel = 1;
>         else
>             storedImage at bit in channel = 0;
> }
> This could be extended easily for 8 channels for a char image for instance.
>  This operation would have to be very fast though cause it is done often on
> the UI thread.
> 1.  Is there a way to do this in VTK with Java?
> 2.  Is this the best scheme for accomplishing my goal?
> 3.  Is there a better scheme for doing this?
> Thanks.
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>
>



More information about the vtkusers mailing list