<br>Hi Ming,<br><br>Thanks for your clear description of the problem and for posting the source code.<br><br>I just reformatted your code a bit, and ... <br>it works for me.... :-)<br><br>Well,<br>I also replaced your RNG object with a call to the GNU/Linux function "random()".<br>
<br>It produces an image of the correct size (33x33) and the pixel values are indeed<br>0 and 1000, and distributed close to 50% versus 50%.<br><br>I'm wondering if the problem may be with the tool that you are using for looking<br>
at the output test.vtk file. It may be that the tool is not interpreting correctly the<br>fact that you have 16-bits values.<br><br>Independent of the problem that you are reporting, please not that your method<br>for accessing pixel values is quite inefficient. I have converted your code to use<br>
ITK iterators (which is the fastest way of visiting pixel data in ITK).<br><br>Please find attached to this email the following files:<br><br><ul><li>Random.cxx: your source code reformatted</li><li>Random2.cxx: converted to use ITK iterators</li>
<li>CMakeLists.txt file for building both</li></ul><br>Please give it a try to building these two files and let us know if you <br>still see any problem.<br><br>(in which case, please let us know what tool you are using for looking<br>
at the output image).<br><br><br> Thanks<br><br><br> Luis<br><br><br>------------------------------------------<br><br><div class="gmail_quote">On Mon, Feb 9, 2009 at 11:57 AM, Ming Chao <span dir="ltr"><<a href="mailto:mingchao2005@gmail.com">mingchao2005@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">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 style="white-space: pre;">        </span>const unsigned int Dimension = 2;</div>
<div> </div><div><span style="white-space: pre;">        </span>typedef unsigned short PixelType;</div><div><span style="white-space: pre;">        </span>typedef itk::Image< PixelType, Dimension ><span style="white-space: pre;">                                                </span> ImageType;</div>
<div><br></div><div><span style="white-space: pre;">        </span>ImageType::Pointer image = ImageType::New();</div><div><br></div><div><span style="white-space: pre;">        </span>ImageType::IndexType start;</div>
<div><span style="white-space: pre;">        </span>start[0] = 0; start[1] = 0; //start[2] = 0;</div><div><br></div><div><span style="white-space: pre;">        </span>ImageType::SizeType size;</div>
<div><span style="white-space: pre;">        </span>size[0] = 33; size[1] = 33; //size[2] = 1;</div><div><br></div><div><span style="white-space: pre;">        </span>ImageType::RegionType region;</div>
<div><span style="white-space: pre;">        </span>region.SetSize( size ); region.SetIndex( start );</div><div><br></div><div><span style="white-space: pre;">        </span>image->SetRegions( region );</div>
<div><span style="white-space: pre;">        </span>image->Allocate(); </div><div><br></div><div><span style="white-space: pre;">        </span>/*</div><div><span style="white-space: pre;">        </span>ImageType::SpacingType spacing;</div>
<div><span style="white-space: pre;">        </span>spacing[0] = 1.0; spacing[1] = 1.0; spacing[2] = 0.0;</div><div><span style="white-space: pre;">        </span>image->SetSpacing( spacing );</div>
<div><br></div><div><span style="white-space: pre;">        </span>ImageType::PointType origin;</div><div><span style="white-space: pre;">        </span>origin[0] = 0.0; origin[1] = 0.0; origin[2] = 0.0;</div>
<div><span style="white-space: pre;">        </span>image->SetOrigin( origin );</div><div><span style="white-space: pre;">        </span>*/</div><div><span style="white-space: pre;">        </span></div>
<div><br></div><div><span style="white-space: pre;">        </span>RNG rdm;</div><div><span style="white-space: pre;">        </span>int counter=0;</div><div><span style="white-space: pre;">        </span>for ( int i = 0; i < size[0]; i++) {</div>
<div><span style="white-space: pre;">                </span>for ( int j = 0; j < size[1]; j++ ) {</div><div><span style="white-space: pre;">                        </span>//for ( int k = 0; k < size[2]; k++ ) {</div>
<div><span style="white-space: pre;">                                        </span></div><div><span style="white-space: pre;">                                </span>ImageType::IndexType pixelIndex;</div><div><span style="white-space: pre;">                                </span>pixelIndex[0] = i;</div>
<div><span style="white-space: pre;">                                </span>pixelIndex[1] = j;</div><div><span style="white-space: pre;">                                </span>//pixelIndex[2] = k;</div><div><br></div><div><span style="white-space: pre;">                                </span>double prob = rdm.uniform();</div>
<div><span style="white-space: pre;">                                </span>//std::cout << "probability = " << prob << std::endl;</div><div><span style="white-space: pre;">                                </span></div>
<div><span style="white-space: pre;">                                </span>if (prob <= 0.5) { </div><div><span style="white-space: pre;">                                        </span>ImageType::PixelType pixelValue = itk::NumericTraits < PixelType >::Zero;</div>
<div><span style="white-space: pre;">                                        </span>image->SetPixel( pixelIndex, pixelValue );</div><div><span style="white-space: pre;">                                        </span>counter++;</div><div><span style="white-space: pre;">                                </span>} else { </div>
<div><span style="white-space: pre;">                                        </span>ImageType::PixelType pixelValue = 1000;</div><div><span style="white-space: pre;">                                        </span>image->SetPixel( pixelIndex, pixelValue );</div>
<div><span style="white-space: pre;">                                </span>}</div><div><span style="white-space: pre;">                                </span></div><div><span style="white-space: pre;">                        </span>//}</div>
<div><span style="white-space: pre;">                </span>}</div><div><span style="white-space: pre;">        </span>}</div><div><br></div><div><span style="white-space: pre;">        </span></div>
<div><span style="white-space: pre;">        </span>for ( int i = 0; i < size[0]; i++) {</div><div><span style="white-space: pre;">                </span>for ( int j = 0; j < size[1]; j++ ) {</div>
<div><span style="white-space: pre;">                        </span>//for ( int k = 0; k < size[2]; k++ ) {</div><div><span style="white-space: pre;">                                </span></div><div><span style="white-space: pre;">                                </span>ImageType::IndexType pixelIndex;</div>
<div><span style="white-space: pre;">                                </span>pixelIndex[0] = i;</div><div><span style="white-space: pre;">                                </span>pixelIndex[1] = j;</div><div><span style="white-space: pre;">                                </span>//pixelIndex[2] = k;</div>
<div><br></div><div><span style="white-space: pre;">                                </span>ImageType::PixelType outPixel = image->GetPixel(pixelIndex); </div><div><span style="white-space: pre;">                                </span>//std::cout << "pixel value = " << outPixel << std::endl;</div>
<div><br></div><div><span style="white-space: pre;">                        </span>//}</div><div><span style="white-space: pre;">                </span>}</div><div><span style="white-space: pre;">        </span>}</div>
<div><span style="white-space: pre;">        </span></div><div><br></div><div><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 style="white-space: pre;">        </span>typedef itk::ImageFileWriter< ImageType ><span style="white-space: pre;">                                                </span> WriterType;</div>
<div><span style="white-space: pre;">        </span>WriterType::Pointer imgWriter = WriterType::New();</div><div><span style="white-space: pre;">        </span>imgWriter->SetInput( image );</div>
<div><span style="white-space: pre;">        </span>imgWriter->SetFileName( "test.vtk" );</div><div><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 style="white-space: pre;">        </span>return EXIT_SUCCESS;</div><div>}</div></div>
<br>_____________________________________<br>
Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at<br>
<a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Please keep messages on-topic and check the ITK FAQ at: <a href="http://www.itk.org/Wiki/ITK_FAQ" target="_blank">http://www.itk.org/Wiki/ITK_FAQ</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://www.itk.org/mailman/listinfo/insight-users" target="_blank">http://www.itk.org/mailman/listinfo/insight-users</a><br>
<br></blockquote></div><br>