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

Dan Mueller dan.muel at gmail.com
Thu Nov 13 14:19:49 EST 2008


Hi Frank,

I was thinking about this problem on the train home and thought as
much. Guess I can't import my true 1-bit data array without
duplicating as 8-bit bool array...

Thanks for confirming this.

Cheers, Dan

2008/11/13  <frankmiller at jhmi.edu>:
> Dan,
>
> A variable of type bool probably has a size of 8-bits on your system.
> You can do a
>
>  std::cout << sizeof( bool )          << std::endl;
>  std::cout << sizeof( unsigned char ) << std::endl;
>
> to see. The sizeof operator returns the size in bytes.
>
> The reason that a bool is not 1-bit is because you can only get the
> address of a variable at byte resolution.
>
> Thus an array of 16 bools is larger than an array of 2 chars. You are
> experiencing a buffer overrun when USE_BOOL_ARRAY is defined as 0. You
> have to be very careful when you do low-level stuff like this. sizeof()
> is you friend.
>
> Hope this helps.
>
> Frank
>
> On Thu, Nov 13, 2008 at 06:15:00PM +0100, Dan Mueller wrote:
>> 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
>
>
>
>> _______________________________________________
>> Insight-users mailing list
>> Insight-users at itk.org
>> http://www.itk.org/mailman/listinfo/insight-users
>
>


More information about the Insight-users mailing list