[Insight-users] Import a boolean image from an array

Dan Mueller dan.muel at gmail.com
Thu Nov 13 12:15:00 EST 2008


Hi Insight Users,

I am struggling to understand how to import a data array into an
itk:Image< bool, 2 >.

I get unexpected results when I use an array of something other than
bool[], for example unsigned char[]. As far as I understand unsigned
char is 8-bits (although doing some googling says that is a /minimum/
of 8-bits, whatever that means). If unsigned char = 8-bits, I would
expect that creating an unsigned char[], setting some values, casting
to bool* and importing should operate the same as using a bool[]. But
the code below does not produce the same result (see attached).

Why not? Is this because of my compiler (Visual Studio 2005)? My
machine (Windows XP 64-bit)? Or some logical error I can't spot?

Thanks for any advice.

Regards, Dan

=== CMakeLists.txt ===
PROJECT(BoolImageTest)
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)

FIND_PACKAGE(ITK REQUIRED)
INCLUDE(${ITK_USE_FILE})

ADD_EXECUTABLE(BoolImageTest main.cxx)
TARGET_LINK_LIBRARIES(BoolImageTest ITKCommon ITKIO)

=== main.cxx ===

#define USE_BOOL_ARRAY 0

#include "itkImage.h"
#include "itkImportImageFilter.h"
#include "itkCastImageFilter.h"
#include "itkImageFileWriter.h"

int main( int argc, char* argv[] )
{
  typedef bool BoolPixelType;
  typedef unsigned char UnsignedCharPixelType;
  const unsigned int Dimension = 2;

  typedef itk::Image< BoolPixelType, Dimension > BoolImageType;
  typedef itk::Image< UnsignedCharPixelType, Dimension > OutputImageType;
  typedef itk::ImportImageFilter< BoolPixelType, Dimension > ImportType;

  BoolImageType::SizeType size;
  size[0] = 16;
  size[1] = 1;
  BoolImageType::RegionType region( size );
  BoolImageType::SpacingType spacing;
  spacing.Fill( 1.0 );
  BoolImageType::PointType origin;
  origin.Fill( 1.0 );

#if USE_BOOL_ARRAY
  bool data[16];
  for (unsigned int i=0; i<8; i++)
    data[i] = true;
  for (unsigned int i=8; i<16; i++)
    data[i] = false;
  void* ptr = (void*)&data;
#else
  unsigned char data[2];
  data[0] = 0xF;
  data[1] = 0x0;
  void* ptr = (void*)&data;
#endif

  ImportType::Pointer import = ImportType::New();
  import->SetRegion( region );
  import->SetSpacing( spacing );
  import->SetOrigin( origin );
  import->SetImportPointer( (bool*)ptr, size[0]*size[1], false );
  import->Update();

  typedef itk::CastImageFilter< BoolImageType, OutputImageType > CastType;
  CastType::Pointer cast = CastType::New();
  cast->SetInput( import->GetOutput() );
  cast->Update();

  typedef itk::ImageFileWriter< OutputImageType > WriterType;
  WriterType::Pointer writer = WriterType::New( );
  writer->SetInput( cast->GetOutput() );
  writer->SetFileName( "D:/Temp/BoolTestOutput.mhd" );
  writer->Update();

  return EXIT_SUCCESS;
}

=== Machine ===
ITK 3.10
CMake 2.6.0
Visual Studio 8.0.50727.762
Windows XP SP2 X64
-------------- next part --------------
A non-text attachment was scrubbed...
Name: BoolTestOutput-bool.mha
Type: application/octet-stream
Size: 338 bytes
Desc: not available
URL: <http://www.itk.org/pipermail/insight-users/attachments/20081113/f9e4f5e8/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: BoolTestOutput-char.mha
Type: application/octet-stream
Size: 338 bytes
Desc: not available
URL: <http://www.itk.org/pipermail/insight-users/attachments/20081113/f9e4f5e8/attachment-0001.obj>


More information about the Insight-users mailing list