[Insight-users] Re: Writing 3D image from text file?
Luis Ibanez
luis.ibanez at kitware.com
Fri Dec 29 17:40:43 EST 2006
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