| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkImageRandomNonRepeatingConstIteratorWithIndex.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:39 $ |
| 7 |
|
Version: $Revision: 1.4 $ |
| 8 |
|
|
| 9 |
|
Copyright (c) Insight Software Consortium. All rights reserved. |
| 10 |
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. |
| 11 |
|
|
| 12 |
|
This software is distributed WITHOUT ANY WARRANTY; without even |
| 13 |
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 |
|
PURPOSE. See the above copyright notices for more information. |
| 15 |
|
|
| 16 |
|
=========================================================================*/ |
| 17 |
|
#ifndef __itkImageRandomNonRepeatingConstIteratorWithIndex_h |
| 18 |
|
#define __itkImageRandomNonRepeatingConstIteratorWithIndex_h |
| 19 |
|
|
| 20 |
|
#include "itkImageConstIteratorWithIndex.h" |
| 21 |
|
#include "itkImage.h" |
| 22 |
|
#include <algorithm> |
| 23 |
|
#include <iostream> |
| 24 |
|
#include "itkMersenneTwisterRandomVariateGenerator.h" |
| 25 |
|
|
| 26 |
|
|
| 27 |
|
namespace itk |
| 28 |
|
{ |
| 29 |
|
|
| 30 |
|
|
| 31 |
IND |
**/* |
| 32 |
IND |
*****The itk::ImageRandomNonRepeatingIterator works by creating a random |
| 33 |
IND |
*****permutation of the image pixels and then using that to control the |
| 34 |
IND |
*****order in which it accesses them. The classes nodeOfPermutation and |
| 35 |
IND |
*****randomPermutation are used to support that. randomPermutation is |
| 36 |
IND |
*****basically container which holds nodeOfPermutation objects. The |
| 37 |
IND |
*****node class overloads the < operator, which allows the sort algorithm |
| 38 |
|
from the STL to be used on it. |
| 39 |
|
*/ |
| 40 |
|
class nodeOfPermutation |
| 41 |
IND |
**{ |
| 42 |
IND |
***public: |
| 43 |
IVP,IVR |
unsigned long priority; |
| 44 |
IVP,IVR |
unsigned long index; |
| 45 |
IVP,IVR |
double value; |
| 46 |
|
nodeOfPermutation () |
| 47 |
|
{ |
| 48 |
|
priority=0; |
| 49 |
|
index=0; |
| 50 |
|
value=0.0; |
| 51 |
|
} |
| 52 |
|
bool operator<( const nodeOfPermutation& b) const |
| 53 |
|
{ |
| 54 |
|
if(priority==b.priority) |
| 55 |
|
{ |
| 56 |
|
return value < b.value; |
| 57 |
|
} |
| 58 |
|
else |
| 59 |
|
{ |
| 60 |
|
return priority<b.priority; |
| 61 |
|
} |
| 62 |
|
} |
| 63 |
IND |
**}; |
| 64 |
|
class randomPermutation |
| 65 |
IND |
**{ |
| 66 |
IND |
***public: |
| 67 |
|
nodeOfPermutation * permutation; |
| 68 |
|
Statistics::MersenneTwisterRandomVariateGenerator::Pointer m_Generator; |
| 69 |
|
unsigned long size; |
| 70 |
|
randomPermutation(unsigned long sz) |
| 71 |
|
{ |
| 72 |
|
size=sz; |
| 73 |
|
permutation=new nodeOfPermutation[size]; |
| 74 |
|
m_Generator = Statistics::MersenneTwisterRandomVariateGenerator::New(); |
| 75 |
|
this->Shuffle(); |
| 76 |
|
} |
| 77 |
IND |
****void Dump() |
| 78 |
|
{ |
| 79 |
IND |
******for(unsigned int i=0;i<size;i++) |
| 80 |
|
{ |
| 81 |
|
std::cout<<permutation[i].value<<" "<<permutation[i].priority |
| 82 |
|
<<" "<<permutation[i].index<<";"; |
| 83 |
|
std::cout<<std::endl; |
| 84 |
|
} |
| 85 |
|
} |
| 86 |
|
void SetPriority(unsigned long i,unsigned long priority) |
| 87 |
|
{ |
| 88 |
|
if(i>size) |
| 89 |
IND |
********{ |
| 90 |
IND |
********std::cerr<<"Error - i dont have "<<i<<" elements"<<std::endl; |
| 91 |
|
} |
| 92 |
|
else |
| 93 |
|
{ |
| 94 |
|
permutation[i].priority=priority; |
| 95 |
|
} |
| 96 |
|
} |
| 97 |
|
void Shuffle() |
| 98 |
|
{ |
| 99 |
|
for(unsigned int i=0;i<size;i++) |
| 100 |
|
{ |
| 101 |
|
permutation[i].value= m_Generator->GetVariateWithClosedRange ( 1.0 ); |
| 102 |
IND |
********permutation[i].index=i; |
| 103 |
IND |
********} |
| 104 |
IND |
******std::sort(permutation,permutation+size); |
| 105 |
|
} |
| 106 |
IND |
****unsigned long operator[](unsigned long i) |
| 107 |
|
{ |
| 108 |
IND |
******return permutation[i].index; |
| 109 |
|
} |
| 110 |
IND |
****~randomPermutation() |
| 111 |
|
{ |
| 112 |
IND |
******delete [] permutation; |
| 113 |
|
} |
| 114 |
|
|
| 115 |
IND |
****/** Reinitialize the seed of the random number generator */ |
| 116 |
IND |
****void ReinitializeSeed() |
| 117 |
|
{ |
| 118 |
IND |
******m_Generator->Initialize(); |
| 119 |
|
} |
| 120 |
|
|
| 121 |
IND |
****void ReinitializeSeed(int seed) |
| 122 |
|
{ |
| 123 |
IND |
******m_Generator->Initialize ( seed ); |
| 124 |
|
} |
| 125 |
IND |
**}; |
| 126 |
|
|
| 127 |
|
|
| 128 |
IND |
**/** \class ImageRandomNonRepeatingConstIteratorWithIndex |
| 129 |
LEN,IND |
**** \brief A multi-dimensional image iterator that visits a random set of pixels |
| 130 |
IND |
**** within an image region. All pixels in the image will be visited before any |
| 131 |
IND |
**** are repeated. A priority image may be passed to the interator which |
| 132 |
IND |
**** will cause it to select certain sets of pixels (those with lower priority |
| 133 |
|
* values) before others. |
| 134 |
IND |
**** |
| 135 |
IND |
**** This class was contributed by Rupert Brooks, McGill Centre for Intelligent |
| 136 |
IND |
**** Machines, Montreal, Canada. It is heavily based on the |
| 137 |
IND |
**** ImageRandomIterator class. |
| 138 |
IND |
**** |
| 139 |
IND |
**** ImageRandomNonRepeatingConstIteratorWithIndex is a multi-dimensional |
| 140 |
IND |
**** iterator class that |
| 141 |
LEN,IND |
**** is templated over image type. ImageRandomNonRepeatingConstIteratorWithIndex |
| 142 |
IND |
**** is constrained to walk only within the specified region. When first |
| 143 |
IND |
**** instantiated, it creates (and stores) a random permutation of the image |
| 144 |
IND |
**** pixels. It then visits each pixel in the order specified by the |
| 145 |
IND |
**** permutation. Thus, iterator++ followed by iterator-- will end up leaving |
| 146 |
IND |
**** the iterator pointing at the same pixel. Furthermore, iterating from |
| 147 |
IND |
**** beginning to end will cover each pixel in the region exactly once. |
| 148 |
IND |
**** |
| 149 |
IND |
**** This iterator can be passed an image the same size as the region, which |
| 150 |
IND |
**** specifies a priority for the pixels. Within areas of this priority image |
| 151 |
IND |
**** that have the same value, the pixel selection will be random. Otherwise |
| 152 |
IND |
**** the pixel selection will be in the order of the priority image. In the |
| 153 |
IND |
**** extreme, this allows the order of the pixel selection to be completely |
| 154 |
IND |
**** specified. |
| 155 |
IND |
**** |
| 156 |
IND |
**** ImageRandomNonRepeatingConstIteratorWithIndex assumes a particular layout |
| 157 |
IND |
**** of the image data. The is arranged in a 1D array as if it were |
| 158 |
IND |
**** [][][][slice][row][col] with |
| 159 |
IND |
**** Index[0] = col, Index[1] = row, Index[2] = slice, etc. |
| 160 |
IND |
**** |
| 161 |
IND |
**** |
| 162 |
IND |
**** \par MORE INFORMATION |
| 163 |
IND |
**** For a complete description of the ITK Image Iterators and their API, please |
| 164 |
LEN,IND |
**** see the Iterators chapter in the ITK Software Guide. The ITK Software Guide |
| 165 |
IND |
**** is available in print and as a free .pdf download from http://www.itk.org. |
| 166 |
IND |
**** |
| 167 |
IND |
**** \author Rupert Brooks, McGill Centre for Intelligent Machines. Canada |
| 168 |
IND |
**** |
| 169 |
IND |
**** \ingroup ImageIterators |
| 170 |
IND |
**** |
| 171 |
IND |
**** \sa ImageConstIterator \sa ConditionalConstIterator |
| 172 |
IND |
**** \sa ConstNeighborhoodIterator \sa ConstShapedNeighborhoodIterator |
| 173 |
IND |
**** \sa ConstSliceIterator \sa CorrespondenceDataStructureIterator |
| 174 |
IND |
**** \sa FloodFilledFunctionConditionalConstIterator |
| 175 |
IND |
**** \sa FloodFilledImageFunctionConditionalConstIterator |
| 176 |
IND |
**** \sa FloodFilledImageFunctionConditionalIterator |
| 177 |
IND |
**** \sa FloodFilledSpatialFunctionConditionalConstIterator |
| 178 |
IND |
**** \sa FloodFilledSpatialFunctionConditionalIterator |
| 179 |
IND |
**** \sa ImageConstIterator \sa ImageConstIteratorWithIndex |
| 180 |
IND |
**** \sa ImageIterator \sa ImageIteratorWithIndex |
| 181 |
IND |
**** \sa ImageLinearConstIteratorWithIndex \sa ImageLinearIteratorWithIndex |
| 182 |
LEN,IND |
**** \sa ImageRandomNonRepeatingConstIteratorWithIndex \sa ImageRandomIteratorWithIndex |
| 183 |
IND |
**** \sa ImageRegionConstIterator \sa ImageRegionConstIteratorWithIndex |
| 184 |
IND |
**** \sa ImageRegionExclusionConstIteratorWithIndex |
| 185 |
IND |
**** \sa ImageRegionExclusionIteratorWithIndex |
| 186 |
IND |
**** \sa ImageRegionIterator \sa ImageRegionIteratorWithIndex |
| 187 |
IND |
**** \sa ImageRegionReverseConstIterator \sa ImageRegionReverseIterator |
| 188 |
IND |
**** \sa ImageReverseConstIterator \sa ImageReverseIterator |
| 189 |
IND |
**** \sa ImageSliceConstIteratorWithIndex \sa ImageSliceIteratorWithIndex |
| 190 |
IND |
**** \sa NeighborhoodIterator \sa PathConstIterator \sa PathIterator |
| 191 |
IND |
**** \sa ShapedNeighborhoodIterator \sa SliceIterator |
| 192 |
IND |
**** \sa ImageConstIteratorWithIndex |
| 193 |
IND |
**** |
| 194 |
IND |
****/ |
| 195 |
IND |
**template<typename TImage> |
| 196 |
LEN,IND |
**class ITK_EXPORT ImageRandomNonRepeatingConstIteratorWithIndex : public ImageConstIteratorWithIndex<TImage> |
| 197 |
IND |
**{ |
| 198 |
IND |
***public: |
| 199 |
IND |
****/** Standard class typedefs. */ |
| 200 |
IND |
****typedef ImageRandomNonRepeatingConstIteratorWithIndex Self; |
| 201 |
TDA,IND |
****typedef ImageConstIteratorWithIndex<TImage> Superclass; |
| 202 |
|
|
| 203 |
LEN,IND |
****/** Index typedef support. While this was already typdef'ed in the superclass |
| 204 |
LEN,IND |
****** it needs to be redone here for this subclass to compile properly with gcc. |
| 205 |
IND |
****** Note that we have to rescope Index back to itk::Index to that is it not |
| 206 |
IND |
****** confused with ImageIterator::Index. */ |
| 207 |
IND |
****typedef typename TImage::IndexType IndexType; |
| 208 |
|
|
| 209 |
LEN,IND |
****/** Region typedef support. While this was already typdef'ed in the superclass |
| 210 |
LEN,IND |
****** it needs to be redone here for this subclass to compile properly with gcc. |
| 211 |
IND |
****** Note that we have to rescope Region back to itk::ImageRegion so that is |
| 212 |
IND |
****** it not confused with ImageIterator::Index. */ |
| 213 |
IND |
****typedef typename TImage::RegionType RegionType; |
| 214 |
|
|
| 215 |
LEN,IND |
****/** Image typedef support. While this was already typdef'ed in the superclass |
| 216 |
LEN,IND |
****** it needs to be redone here for this subclass to compile properly with gcc. |
| 217 |
IND |
****** Note that we have to rescope Index back to itk::Index to that is it not |
| 218 |
IND |
****** confused with ImageIterator::Index. */ |
| 219 |
IND |
****typedef TImage ImageType; |
| 220 |
|
|
| 221 |
IND |
****/** PixelContainer typedef support. Used to refer to the container for |
| 222 |
IND |
****** the pixel data. While this was already typdef'ed in the superclass |
| 223 |
LEN,IND |
****** it needs to be redone here for this subclass to compile properly with gcc. */ |
| 224 |
IND |
****typedef typename TImage::PixelContainer PixelContainer; |
| 225 |
TDA,IND |
****typedef typename PixelContainer::Pointer PixelContainerPointer; |
| 226 |
|
|
| 227 |
IND |
****/** Default constructor. Needed since we provide a cast constructor. */ |
| 228 |
IND |
****ImageRandomNonRepeatingConstIteratorWithIndex(); |
| 229 |
LEN,IND |
****~ImageRandomNonRepeatingConstIteratorWithIndex() { if( m_Permutation ) delete m_Permutation; }; |
| 230 |
|
|
| 231 |
IND |
****/** Constructor establishes an iterator to walk a particular image and a |
| 232 |
IND |
****** particular region of that image. */ |
| 233 |
LEN,IND |
****ImageRandomNonRepeatingConstIteratorWithIndex(const ImageType *ptr, const RegionType& region); |
| 234 |
|
|
| 235 |
IND |
****/** Constructor that can be used to cast from an ImageIterator to an |
| 236 |
LEN,IND |
****** ImageRandomNonRepeatingConstIteratorWithIndex. Many routines return an ImageIterator but for a |
| 237 |
LEN,IND |
****** particular task, you may want an ImageRandomNonRepeatingConstIteratorWithIndex. Rather than |
| 238 |
IND |
****** provide overloaded APIs that return different types of Iterators, itk |
| 239 |
IND |
****** returns ImageIterators and uses constructors to cast from an |
| 240 |
IND |
****** ImageIterator to a ImageRandomNonRepeatingConstIteratorWithIndex. */ |
| 241 |
LEN,IND |
****ImageRandomNonRepeatingConstIteratorWithIndex( const ImageConstIteratorWithIndex<TImage> &it) |
| 242 |
|
{ |
| 243 |
IND |
******this->ImageConstIteratorWithIndex<TImage>::operator=(it); |
| 244 |
IND |
******m_Permutation = NULL; |
| 245 |
|
} |
| 246 |
IND |
****/** Move an iterator to the beginning of the region. */ |
| 247 |
IND |
****void GoToBegin(void) |
| 248 |
|
{ |
| 249 |
IND |
******m_NumberOfSamplesDone = 0L; |
| 250 |
IND |
******this->UpdatePosition(); |
| 251 |
|
} |
| 252 |
|
|
| 253 |
IND |
****/** Move an iterator to one position past the End of the region. */ |
| 254 |
IND |
****void GoToEnd(void) |
| 255 |
|
{ |
| 256 |
IND |
******m_NumberOfSamplesDone = m_NumberOfSamplesRequested; |
| 257 |
IND |
******this->UpdatePosition(); |
| 258 |
|
} |
| 259 |
|
|
| 260 |
IND |
****/** Is the iterator at the beginning of the region? */ |
| 261 |
IND |
****bool IsAtBegin(void) const |
| 262 |
|
{ |
| 263 |
IND |
******return (m_NumberOfSamplesDone == 0L); |
| 264 |
|
} |
| 265 |
|
|
| 266 |
IND |
****/** Is the iterator at the end of the region? */ |
| 267 |
IND |
****bool IsAtEnd(void) const |
| 268 |
|
{ |
| 269 |
IND |
******return (m_NumberOfSamplesDone >= m_NumberOfSamplesRequested); |
| 270 |
|
} |
| 271 |
|
|
| 272 |
|
|
| 273 |
IND |
****/** The moving image dimension. */ |
| 274 |
IND |
****itkStaticConstMacro( ImageDimension, unsigned int, |
| 275 |
|
::itk::GetImageDimension< TImage >::ImageDimension ); |
| 276 |
|
|
| 277 |
|
|
| 278 |
IND |
****/** Image with priorities */ |
| 279 |
IND |
****typedef itk::Image< unsigned long, |
| 280 |
|
itkGetStaticConstMacro(ImageDimension ) |
| 281 |
IND |
************************************************> PriorityImageType; |
| 282 |
|
|
| 283 |
IND |
****/** Set the priority image. The priority image controls the order |
| 284 |
IND |
********of the random selection. Pixels of the same priority will be |
| 285 |
IND |
********ordered randomly, but pixels of lower priority value will be |
| 286 |
IND |
********selected first. |
| 287 |
IND |
*******/ |
| 288 |
IND |
****void SetPriorityImage(const PriorityImageType * priorityImage); |
| 289 |
|
|
| 290 |
IND |
****/** Increment (prefix) the selected dimension. |
| 291 |
IND |
****** No bounds checking is performed. \sa GetIndex \sa operator-- */ |
| 292 |
IND |
****Self & operator++() |
| 293 |
|
{ |
| 294 |
IND |
******m_NumberOfSamplesDone++; |
| 295 |
IND |
******this->UpdatePosition(); |
| 296 |
IND |
******return *this; |
| 297 |
|
} |
| 298 |
|
|
| 299 |
IND |
****/** Decrement (prefix) the selected dimension. |
| 300 |
IND |
****** No bounds checking is performed. \sa GetIndex \sa operator++ */ |
| 301 |
IND |
****Self & operator--() |
| 302 |
|
{ |
| 303 |
IND |
******m_NumberOfSamplesDone--; |
| 304 |
IND |
******this->UpdatePosition(); |
| 305 |
IND |
******return *this; |
| 306 |
|
} |
| 307 |
|
|
| 308 |
IND |
****/** Set/Get number of random samples to get from the image region */ |
| 309 |
IND |
****void SetNumberOfSamples( unsigned long number ); |
| 310 |
IND |
****unsigned long GetNumberOfSamples( void ) const; |
| 311 |
|
|
| 312 |
IND |
****/** Reinitialize the seed of the random number generator */ |
| 313 |
IND |
****void ReinitializeSeed(); |
| 314 |
IND |
****/** Reinitialize the seed of the random number generator with |
| 315 |
IND |
********a specific value |
| 316 |
IND |
*****/ |
| 317 |
IND |
****void ReinitializeSeed(int); |
| 318 |
|
|
| 319 |
IND |
***private: |
| 320 |
IND |
****void UpdatePosition(); |
| 321 |
IND |
****unsigned long m_NumberOfSamplesRequested; |
| 322 |
IND |
****unsigned long m_NumberOfSamplesDone; |
| 323 |
IND |
****unsigned long m_NumberOfPixelsInRegion; |
| 324 |
IND |
****randomPermutation * m_Permutation; |
| 325 |
IND |
**}; |
| 326 |
|
|
| 327 |
|
} // end namespace itk |
| 328 |
|
|
| 329 |
|
#ifndef ITK_MANUAL_INSTANTIATION |
| 330 |
|
#include "itkImageRandomNonRepeatingConstIteratorWithIndex.txx" |
| 331 |
|
#endif |
| 332 |
|
|
| 333 |
|
#endif |
| 334 |
|
|
| 335 |
EOF |
|
| 336 |
EOF,EML |
|
| 337 |
EOF,EML |
|