<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 &quot;random()&quot;.<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&#39;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>&nbsp;&nbsp;&nbsp;&nbsp; Thanks<br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Luis<br><br><br>------------------------------------------<br><br><div class="gmail_quote">On Mon, Feb 9, 2009 at 11:57 AM, Ming Chao <span dir="ltr">&lt;<a href="mailto:mingchao2005@gmail.com">mingchao2005@gmail.com</a>&gt;</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&#39;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&#39;t know what I did wrong here. Any help or suggestions would be appreciated!&nbsp;<div>

<br></div><div><br></div><div>Here is the code I used to generate the image.</div><div><br></div><div><div>#include &quot;itkImage.h&quot;</div><div><br></div><div>#include &quot;itkImageFileReader.h&quot;</div><div>#include &quot;itkImageFileWriter.h&quot;</div>

<div><br></div><div>#include &lt;fstream&gt;</div><div>//#include &lt;cstdlib&gt;</div><div>//#include &lt;ctime&gt;</div><div><br></div><div>#include &quot;rng.h&quot;</div><div><br></div><div>//using namespace std;</div>

<div><br></div><div>//---------------------------------------------------------------------------------------</div><div>int main( &nbsp; )</div><div>{</div><div><br></div><div><span style="white-space: pre;">        </span>const &nbsp; unsigned int &nbsp; Dimension = 2;</div>

<div>&nbsp;</div><div><span style="white-space: pre;">        </span>typedef unsigned short &nbsp;PixelType;</div><div><span style="white-space: pre;">        </span>typedef itk::Image&lt; PixelType, Dimension &gt;<span style="white-space: pre;">                                                </span> &nbsp;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-&gt;SetRegions( region );</div>
<div><span style="white-space: pre;">        </span>image-&gt;Allocate();&nbsp;</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-&gt;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-&gt;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 &lt; size[0]; i++) {</div>

<div><span style="white-space: pre;">                </span>for ( int j = 0; j &lt; size[1]; j++ ) {</div><div><span style="white-space: pre;">                        </span>//for ( int k = 0; k &lt; 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 &lt;&lt; &quot;probability = &quot; &lt;&lt; prob &lt;&lt; std::endl;</div><div><span style="white-space: pre;">                                </span></div>
<div><span style="white-space: pre;">                                </span>if (prob &lt;= 0.5) {&nbsp;</div><div><span style="white-space: pre;">                                        </span>ImageType::PixelType pixelValue = itk::NumericTraits &lt; PixelType &gt;::Zero;</div>
<div><span style="white-space: pre;">                                        </span>image-&gt;SetPixel( pixelIndex, pixelValue );</div><div><span style="white-space: pre;">                                        </span>counter++;</div><div><span style="white-space: pre;">                                </span>} else {&nbsp;</div>

<div><span style="white-space: pre;">                                        </span>ImageType::PixelType pixelValue = 1000;</div><div><span style="white-space: pre;">                                        </span>image-&gt;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 &lt; size[0]; i++) {</div><div><span style="white-space: pre;">                </span>for ( int j = 0; j &lt; size[1]; j++ ) {</div>
<div><span style="white-space: pre;">                        </span>//for ( int k = 0; &nbsp;k &lt; 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-&gt;GetPixel(pixelIndex);&nbsp;</div><div><span style="white-space: pre;">                                </span>//std::cout &lt;&lt; &quot;pixel value = &quot; &lt;&lt; outPixel &lt;&lt; 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 &lt;&lt; counter &lt;&lt; &quot; pixels of &quot; &lt;&lt; size[0]*size[1] &lt;&lt; &quot; have zero values.&quot; &lt;&lt; std::endl;</div>

<div><br></div><div><br></div><div><span style="white-space: pre;">        </span>typedef itk::ImageFileWriter&lt; &nbsp;ImageType &gt;<span style="white-space: pre;">                                                </span> &nbsp;WriterType;</div>
<div><span style="white-space: pre;">        </span>WriterType::Pointer imgWriter = WriterType::New();</div><div><span style="white-space: pre;">        </span>imgWriter-&gt;SetInput( image );</div>
<div><span style="white-space: pre;">        </span>imgWriter-&gt;SetFileName( &quot;test.vtk&quot; );</div><div><span style="white-space: pre;">        </span>imgWriter-&gt;Update();</div><div>
<br></div><div>&nbsp;</div><div><br></div><div><br></div><div><br></div><div>&nbsp;<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>