[Insight-users] ImageRandomNonRepeatingConstIteratorWithIndex Bug ?

Emma Saunders emmasaunders123 at gmail.com
Fri Dec 6 13:30:39 EST 2013


My Mistake not all voxels are visited in the for loop here, using  while(!
imageIterator.IsAtEnd())

does the job


On Fri, Dec 6, 2013 at 4:04 PM, Emma Saunders <emmasaunders123 at gmail.com>wrote:

> Hi all,
>
> I am using the  ImageRandomNonRepeatingConstIteratorWithIndex to randomly
> sample images without repeat.  When using the number of samples as the size
> of the image however I don't get the full image back.
>
> I am using the example in examples/data/brainweb1e1a10f20.mha.  Below is
> my code, attached is my result,
>
> Any ideas?
>
>
> #include "itkImageFileWriter.h"
> #include "itkImageFileReader.h"
> #include "itkImageFileWriter.h"
> #include "itkImage.h"
> #include "itkImageFileReader.h"
> #include "itkImageRandomConstIteratorWithIndex.h"
> #include "itkImageRandomNonRepeatingConstIteratorWithIndex.h"
>
> int main(int argc, char*argv[])
> {
>   const    unsigned int    Dimension = 3;
>   typedef unsigned char PixelType;
>   typedef itk::Image<PixelType, Dimension>  ImageType;
>
>   typedef itk::ImageFileReader< ImageType  > ImageReaderType;
>   typedef itk::ImageFileWriter< ImageType  > ImageWriterType;
>
>   ImageReaderType::Pointer ImageReader   = ImageReaderType::New();
>   ImageWriterType::Pointer ImageWriter =  ImageWriterType::New();
>
>   ImageReader->SetFileName( argv[1] );
>   ImageWriter->SetFileName( argv[2] );
>
>
>     ImageType::Pointer Image = ImageReader->GetOutput();
>     Image->Update();
>
> ImageType::RegionType region=Image->GetLargestPossibleRegion();
>  ImageType::SizeType size=region.GetSize();
> ImageType::IndexType index=region.GetIndex();
>     ImageType::IndexType start = index;
>     ImageType::SpacingType   spacing   = Image->GetSpacing();
>     ImageType::DirectionType direction = Image->GetDirection();
>
>
>     ImageType::RegionType newregion;
>     newregion.SetSize(size);
>     newregion.SetIndex(start);
>     std::cout << "The size of the image is " << size <<std::endl;
>
> //Define Output Image
>
>   ImageType::Pointer output = ImageType::New();
>   output->SetRegions(newregion);
>   output->SetSpacing(spacing);
>   output->SetDirection(direction);
>   output->Allocate();
>   output->FillBuffer(0);
>   output->Update();
>
>  typedef itk::ImageRandomNonRepeatingConstIteratorWithIndex<
>                                           ImageType > ConstIteratorType;
>
>
>   ConstIteratorType inputIt(  Image,  region );
>
>    unsigned int number = 7069860;
>   inputIt.SetNumberOfSamples( number ); //Number of samples total is
> 3281040
>   std::cout << "The number of samples is " << number << std::endl;
>   inputIt.ReinitializeSeed();
>   inputIt.GoToBegin();
>
>
>   //while(!inputIt.IsAtEnd())
>   for ( inputIt.GoToBegin(); ! inputIt.IsAtEnd(); ++inputIt)
>     {
>
>     //std::cout <<inputIt.GetIndex()<< " " ;
>     //std::cout << inputIt.Get() << std::endl;
>     output->SetPixel(inputIt.GetIndex(), inputIt.Get());
>
>     ++inputIt;
>     }
>
>   output->Update();
>   ImageWriter->SetInput( output );
>   ImageWriter->Update();
>
>
>
>   return EXIT_SUCCESS;
> }
>
>
> Thanks Emma
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20131206/11db9722/attachment.htm>


More information about the Insight-users mailing list