Hi all,<div><br></div><div>I wrote a code to read float numbers  from a file and tried to create a 3-D .vtk image.</div><div><br></div><div>I have an image in the end but the image is proper?</div><div><br></div><div>Can some one help to solve this problem??</div>
<div><br></div><div><br></div><div>I attached my code!!!!</div><div><br></div><div>The input file has float numbers, I want to read from the file and write the pixel value.</div><div><br></div><div>****************************************************************************************************</div>
<div><br></div><div><div>void Ui_MainWindow::makeimage()</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>{</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>cout&lt;&lt;&quot;MakeImage&quot;&lt;&lt;endl;</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>FILE *fp;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>FILE *fpp;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>float f;</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>fp = fopen(&quot;C:\\selimmingwinput\\FA.txt&quot;, &quot;r&quot;);</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>fpp = fopen(&quot;C:\\selimmingwinput\\b0.txt&quot;, &quot;r&quot;);</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 (fp == NULL) </div><div><span class="Apple-tab-span" style="white-space:pre">                </span>{</div>
<div>         printf(&quot;I couldn&#39;t open results.dat for reading.\n&quot;);</div><div>         exit(0);</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>int counter =0;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span><span class="Apple-tab-span" style="white-space:pre">        </span></div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>//<span class="Apple-tab-span" style="white-space:pre">        </span>ImageIOType::Pointer gdcmImageIO = ImageIOType::New();</div><div><span class="Apple-tab-span" style="white-space:pre">                </span></div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>typedef itk::Image&lt; float, 3 &gt; ImageType;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>typedef itk::ImageFileWriter&lt; ImageType &gt; WriterType;</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::Pointer image = ImageType::New();</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>WriterType::Pointer writer = WriterType::New();</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>cout&lt;&lt;&quot;MakeImage&quot;&lt;&lt;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>ImageType::IndexType start;</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></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>size[0]  = 144;  // size along X</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>size[1]  = 144;  // size along Y</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>size[2]  = 60;  // size along Z</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>start[0] =   0;  // first index on X</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>start[1] =   0;  // first index on Y</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>start[2] =   0;  // first index on Z</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::RegionType region;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>region.SetSize( size );</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>region.SetIndex( start );</div><div><span class="Apple-tab-span" style="white-space:pre">                </span></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>image-&gt;SetRegions( region );</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>image-&gt;Allocate();</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>ImageType::PixelType  initialValue = 0;</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>image-&gt;FillBuffer( initialValue );</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>cout&lt;&lt;&quot;MakeImage&quot;&lt;&lt;endl;</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>ImageType::PixelType   pixelValue;</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>ImageType::SpacingType spacing;</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>spacing[0]=0.33;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>spacing[1]=0.33;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>spacing[2]=1.2;</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>int counter2=0;</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>ImageType::PointType origin;</div><div><br></div><div>
<span class="Apple-tab-span" style="white-space:pre">                </span>origin[0]=0.0;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>origin[1]=0.0;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>origin[2]=0.0;</div>
<div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>image-&gt;SetOrigin(origin);</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>image-&gt;SetSpacing(spacing);</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 z=0;z&lt;144;z++)</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 j=0;j&lt;144;j++)</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&lt;60;i++)</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                        </span>{</div><div><span class="Apple-tab-span" style="white-space:pre">                                                </span>pixelIndex[0] = j;   // x position</div><div><span class="Apple-tab-span" style="white-space:pre">                                                </span>pixelIndex[1] = z;   // y position</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                                </span>pixelIndex[2] = i;   // z position</div><div><span class="Apple-tab-span" style="white-space:pre">                                                </span>fscanf (fp, &quot;%f&quot;, &amp;f);</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                                </span>pixelValue=f;</div><div><span class="Apple-tab-span" style="white-space:pre">                                        </span>//<span class="Apple-tab-span" style="white-space:pre">        </span>cout&lt;&lt;pixelValue&lt;&lt;endl;</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                                </span>image-&gt;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>}</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>cout&lt;&lt;counter&lt;&lt;endl;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>cout&lt;&lt;&quot;MakeImage&quot;&lt;&lt;endl;</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>writer-&gt;SetInput(image);</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>writer-&gt;SetFileName(&quot;C:\\selimmingwinput\\test.vtk&quot;);</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>cout&lt;&lt;&quot;MakeImage&quot;&lt;&lt;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></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>try</div>
<div><span class="Apple-tab-span" style="white-space:pre">                        </span>{</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>writer-&gt;Update();</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>  catch( itk::ExceptionObject &amp; e )</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>{</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>std::cerr &lt;&lt; &quot;WRITER UPDATE ERROR&quot; &lt;&lt; std::endl;</div>
<div><span class="Apple-tab-span" style="white-space:pre">                        </span>std::cerr &lt;&lt; e &lt;&lt; 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>}</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>
<div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div>***************************************************************************************************</div>
<div><br></div><div><br>-- <br>Selim Hasan<br><br>Yeditepe University<br>Computer Science Engineering<br><br><a href="mailto:selimhasan@gmail.com">selimhasan@gmail.com</a><br>
</div>