[Insight-users] Re: How to access pixels of itk::Image<TPixel, Dimension> (zhao)

Suzanne Vogel vogel at cs . unc . edu
Fri, 09 Aug 2002 08:19:07 -0400


Hi Zhao,

To access Pixels of Image, I declare an Image iterator and use its Get() 
method. See my copied-and-pasted code below. ;)

//===================================================================
/**
SaveImageData().
Saves the image data scaled to the range [0,255] and size of image.
*/
//===================================================================
template <typename TTextureType>
void
OrthogonalSliceTexture<TTextureType>
::SaveImageData(TImageType *image)
{
   // Get max, min image intensities.
   FilterType::Pointer filter = FilterType::New();
   filter->SetInput(image);
   filter->Update();
   m_MinIntensity = filter->GetMinimum();
   m_MaxIntensity = filter->GetMaximum();
   m_ImageToTextureGrayscaleFactor = 
255.0/(double)(m_MaxIntensity-m_MinIntensity);

   image->SetRequestedRegionToLargestPossibleRegion();
   SizeType size = (image->GetLargestPossibleRegion()).GetSize();
   m_ImageSize[0] = size[0];
   m_ImageSize[1] = size[1];
   m_ImageSize[2] = size[2];

   int i,j,k;
   m_Image = new PixelType**[size[0]];
   for (i=0; i<size[0]; i++)
   {
     m_Image[i] = new PixelType*[size[1]];
     for (j=0; j<size[1]; j++)
     {
       m_Image[i][j] = new PixelType[size[2]];
     }
   }
   //-----
   IteratorType itr(image, image->GetLargestPossibleRegion());
   IteratorType *iterator = &itr;

   // 1st & 2nd dimensions to iterate through
   iterator->SetFirstDirection(2);
   iterator->SetSecondDirection(1);

   i=0;
   for (iterator->GoToBegin(); !(iterator->IsAtEnd()); 
iterator->NextSlice())
   {
     j=0;
     for (; !(iterator->IsAtEndOfSlice()); iterator->NextLine())
     {
       k=0;
       for (; !(iterator->IsAtEndOfLine()); ++(*iterator))
       {
         m_Image[i][j][k] = iterator->Get();
         k++;
       }
       j++;
     }
     i++;
   }
}
--
Suzanne
vogel@cs.unc.edu

>    1. How to access pixels of itk::Image<TPixel, Dimension> (zhao)
> 
> --__--__--
> 
> Message: 1
> Date: Fri, 9 Aug 2002 16:24:51 +0800
> From: "zhao" <zhao_c_g@263.net>
> To: itk <insight-users@public.kitware.com>
> Subject: [Insight-users] How to access pixels of itk::Image<TPixel, Dimension>
> 
> itkHi everyone,
> 	I know one way of accessing the bits of itk::Image<...> is to use the member function GetBufferPointer(). But I think a better way is to use iterators to access the pixels. The online document suggests that function Begin() be used to return an iterator. However, Begin() is not a member of itk::Image<...>. Is there any intermediate step I need to take to solve this problem??
> 	Thanks for your reply.
> 
> Zhao 
> 
> 
> 
> 
> --__--__--
> 
> _______________________________________________
> Insight-users mailing list
> Insight-users@public.kitware.com
> http://public.kitware.com/mailman/listinfo/insight-users
> 
> 
> End of Insight-users Digest


-- 
           .|.
       \.   .|.   ./
   .\.  .\. .|. ./   ./.
     .\. ..\.|./.. ./.
  ......\(       )/......
- - - -( Suzanne )- - - -
  ....../(       )\......
     ./. ./..|..\. .\.
   ./. ./.  .|.  .\  .\.
     ./.    .|.    .\.
            .|.