[Insight-users] how to get the buffer data
Dan Mueller
dan.muel at gmail.com
Wed Dec 29 10:18:47 EST 2010
Hi Ali,
A pixel may consist of more than one component or channel.
Example 1: unsigned char = 1 component
Example 2: RGBPixel<signed short> = 3 components (1 Red, 1 Green, 1 Blue)
bitsPerComponent is the number of bits per component
Example 1: unsigned char = 8 bits per component
Example 2 RGBPixel<signed short> = 16 bits per component
bitsPerPixel is the total number of bits per pixel (including all components)
Example 1: unsigned char = 8 bits per pixel
Example 2: RGBPixel<signed short> = 16 * 3 = 48 bits per pixel
You can find out the number of bits per component using the
sizeof(component_type) operation.
Example 1: bitsPerComponent = sizeof(unsigned char)
Example 2: bitsPerComponent = sizeof(signed short)
HTH
Cheers, Dan
On 29 December 2010 14:40, Ali Habib <ali.mahmoud.habib at gmail.com> wrote:
> and how to get bytes per pixel
> Best regards
>
> On Wed, Dec 29, 2010 at 4:58 PM, Ali Habib <ali.mahmoud.habib at gmail.com>
> wrote:
>>
>> I got it ,
>> but may I ask a question what's the difference between bitsPerComponent,
>> and
>> bitsPerPixel
>> I just get confused
>> Best regards
>>
>> On Wed, Dec 29, 2010 at 4:16 PM, Dan Mueller <dan.muel at gmail.com> wrote:
>>>
>>> Hi Ali,
>>>
>>> Option 1:
>>> ImageType::Pointer image_1 = reader->GetOutput();
>>> unsigned int numComponents = image_1->GetNumberOfComponentsPerPixel();
>>> // NOTE: Only really works for itk::Vector images eg. RGBPixel returns 1
>>>
>>> Option 2:
>>> ImageType::Pointer image_1 = reader->GetOutput();
>>> InputImageType::IndexType index;
>>> index[0]=1;
>>> index[1]=1;
>>> ImageType::PixelType pixel_value= image_1->GetPixel( index );
>>> unsigned int numComponents =
>>>
>>> itk::VectorPixelTraits<ImageType::PixelType>::GetNumberOfComponents(pixel_value);
>>> // NOTE: This works for many image types. eg.
>>> // unsigned char returns 1
>>> // RGB returns 3
>>> // FixedArray<float, 4> returns 4
>>>
>>> HTH
>>>
>>> Cheers, Dan
>>>
>>> On 29 December 2010 11:17, Ali Habib <ali.mahmoud.habib at gmail.com> wrote:
>>> > Sorry for disturbance,
>>> > but I have
>>> > ImageType::Pointer image_1 = reader->GetOutput() ;
>>> > I misunderstand how to get the pixel type of image_1 , using the 2
>>> > options
>>> > you gave me . could u give me more details
>>> > Best regards
>>> >
>>> > On Tue, Dec 28, 2010 at 11:26 AM, Dan Mueller <dan.muel at gmail.com>
>>> > wrote:
>>> >>
>>> >> Hi Ali,
>>> >>
>>> >> Try the following to get the image buffer as a pointer:
>>> >> InputPixelType* buffer = image_1->GetBufferPointer();
>>> >>
>>> >> You can then treat the buffer as a simple pointer (e.g. cast to
>>> >> unsigned char *, whatever).
>>> >>
>>> >> Determining if the pixel type is scalar or not is a little trickier.
>>> >> At first glance the method
>>> >> itk::ImageBase::GetNumberOfComponentsPerPixel() seems helpful, but it
>>> >> does not work as you might expect (see documentation). I'm not sure if
>>> >> there is a better way, but I use a specialized traits class to help
>>> >> (see below). You use the class as follows:
>>> >> InputPixelType pixel;
>>> >> unsigned int numComponents =
>>> >> itk::VectorPixelTraits<InputPixelType>::GetNumberOfComponents(pixel);
>>> >>
>>> >> HTH
>>> >>
>>> >> Cheers, Dan
>>> >>
>>> >>
>>> >>
>>> >> /*=========================================================================
>>> >>
>>> >> Program: Insight Segmentation & Registration Toolkit
>>> >> Module: $RCSfile: itkVectorPixelTraits.h,v $
>>> >> Language: C++
>>> >> Date: $Date$
>>> >> Version: $Revision$
>>> >>
>>> >> Copyright (c) Insight Software Consortium. All rights reserved.
>>> >> See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for
>>> >> details.
>>> >>
>>> >> This software is distributed WITHOUT ANY WARRANTY; without even
>>> >> the implied warranty of MERCHANTABILITY or FITNESS FOR A
>>> >> PARTICULAR
>>> >> PURPOSE. See the above copyright notices for more information.
>>> >>
>>> >>
>>> >>
>>> >> =========================================================================*/
>>> >> #ifndef __itkVectorPixelTraits_h
>>> >> #define __itkVectorPixelTraits_h
>>> >>
>>> >> #include "itkMacro.h"
>>> >> #include "itkArray.h"
>>> >> #include "itkVector.h"
>>> >> #include "itkFixedArray.h"
>>> >> #include "itkRGBPixel.h"
>>> >> #include "itkRGBAPixel.h"
>>> >> #include "itkVariableLengthVector.h"
>>> >> #include "itkNumericTraits.h"
>>> >>
>>> >> namespace itk
>>> >> {
>>> >>
>>> >> /** \class VectorPixelTraits
>>> >> * \brief Define additional traits for vector pixels (eg. fixed array,
>>> >> RGB).
>>> >> *
>>> >> * VectorPixelTraits is used to extend the traits associated with
>>> >> vector
>>> >> pixel
>>> >> * types such as fixed array, RGBPixel, VariableLengthVector, and so
>>> >> on.
>>> >> * These traits also support scalar types (such as unsigned char,
>>> >> short)
>>> >> * so that both vector and scalar types can be treated in a unified
>>> >> way.
>>> >> *
>>> >> * Note this default implementation defines traits for all scalar
>>> >> types
>>> >> * (eg. unsigned char, short, double, etc).
>>> >> *
>>> >> * \ingroup DataRepresentation
>>> >> */
>>> >> template <class T>
>>> >> class VectorPixelTraits {
>>> >> public:
>>> >> /** Get the number of components for the given type.
>>> >> * An input is required for variable length types whose
>>> >> * length is defined at run-time rather than compile-time.
>>> >> * For other types the input is simply ignored. */
>>> >> static unsigned int GetNumberOfComponents( T input ) { return 1; }
>>> >>
>>> >> /* Return the reference to the given indexed element in the vector.
>>> >> */
>>> >> static T *GetElement( unsigned int i, T *pixel ) { return pixel; }
>>> >>
>>> >> /* Set the value of the left element with the value of the right. */
>>> >> static void SetElement( unsigned int i, T &left, T right ) { left =
>>> >> right; }
>>> >>
>>> >> /* Return the zero value. */
>>> >> static T ZeroValue( T input ) { return NumericTraits<T>::ZeroValue();
>>> >> }
>>> >>
>>> >> /* Convert the given double to the pixel type.
>>> >> NOTE: This is not implemented for VariableLengthVectors! */
>>> >> static T Convert( double input ) { return static_cast<T>(input); }
>>> >> };
>>> >>
>>> >> /** Define a macro to be used for RGB types. */
>>> >> #define RGBSTATICTRAITSMACRO( T, E ) \
>>> >> template<> \
>>> >> class VectorPixelTraits< T<E> > { \
>>> >> public: \
>>> >> static unsigned int GetNumberOfComponents( T<E> input ) \
>>> >> { return 3; } \
>>> >> static E *GetElement( unsigned int i, T<E> *pixel ) \
>>> >> { return &((*pixel)[i]); } \
>>> >> static void SetElement( unsigned int i, T<E> &left, T<E> right ) \
>>> >> { left[i] = right[i]; } \
>>> >> static T<E> ZeroValue( T<E> input ) \
>>> >> { return NumericTraits<T<E>>::ZeroValue(); } \
>>> >> static T<E> Convert( double input ) \
>>> >> { T<E> value( static_cast<E>(input) ); return value; } \
>>> >> }; \
>>> >>
>>> >> RGBSTATICTRAITSMACRO( RGBPixel, unsigned char );
>>> >> RGBSTATICTRAITSMACRO( RGBPixel, signed char );
>>> >> RGBSTATICTRAITSMACRO( RGBPixel, unsigned short );
>>> >> RGBSTATICTRAITSMACRO( RGBPixel, signed short );
>>> >> RGBSTATICTRAITSMACRO( RGBPixel, unsigned long );
>>> >> RGBSTATICTRAITSMACRO( RGBPixel, signed long );
>>> >> RGBSTATICTRAITSMACRO( RGBPixel, float );
>>> >> RGBSTATICTRAITSMACRO( RGBPixel, double );
>>> >>
>>> >> /** Define a macro to be used for RGBA types. */
>>> >> #define RGBASTATICTRAITSMACRO( T, E ) \
>>> >> template<> \
>>> >> class VectorPixelTraits< T<E> > { \
>>> >> public: \
>>> >> static unsigned int GetNumberOfComponents( T<E> input ) { return 4; }
>>> >> \
>>> >> static E *GetElement( unsigned int i, T<E> *pixel ) { return
>>> >> &((*pixel)[i]); } \
>>> >> static void SetElement( unsigned int i, T<E> &left, T<E> right ) {
>>> >> left[i] = right[i]; } \
>>> >> static T<E> ZeroValue( T<E> input ) { T<E> zero =
>>> >> NumericTraits<T<E>>::ZeroValue(); zero.SetAlpha( 254 ); return zero; }
>>> >> \
>>> >> static T<E> Convert( double input ) { T<E> value(
>>> >> static_cast<E>(input) ); return value; } \
>>> >> }; \
>>> >>
>>> >> RGBASTATICTRAITSMACRO( RGBAPixel, unsigned char );
>>> >> RGBASTATICTRAITSMACRO( RGBAPixel, signed char );
>>> >> RGBASTATICTRAITSMACRO( RGBAPixel, unsigned short );
>>> >> RGBASTATICTRAITSMACRO( RGBAPixel, signed short );
>>> >> RGBASTATICTRAITSMACRO( RGBAPixel, unsigned long );
>>> >> RGBASTATICTRAITSMACRO( RGBAPixel, signed long );
>>> >> RGBASTATICTRAITSMACRO( RGBAPixel, float );
>>> >> RGBASTATICTRAITSMACRO( RGBAPixel, double );
>>> >>
>>> >> /** Define a macro to be used for all (fixed) array types. */
>>> >> #define FIXEDARRAYSTATICTRAITSMACRO( T, E, N ) \
>>> >> template<> \
>>> >> class VectorPixelTraits< T<E,N> > { \
>>> >> public: \
>>> >> static unsigned int GetNumberOfComponents(T<E,N> input) { return N; }
>>> >> \
>>> >> static E *GetElement( unsigned int i, T<E,N> *pixel) { return
>>> >> &((*pixel)[i]); } \
>>> >> static void SetElement( unsigned int i, T<E,N> &left, T<E,N> right)
>>> >> { left[i] = right[i]; } \
>>> >> static T<E,N> ZeroValue( T<E,N> input ) { T<E,N> zero; zero.Fill(
>>> >> NumericTraits<E>::ZeroValue() ); return zero; } \
>>> >> static T<E,N> Convert( double input ) { T<E,N> value; value.Fill(
>>> >> static_cast<E>(input) ); return value; } \
>>> >> }; \
>>> >>
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, unsigned char, 1 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, signed char, 1 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, unsigned short, 1 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, signed short, 1 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, unsigned long, 1 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, signed long, 1 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, float, 1 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, double, 1 );
>>> >>
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, unsigned char, 2 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, signed char, 2 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, unsigned short, 2 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, signed short, 2 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, unsigned long, 2 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, signed long, 2 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, float, 2 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, double, 2 );
>>> >>
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, unsigned char, 3 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, signed char, 3 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, unsigned short, 3 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, signed short, 3 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, unsigned long, 3 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, signed long, 3 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, float, 3 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, double, 3 );
>>> >>
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, unsigned char, 4 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, signed char, 4 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, unsigned short, 4 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, signed short, 4 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, unsigned long, 4 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, signed long, 4 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, float, 4 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( FixedArray, double, 4 );
>>> >>
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, unsigned char, 1 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, signed char, 1 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, unsigned short, 1 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, signed short, 1 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, unsigned long, 1 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, signed long, 1 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, float, 1 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, double, 1 );
>>> >>
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, unsigned char, 2 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, signed char, 2 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, unsigned short, 2 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, signed short, 2 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, unsigned long, 2 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, signed long, 2 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, float, 2 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, double, 2 );
>>> >>
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, unsigned char, 3 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, signed char, 3 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, unsigned short, 3 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, signed short, 3 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, unsigned long, 3 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, signed long, 3 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, float, 3 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, double, 3 );
>>> >>
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, unsigned char, 4 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, signed char, 4 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, unsigned short, 4 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, signed short, 4 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, unsigned long, 4 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, signed long, 4 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, float, 4 );
>>> >> FIXEDARRAYSTATICTRAITSMACRO( Vector, double, 4 );
>>> >>
>>> >> /** Define a macro to be used for all (variable) array types. */
>>> >> #define VARIABLEARRAYSTATICTRAITSMACRO( T, E ) \
>>> >> template<> \
>>> >> class VectorPixelTraits< T<E> > { \
>>> >> public: \
>>> >> static unsigned int GetNumberOfComponents(T<E> input) { return
>>> >> input.Size(); } \
>>> >> static E *GetElement( unsigned int i, T<E> *pixel) { return
>>> >> &((*pixel)[i]); } \
>>> >> static void SetElement( unsigned int i, T<E> &left, T<E> right) {
>>> >> left[i] = right[i]; } \
>>> >> static T<E> ZeroValue( T<E> input ) { return
>>> >> NumericTraits<T<E>>::Zero(input); } \
>>> >> static T<E> Convert( double input ) { T<E> value; /* NOT IMPLEMENTED
>>> >> */ return value; } \
>>> >> }; \
>>> >>
>>> >> VARIABLEARRAYSTATICTRAITSMACRO( VariableLengthVector, unsigned char );
>>> >> VARIABLEARRAYSTATICTRAITSMACRO( VariableLengthVector, signed char );
>>> >> VARIABLEARRAYSTATICTRAITSMACRO( VariableLengthVector, unsigned short
>>> >> );
>>> >> VARIABLEARRAYSTATICTRAITSMACRO( VariableLengthVector, signed short );
>>> >> VARIABLEARRAYSTATICTRAITSMACRO( VariableLengthVector, unsigned long );
>>> >> VARIABLEARRAYSTATICTRAITSMACRO( VariableLengthVector, signed long );
>>> >> VARIABLEARRAYSTATICTRAITSMACRO( VariableLengthVector, float );
>>> >> VARIABLEARRAYSTATICTRAITSMACRO( VariableLengthVector, double );
>>> >>
>>> >> } // end namespace itk
>>> >>
>>> >> #endif
>>> >>
>>> >>
>>> >> On 28 December 2010 09:52, Ali Habib <ali.mahmoud.habib at gmail.com>
>>> >> wrote:
>>> >> > Dear All,
>>> >> > I need the pixel data as unsigned char* , I created my pipeline but
>>> >> > I
>>> >> > stuck
>>> >> > on how to get the buffer data , also I need to know does the
>>> >> > pixel_value
>>> >> > will show me if the input is scaler or not
>>> >> > the code I use is :
>>> >> > typedef signed short InputPixelType;
>>> >> > const unsigned int InputDimension = 2;
>>> >> > typedef itk::Image< InputPixelType, InputDimension > InputImageType;
>>> >> > typedef itk::ImageFileReader< InputImageType > ReaderType;
>>> >> > typedef InputImageType::SizeType SizeType;
>>> >> > ReaderType::Pointer reader = ReaderType::New();
>>> >> > reader->SetFileName( "4.dcm" );
>>> >> > typedef itk::GDCMImageIO ImageIOType;
>>> >> > ImageIOType::Pointer gdcmImageIO = ImageIOType::New();
>>> >> > reader->SetImageIO( gdcmImageIO );
>>> >> > try
>>> >> > {
>>> >> > reader->Update();
>>> >> > }
>>> >> > catch (itk::ExceptionObject & e)
>>> >> > {
>>> >> > std::cerr << "exception in file reader " << std::endl;
>>> >> > std::cerr << e << std::endl;
>>> >> > // return EXIT_FAILURE;
>>> >> > }
>>> >> > InputImageType::Pointer image_1 = reader->GetOutput() ;
>>> >> > // the image parameters
>>> >> > unsigned long lwidth =
>>> >> > image_1->GetLargestPossibleRegion().GetSize()[0]
>>> >> > ;
>>> >> > unsigned long lheight =
>>> >> > image_1->GetLargestPossibleRegion().GetSize()[1]
>>> >> > ;
>>> >> > InputImageType::IndexType index;
>>> >> > index[0]=1;
>>> >> > index[1]=1;
>>> >> > signed short pixel_value;
>>> >> > pixel_value= image_1->GetPixel( index );
>>> >
>>> >
>>
>
>
More information about the Insight-users
mailing list