[Insight-users] Re: Writing 3D image from text file?

Luis Ibanez luis.ibanez at kitware.com
Sun Dec 31 18:08:48 EST 2006


Hi Ekta

It is normal that you don't find anything in the image,


You are missing to call:


      image->SetPixel( pixelIndex , pixelValue )



in the if( IsInside() ) block inside the while loop
that iterate through the points.


You had the call in the code that you posted in your
previous email, but not in the one that you just posted.
You probably deleted it by accident.


    Please add the SetPixel() call in your code.


BTW, please not also that if you are using an unsigned short
image, and you store values that are small compared to the
maximum possible value (65535) then those pixels will appear
black in most primitive image viewers.  You may want to make
sure that you use and image viewer that rescales the image
intensities (or at least offers you the option to do so upon
request), for example GIMP or ImageMagick's diplay.


   Regards,


      Luis


----------------
Ekta Amar wrote:
> Hello Luis,
> 
> I changed the format to .tiff which supports 3D images. Hence, I'm able 
> to write 3D image but unfortunalty there is nothing in the image. Its 
> just creating a blank image ..
> 
> I think I'm not able to provide correctly the input to Image writer. I'm 
> able to read text file but not able provide that information to write on 
> image. I went through chapter Image read and write but unable to find 
> any solution to write it. I used "TransformPhysicalPointToIndex " to 
> convert the points to pixel but still not able to write that point and 
> create a 3D image.
> 
> Please let me know where I'm doing wrong.
> 
> Thanks,
> Ekta
> 
> Following is my code,
> #include "itkImage.h"
> #include "itkPoint.h"
> 
> #include "itkImageFileWriter.h"
> 
> #include "itkPNGImageIO.h"
> 
> #include "itkPointSet.h"
> 
> int main(int argc, char * argv[])
> 
> {
> 
> const unsigned int Dimension = 3;
> 
> typedef itk::PointSet< float, Dimension > PointSetType;
> 
> PointSetType::Pointer ptPointSet = PointSetType::New ();
> 
> typedef PointSetType::PointType PointType;
> 
> typedef PointSetType::PointsContainer PointsContainer;
> 
> PointsContainer::Pointer ptPointContainer = PointsContainer::New();
> 
> PointType ptPoint;
> 
> argv[1] = "pointFile.txt";
> 
> std::ifstream pointFile;
> 
> pointFile.open( argv[1] );
> 
> if (pointFile.fail() )
> 
> {
> 
> std::cerr << "Error opening points file : " << std::endl;
> 
> std::cerr << argv[1] << std::endl;
> 
> return 2;
> 
> }
> 
> unsigned int pointId = 0;
> 
> pointFile >> ptPoint ;
> 
> while ( !pointFile.eof() )
> 
> {
> 
> ptPointContainer -> InsertElement ( pointId, ptPoint );
> 
> pointFile >> ptPoint;
> 
> pointId++;
> 
> }
> 
> ptPointSet -> SetPoints ( ptPointContainer );
> 
> std:: cout << "Number of points =" << ptPointSet -> GetNumberOfPoints() 
> << std::endl;
> 
> // To write image
> 
> typedef itk::Image< unsigned short, 3 > ImageType;
> 
> ImageType::Pointer image = ImageType::New();
> 
> ImageType::IndexType start;
> 
> ImageType::SizeType size;
> 
> size[0] = 111; // size along X
> 
> size[1] = 112; // size along Y
> 
> size[2] = 10.63; // size along Z
> 
> start[0] = 0; // first index on X
> 
> start[1] = 0; // first index on Y
> 
> start[2] = 0; // first index on Z
> 
> ImageType::RegionType region;
> 
> region.SetSize( size );
> 
> region.SetIndex( start );
> 
> 
> image->SetRegions( region );
> 
> image->Allocate();
> 
> image->FillBuffer( 0 );
> 
> 
> ImageType::SpacingType spacing;
> 
> spacing[0]= 8.0;
> 
> spacing[1]= 8.0;
> 
> spacing[2]= 8.0;
> 
> image ->SetSpacing( spacing );
> 
> const ImageType::SpacingType& sp = image ->GetSpacing();
> 
> std::cout <<" Spacing = ";
> 
> std::cout <<sp[0] <<" , "<< sp[1] <<" , "<< sp[2] << std::endl;
> 
> ImageType::PointType origin;
> 
> 
> origin[0] = 494553;
> 
> origin[1] = 4249578;
> 
> origin[2] = -38.676;
> 
> image -> SetOrigin (origin);
> 
> const ImageType::PointType& orgn = image-> GetOrigin();
> 
> std::cout<<" Origin = ";
> 
> std::cout<< orgn[0] << ", "<< orgn[1] <<" , " <<orgn[2] << std::endl;
> 
> typedef itk::Point< float, ImageType::ImageDimension > PointType;
> 
> ImageType::IndexType pixelIndex;
> 
> //Tried to read the points sequentially
> 
> typedef PointSetType::PointDataContainer PointDataContainer;
> 
> PointsContainer::Pointer point2 = ptPointSet ->GetPoints();
> 
> typedef PointsContainer::Iterator PointsIterator;
> 
> PointsIterator pointIterator = point2 ->Begin();
> 
> PointsIterator end = point2 -> End();
> 
> PointType pointValue;
> 
> while (pointIterator != end)
> 
> {
> 
> bool isInside = image -> TransformPhysicalPointToIndex ( ptPoint, 
> pixelIndex );
> 
> 
> if (isInside)
> 
> {
> 
> pointValue = pointIterator.Value();
> 
> std::cout<<pointValue<<std::endl;
> 
> ++pointIterator;
> 
> 
> }
> 
> }
> 
> typedef itk::ImageFileWriter< ImageType > WriterType;
> 
> WriterType::Pointer writer = WriterType::New();
> 
> char pic[200] = "try.tiff";
> 
> const char * outputFilename = pic;
> 
> writer->SetFileName( outputFilename );
> 
> writer ->SetInput(image );
> 
> try
> 
> {
> 
> writer->Update();
> 
> std::cerr << "Success" << std::endl;
> 
> }
> 
> catch( itk::ExceptionObject & err )
> 
> {
> 
> std::cerr << "ExceptionObject caught !" << std::endl;
> 
> std::cerr << err << std::endl;
> 
> return EXIT_FAILURE;
> 
> }
> 
> 
> return 0;
> 
> }
> 
> ----- Original Message ----- From: "Luis Ibanez" <luis.ibanez at kitware.com>
> To: "ekta amar" <ektaamar at gmail.com>
> Cc: <amar.3 at osu.edu>; "Insight Users" <insight-users at itk.org>
> Sent: Friday, December 29, 2006 5:40 PM
> Subject: Re: Writing 3D image from text file?
> 
> 
>>
>> Hi Ekta,
>>
>> What is the message that the exception give you ?
>>
>>
>> You are probably having trouble because you are using a
>> PNG fileformat in order to save a 3D image.
>>
>>
>>          PNG only supports 2D images.
>>
>>
>> You may want to use a different file format, such as
>> VTK, MetaImage or Analyze, for saving your image.
>>
>> You will find a list of the file format supported
>> in the ITK Software Guide:
>>
>>
>>    http://www.itk.org/ItkSoftwareGuide.pdf
>>
>> in the Chapter "Reading and Writing Images"
>>
>>
>>
>>    Regards,
>>
>>
>>
>>       Luis
>>
>>
>>
>> ------------------
>> ekta amar wrote:
>>
>>> Dear Luis,
>>>
>>> The answers to all of your questions is Yes..I want to see points 
>>> (text file) which are in space as pixels in 3D image.
>>>
>>> As per told by you I tried to write an image (/just one single point 
>>> for try/) but unable to write it...
>>> I used the command *writer -> SetInput(image)* where /*image*/ is 
>>> variable where my pixel index and pixel value is stored.
>>>
>>> I'm not able to write the image. Everytime I'm getting error 
>>> Exception object caught.
>>>
>>> Following is my code I used to write.
>>>
>>> Please go through it and let me know where I'm doing wrong.
>>>
>>> In case I've to read points from file then I've to modify the 
>>> pixelIndex? Is it right?  #include "itkImage.h"
>>>
>>> #include "itkPoint.h"
>>>
>>> #include " itkImageFileWriter.h"
>>>
>>>  int main()
>>>
>>> {
>>>
>>> typedef itk::Image< unsigned short, 3 > ImageType;
>>>
>>> ImageType::Pointer image = ImageType::New();
>>>
>>> ImageType::IndexType start;
>>>
>>> ImageType::SizeType size;
>>>
>>> size[0] = 200; // size along X
>>>
>>> size[1] = 200; // size along Y
>>>
>>> size[2] = 200; // size along Z
>>>
>>> start[0] = 0; // first index on X
>>>
>>> start[1] = 0; // first index on Y
>>>
>>> start[2] = 0; // first index on Z
>>>
>>> ImageType::RegionType region;
>>>
>>> region.SetSize( size );
>>>
>>> region.SetIndex( start );
>>>
>>>
>>>
>>> image->SetRegions( region );
>>>
>>> image->Allocate();
>>>
>>> image->FillBuffer( 0 );
>>>
>>>
>>>
>>> ImageType::SpacingType spacing;
>>>
>>> spacing[0]= 0.33;
>>>
>>> spacing[1]= 0.33;
>>>
>>> spacing[2]= 1.20;
>>>
>>> image ->SetSpacing( spacing );
>>>
>>> ImageType::PointType origin;
>>>
>>> origin[0] = 0;
>>>
>>> origin[1] = 0;
>>>
>>> origin[2] = 0;
>>>
>>> image -> SetOrigin (origin);
>>>
>>> typedef itk::Point< float, ImageType::ImageDimension > PointType;
>>>
>>> PointType point;
>>>
>>> point[0] = 1.45;
>>>
>>> point[1] = 7.21;
>>>
>>> point[2] = 9.28;
>>>
>>> ImageType::IndexType pixelIndex;
>>>
>>> bool isInside = image -> TransformPhysicalPointToIndex ( point, 
>>> pixelIndex );
>>>
>>> if (isInside)
>>>
>>> {
>>>
>>> ImageType::PixelType pixelValue = image ->GetPixel(pixelIndex);
>>>
>>> pixelValue += 5;
>>>
>>> image -> SetPixel( pixelIndex, pixelValue );
>>>
>>> }
>>>
>>> typedef itk::ImageFileWriter< ImageType > WriterType;
>>>
>>> WriterType::Pointer writer = WriterType::New();
>>>
>>> char pic[200] = " try12.png";
>>>
>>> const char * outputFilename = pic;
>>>
>>> writer->SetFileName( outputFilename );
>>>
>>> *writer ->SetInput(image ); //Not able to write image..
>>> *
>>> try
>>>
>>> {
>>>
>>> writer->Update();
>>>
>>> std::cerr << "Success" << std::endl;
>>>
>>> }
>>>
>>> catch( itk::ExceptionObject & err )
>>>
>>> {
>>>
>>> std::cerr << "ExceptionObject caught !" << std::endl;
>>>
>>> std::cerr << err << std::endl;
>>>
>>> return EXIT_FAILURE;
>>>
>>> }
>>>
>>>
>>>
>>> return 0;
>>>
>>> }
>>>  Please tell how can I modify this writer command to write the image 
>>> properly.
>>>  Thanks,
>>> Ekta
>>>  ----- Original Message -----
>>> From: "Luis Ibanez" <luis.ibanez at kitware.com 
>>> <mailto:luis.ibanez at kitware.com>>
>>> To: "Ekta Amar" <amar.3 at osu.edu <mailto:amar.3 at osu.edu>>
>>> Cc: "ITK" <Insight-users at itk.org <mailto:Insight-users at itk.org>>
>>> Sent: Friday, December 29, 2006 11:14 AM
>>> Subject: Re: Writing 3D image from text file?
>>>
>>>  >
>>>  > Hi Ekta,
>>>  >
>>>  > What are the x,y,z coordinates representing ?
>>>  >
>>>  > Points in space ?
>>>  >
>>>  > Would you like to see them as pixels turned on in a 3D image ?
>>>  >
>>>  > You could do
>>>  >
>>>  > 1) Create an image whose physical extent covers all the points.
>>>  > 2) Initialize the image pixels to zero
>>>  > 3) Do a for loop over all the points and invoke the image method
>>>  >    PhysicalPointToIndex(), then take that index and invoke the
>>>  >    image method SetPixe(index,value) in order to set the pixel to
>>>  >    a high value.
>>>  > 4) Write the image to a file using the ImageFileWriter
>>>  >
>>>  >
>>>  >
>>>  > Regards,
>>>  >
>>>  >
>>>  >    Luis
>>>  >
>>>  >
>>>  >
>>>  > ---------------
>>>  > Ekta Amar wrote:
>>>  >> Hello Luis,
>>>  >> >> Is there any way I can read a text file having x, y, and z 
>>> co-ordinates
>>>  >> and write it as a 3D image?
>>>  >> >> I'm able to read a text file but I'm not getting how can I 
>>> make/write  a
>>>  >> 3D image from that text file.
>>>  >> >> I tried using Image2.cxx example which explains reading from a 
>>> file but
>>>  >> when I'm using .txt file to read it is giving me error, which I can
>>>  >> understand is not correct format.
>>>  >> >> Is there a way I can read text file and create a 3D image from 
>>> that.
>>>  >> >> Please help me,
>>>  >> >> Thanks,
>>>  >> Ekta
>>>  >>
>>>
> 
> 


More information about the Insight-users mailing list