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