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<<"MakeImage"<<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("C:\\selimmingwinput\\FA.txt", "r");</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>fpp = fopen("C:\\selimmingwinput\\b0.txt", "r");</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("I couldn't open results.dat for reading.\n");</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< float, 3 > ImageType;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>typedef itk::ImageFileWriter< ImageType > 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<<"MakeImage"<<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->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>ImageType::PixelType initialValue = 0;</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>image->FillBuffer( initialValue );</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>cout<<"MakeImage"<<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->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->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<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<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<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, "%f", &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<<pixelValue<<endl;</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>}</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<<counter<<endl;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>cout<<"MakeImage"<<endl;</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>writer->SetInput(image);</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>writer->SetFileName("C:\\selimmingwinput\\test.vtk");</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<<"MakeImage"<<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->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 & 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 << "WRITER UPDATE ERROR" << std::endl;</div>
<div><span class="Apple-tab-span" style="white-space:pre">                        </span>std::cerr << e << 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>