I tried to generate a binary image based on a random number. I observed something I haven't understood. What I did was quite simple. I generated a uniform random number between 0 and 1. If the random number is smaller than 0.5, then the image intensity value was set to zero, otherwise 1000. I tested the idea by generating a 2D image. What I found was that when the image sizes (both x and y) were set to 2^N (N= 1, 2, 3, 4....), the generated image was fine. With other image sizes, there were two interesting observations: (1) the generated image was not a binary image, ie, the image intensities were not 0 and 1000 but there were some intermediate values; (2) the sizes of the generated image were not the sizes I specified. For instance, if I specified a 3X3 region, but the generated image would have 4X4 sizes. I don't know what I did wrong here. Any help or suggestions would be appreciated! <div>
<br></div><div><br></div><div>Here is the code I used to generate the image.</div><div><br></div><div><div>#include "itkImage.h"</div><div><br></div><div>#include "itkImageFileReader.h"</div><div>#include "itkImageFileWriter.h"</div>
<div><br></div><div>#include <fstream></div><div>//#include <cstdlib></div><div>//#include <ctime></div><div><br></div><div>#include "rng.h"</div><div><br></div><div>//using namespace std;</div>
<div><br></div><div>//---------------------------------------------------------------------------------------</div><div>int main( )</div><div>{</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>const unsigned int Dimension = 2;</div>
<div> </div><div><span class="Apple-tab-span" style="white-space:pre">        </span>typedef unsigned short PixelType;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>typedef itk::Image< PixelType, Dimension ><span class="Apple-tab-span" style="white-space:pre">                                                </span> ImageType;</div>
<div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>ImageType::Pointer image = ImageType::New();</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>ImageType::IndexType start;</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>start[0] = 0; start[1] = 0; //start[2] = 0;</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>ImageType::SizeType size;</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>size[0] = 33; size[1] = 33; //size[2] = 1;</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>ImageType::RegionType region;</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>region.SetSize( size ); region.SetIndex( start );</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>image->SetRegions( region );</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>image->Allocate(); </div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>/*</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>ImageType::SpacingType spacing;</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>spacing[0] = 1.0; spacing[1] = 1.0; spacing[2] = 0.0;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>image->SetSpacing( spacing );</div>
<div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>ImageType::PointType origin;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>origin[0] = 0.0; origin[1] = 0.0; origin[2] = 0.0;</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>image->SetOrigin( origin );</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>*/</div><div><span class="Apple-tab-span" style="white-space:pre">        </span></div>
<div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>RNG rdm;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>int counter=0;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>for ( int i = 0; i < size[0]; i++) {</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>for ( int j = 0; j < size[1]; j++ ) {</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>//for ( int k = 0; k < size[2]; k++ ) {</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                        </span></div><div><span class="Apple-tab-span" style="white-space:pre">                                </span>ImageType::IndexType pixelIndex;</div><div><span class="Apple-tab-span" style="white-space:pre">                                </span>pixelIndex[0] = i;</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                </span>pixelIndex[1] = j;</div><div><span class="Apple-tab-span" style="white-space:pre">                                </span>//pixelIndex[2] = k;</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                                </span>double prob = rdm.uniform();</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                </span>//std::cout << "probability = " << prob << std::endl;</div><div><span class="Apple-tab-span" style="white-space:pre">                                </span></div>
<div><span class="Apple-tab-span" style="white-space:pre">                                </span>if (prob <= 0.5) { </div><div><span class="Apple-tab-span" style="white-space:pre">                                        </span>ImageType::PixelType pixelValue = itk::NumericTraits < PixelType >::Zero;</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                        </span>image->SetPixel( pixelIndex, pixelValue );</div><div><span class="Apple-tab-span" style="white-space:pre">                                        </span>counter++;</div><div><span class="Apple-tab-span" style="white-space:pre">                                </span>} else { </div>
<div><span class="Apple-tab-span" style="white-space:pre">                                        </span>ImageType::PixelType pixelValue = 1000;</div><div><span class="Apple-tab-span" style="white-space:pre">                                        </span>image->SetPixel( pixelIndex, pixelValue );</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">                                </span></div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>//}</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span></div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>for ( int i = 0; i < size[0]; i++) {</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>for ( int j = 0; j < size[1]; j++ ) {</div>
<div><span class="Apple-tab-span" style="white-space:pre">                        </span>//for ( int k = 0; k < size[2]; k++ ) {</div><div><span class="Apple-tab-span" style="white-space:pre">                                </span></div><div><span class="Apple-tab-span" style="white-space:pre">                                </span>ImageType::IndexType pixelIndex;</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                </span>pixelIndex[0] = i;</div><div><span class="Apple-tab-span" style="white-space:pre">                                </span>pixelIndex[1] = j;</div><div><span class="Apple-tab-span" style="white-space:pre">                                </span>//pixelIndex[2] = k;</div>
<div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                                </span>ImageType::PixelType outPixel = image->GetPixel(pixelIndex); </div><div><span class="Apple-tab-span" style="white-space:pre">                                </span>//std::cout << "pixel value = " << outPixel << std::endl;</div>
<div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>//}</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span></div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>std::cout << counter << " pixels of " << size[0]*size[1] << " have zero values." << std::endl;</div>
<div><br></div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>typedef itk::ImageFileWriter< ImageType ><span class="Apple-tab-span" style="white-space:pre">                                                </span> WriterType;</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>WriterType::Pointer imgWriter = WriterType::New();</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>imgWriter->SetInput( image );</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>imgWriter->SetFileName( "test.vtk" );</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>imgWriter->Update();</div><div>
<br></div><div> </div><div><br></div><div><br></div><div><br></div><div> <span class="Apple-tab-span" style="white-space:pre">        </span>return EXIT_SUCCESS;</div><div>}</div></div>