[Insight-users] Getting unknown amount random Pixels

Luis Ibanez luis.ibanez at kitware.com
Sun Sep 12 17:19:45 EDT 2004



Hi Neilson,


You can easily overcome this difficulty by taking the code of the
Iterator, renaming the class and adding to it the functionality
you need for evaluating the threshold.


Please do the following:


1) Take the files


    Insight/Code/Common

       itkImageRandomConstIteratorWithIndex.h
       itkImageRandomConstIteratorWithIndex.txx

    and rename them as

       itkImageRandomConditionalConstIteratorWithIndex.h
       itkImageRandomConditionalConstIteratorWithIndex.txx       





2) Edit both new files and replace all occurrences of the string

       ImageRandomConstIteratorWithIndex

with

       ImageRandomConditionalConstIteratorWithIndex





3) At the end of the header file (extension .h) just below the line

         unsigned long  m_NumberOfPixelsInRegion;

     add a new member variable

         IndexType   m_Threshold;


4)   In the current  method  Operator++()

  Self & operator++()
  {
    this->RandomJump();
    m_NumberOfSamplesDone++;
    return *this;
  }

add the following the condition for checking the
pixel value against the threshold

  Self & operator++()
  {
    do {
       this->RandomJump();
       }  while( this->Get() < m_Threshold );
     m_NumberOfSamplesDone++;
     return *this;
  }



  With this change, every time to increment the iterator, it will
  keep picking random samples until it finds one with value
  above the threshold that you set.

   NOTE This may take significant time if the pixels with values
   over the threshold are not very abundant in your image.



5) Add the Set/Get methods for the Threshold by
   inserting lines


      itkSetMacro( Threshold, PixelType );
      itkGetMacro( Threshold, PixelType );
 



   
6) Then in your code instantiate this new Iterator


  typedef ImageRandomConditionalConstIreratorWithIndex<ImageType> 
RandomIterator;

  RandomIterator randIter( M_FixedImage, this->GetFixedImageRegion());

  randIter.SetNumberOfSamples( TheNumberOfSamplesThatIReallyWantToGet );

  randIter.SetThreshold( ThisIsMyThresholdForSelectingPixels );

  randIter.GoToBegin();
   

  // this is just in case you want to put them in an array....  
  std::vector<PixelType> myRandomPixelValuesOverThreshold;

  do {
     ++randIter;
     myRandomPixelValuesOverThreshold.push_back( randIter.Get() );
   } while( !randIter.IsAtEnd() );   



 // At this point, the STL::Vector container has a list of 
 // grayscale values selected from randomly distributed
 // pixels, all of which have values over the threshold.




You should read the Chapter on Image
Iterators from the ITK Software Guide.


    http://www.itk.org/ItkSoftwareGuide.pdf






Regards,



     Luis


 ----------------------------

> ------------------------------------------------------------------------
>
>Hi,
>
>	I am currently trying to get a set of N number of Pixels in a image in 
>random locations over a set threshold.  I was attempting to use 
>ImageRandomConstIteratorWithIndex but have come to realize that you 
>need to set the number of samples you will be using before starting the 
>iterator.  My problems is I don't know how many pixels I need to sample 
>before getting N pixels over the threshold.
>
>Here is my code:
>
>	typedef ImageRandomConstIreratorWithIndex<ImageType> RandomIterator;
>	RandomIterator randIter( M_FixedImage, this->GetFixedImageRegion())
>
>	randIter.SetNumberOfSamples( m_NumberOfSpatialSamples );
>	randIter.GoToBegin();
>
>	RandomIterator ::PixelType pixelValue;
>	
>	for(int i = 0; i< N; i++)
>	{
>		pixelValue = randIter.Get();
>		
>		while(PixelValue == 0)
>		{
>			++randIter;
>			pixelValue = randIter.Get();
>		}
>		printf("pixel value = %e \n", pixelValue);
>
>		container[i] = pixelValue;
>	}
>
>	The stuff that is printed to the screen is something like 8.61e-304
>
>Even when I simply try to use "get" some strange number comes out.  Is 
>it because get doesn't return a float or double but a object of some 
>kind of container?
>
>Neilson
>
>  
>
> ------------------------------------------------------------------------






More information about the Insight-users mailing list