[Insight-users] Re: image reader->Update() problem

Daniel Mace dlm19 at duke.edu
Thu Jan 11 18:02:56 EST 2007


Ashish,

The assertion errors tend to pop up (only/more often) with windows 
machines for some reason, so they are odd bugs to track down.  I could 
be wrong, but I'm not entirely sure that your image file has set its 
region settings correctly.  Just to make sure, after the line
-----------------
InputImageType::Pointer image = reader->GetOutput();
-----------------

add in a
-----------------
image->Update()
----------------

Just to make sure that the image->GetRequestedRegion() actually returns 
a valid region.  I'm pretty sure that assigning it to an image doesn't 
actually create the image regions until it is forced to update.  Had 
this code been in a ImageToImageFilter the Update() would be propagated 
down the line before the Regions and iterators were called in the 
GenerateData() method.  I don't think that is happening here.

Cheers,
Dan

Ashish Singh wrote:
> Hi Dan,
>
> I tried it with the 'for' loop that you suggested. I even tried it 
> with different set of images. It behaves the same each time, i.e. the 
> first run in the for loop goes fine, the second time, it crashes. So 
> what I did was to comment each line below the reader->Update() one by 
> one to find out if anything else is causing the error. And I found out 
> that if I comment the writer1->Update() line down below in the for 
> loop, it doesn't crash at reader->Update anymore and goes through the 
> entire for loop. But then I don't want to comment the 
> writer1->Update() line, because then I can't write the files.
> Do you know how to fix it? Has anyone else faced this problem?
>
> Thanks,
> Ashish
>
> On 1/11/07, *Daniel Mace* < dlm19 at duke.edu <mailto:dlm19 at duke.edu>> 
> wrote:
>
>     Ashish,
>
>     It could be an issue with the reader reading in that particular file.
>     Tracking down the particular location of the vector assertion
>     error can
>     be a bit painful as everything is caught by the ITK exception
>     libraries
>     and repropagated.  See what happens if you start you loop off with
>     the
>     second image.  Replace your for loop with the following and see if it
>     gets through the first iteration.
>
>     ---------------------------
>       for(fni = 1; fni<numberOfFilenames; fni++)
>     ---------------------------
>
>     If it still dies on the second loop, then we can at least
>     contribute it
>     to the code you wrote and not an error with the reader reading in a
>     particular tag/etc. for that particular image.
>
>     Cheers,
>     Dan
>
>     Ashish Singh wrote:
>     > Dan here's my complete code.It is crashing inside the loop
>     second time
>     > at the reader->Update().Can you or anyone please help me figure out
>     > what's wrong  here?
>     >
>     > Thanks,
>     > Ashish
>     > ------------
>     > #include " itkImageFileReader.h"
>     > #include "itkImageFileWriter.h"
>     > #include "itkRescaleIntensityImageFilter.h"
>     > #include "itkGDCMImageIO.h"
>     > #include "itkImageRegionConstIterator.h"
>     > #include "itkImageRegionIterator.h"
>     > #include "itkRegionOfInterestImageFilter.h"
>     > #include "itkGDCMSeriesFileNames.h"
>     > #include <list>
>     > #include <fstream>
>     > #include <vector>
>     > #include <itksys/SystemTools.hxx>
>     >
>     > using namespace std;
>     >  int main( int argc, char* argv[] )
>     >  {
>     >
>     >   typedef signed short InputPixelType;
>     >   const unsigned int   InputDimension = 2;
>     >
>     >   typedef itk::Image< InputPixelType, InputDimension >
>     InputImageType;
>     >   typedef itk::ImageFileReader< InputImageType > ReaderType;
>     >
>     >   typedef itk::GDCMImageIO           ImageIOType;
>     >   ImageIOType::Pointer gdcmImageIO = ImageIOType::New();
>     >
>     >   ReaderType::Pointer reader = ReaderType::New();
>     >
>     >   typedef itk::GDCMSeriesFileNames NamesGeneratorType;
>     >   NamesGeneratorType::Pointer nameGenerator =
>     NamesGeneratorType::New();
>     >   nameGenerator->SetInputDirectory( "D:\\testimages\\SRS00001"  );
>     >
>     >   typedef std::vector<std::string> FileNamesContainer;
>     >   FileNamesContainer fileNames = nameGenerator->GetInputFileNames();
>     >   unsigned int numberOfFilenames =  fileNames.size();
>     >   std::cout << numberOfFilenames << std::endl;
>     >   unsigned int fni;
>     >   for(fni = 0; fni<numberOfFilenames; fni++)
>     >   {
>     >     std::cout << "filename # " << fni << " = ";
>     >     std::cout << fileNames[fni] << std::endl;
>     >   }
>     >
>     >  typedef itk::Image<InputPixelType, InputDimension> InputImageType;
>     >  typedef itk::ImageRegionConstIterator< InputImageType >
>     > ConstIteratorType;
>     >  typedef itk::ImageRegionIterator< InputImageType > IteratorType;
>     >  InputImageType::Pointer image2 = InputImageType::New();
>     >  typedef itk::ImageFileWriter< InputImageType >  Writer1Type;
>     >  Writer1Type::Pointer writer1 = Writer1Type::New();
>     >
>     >   for(fni = 0; fni<numberOfFilenames; fni++)
>     >   {
>     >       cout<<"inside for loop"<<" "<<"loop # "<<fni<<endl;
>     >
>     >       reader->SetFileName( fileNames[fni] );
>     >       reader->SetImageIO( gdcmImageIO );
>     >       try
>     >         {
>     >             reader->Update();
>     >         }
>     >       catch (itk::ExceptionObject & e)
>     >         {
>     >             std::cerr << "exception in file reader " << std::endl;
>     >             std::cerr << e << std::endl;
>     >             return EXIT_FAILURE;
>     >         }
>     >
>     >      InputImageType::Pointer image = reader->GetOutput();
>     >      image2->SetRegions(image->GetRequestedRegion());
>     >      image2->CopyInformation(image);
>     >      image2->Allocate();
>     >
>     >      ConstIteratorType in1( image, image->GetRequestedRegion() );
>     >
>     >      IteratorType out( image2, image->GetRequestedRegion() );
>     >
>     >      for (in1.GoToBegin(),out.GoToBegin
>     ();!in1.IsAtEnd();++in1,++out)
>     > //copy original image to new image
>     >      {
>     >         out.Set(in1.Get());
>     >      }
>     >
>     >       if(fni==0)
>     >         writer1->SetFileName("D:\\testimages\\SRS\\IM1.dcm" );
>     >       if(fni==1)
>     >           writer1->SetFileName("D:\\testimages\\SRS\\IM2.dcm" );
>     >       writer1->SetInput( image2 );
>     >       writer1->SetImageIO(gdcmImageIO);
>     >
>     >       try
>     >         {
>     >             writer1->Update();
>     >         }
>     >       catch (itk::ExceptionObject & e)
>     >         {
>     >             std::cerr << "exception in file writer " << std::endl;
>     >             std::cerr << e << std::endl;
>     >             return EXIT_FAILURE;
>     >         }
>     >   }//for loop end
>     >     return EXIT_SUCCESS;
>     >
>     >  }
>     > --------------
>     >
>     > On 1/11/07, *Ashish Singh* <mrasingh at gmail.com
>     <mailto:mrasingh at gmail.com>
>     > <mailto:mrasingh at gmail.com <mailto:mrasingh at gmail.com>>> wrote:
>     >
>     >     Thanks Dan. I tried this. It still doesn't work.Now I get an
>     >     unhandled exception at 0x00406515 in my exe file.
>     >
>     >
>     >     On 1/11/07, *Daniel Mace* < dlm19 at duke.edu
>     <mailto:dlm19 at duke.edu>
>     >     <mailto: dlm19 at duke.edu <mailto:dlm19 at duke.edu>>> wrote:
>     >
>     >         Ashish,
>     >
>     >         I think I ran into the same problem a while ago.  I ended up
>     >         replacing
>     >         the code with the following
>     >         -----------------------------------------
>     >         const vector<string> &fileNames
>     >         =  nameGenerator->GetInputFileNames();
>     >         -----------------------------------------
>     >
>     >         and then set the file names by calling
>     >
>     >         ---------------------------------------------
>     >         reader->SetFileName( fileNames[i].c_str());
>     >         ---------------------------------------------
>     >
>     >         More of a quick fix for me (as I was returning to C++
>     after an
>     >         8 year
>     >         hiatus), and I'm sure there is something more
>     efficient.  Try
>     >         making
>     >         that change and let me know if it works.
>     >
>     >         Cheers,
>     >         Dan
>     >
>     >         Ashish Singh wrote:
>     >         > Thanks for replying Dan. Yes that's what it is,
>     >         > reader->SetFileName(fileNames[fni]).
>     >         > The fname was a typo in my previous message,sorry for
>     that.
>     >         It doesn't
>     >         > work with
>     >         > reader->SetFileName(fileNames[fni])
>     >         >
>     >         > The exact error message is a pop up box, titled Microsft
>     >         Visual C++
>     >         > debug Library. It reads-
>     >         > ---------
>     >         > Debug Assertion Failed!
>     >         > Program:...
>     >         > File: C:\program Files(x86)\Microsft Visual Studio
>     >         8\VC\include\vector
>     >         > Line: 756
>     >         >
>     >         > Expression: vector subscript out of range
>     >         >
>     >         > For information on how your program can cause assertion
>     >         failure, see
>     >         > the Visual C++ documentation on asserts.
>     >         >
>     >         > abort-retry-ignore
>     >         > --------
>     >         >
>     >         > Do you know what is going wrong here?
>     >         >
>     >         > Thanks,
>     >         > Ashish
>     >         >
>     >         > On 1/11/07, *Daniel Mace* < dlm19 at duke.edu
>     <mailto:dlm19 at duke.edu>
>     >         <mailto:dlm19 at duke.edu <mailto:dlm19 at duke.edu>> <mailto:
>     dlm19 at duke.edu <mailto:dlm19 at duke.edu>
>     >         <mailto: dlm19 at duke.edu <mailto:dlm19 at duke.edu>>>> wrote:
>     >         >
>     >         >     Ashish,
>     >         >
>     >         >     What's the exact error message that it gives
>     you?  Also,
>     >         where do you
>     >         >     set "fname".  perhaps it should be:
>     >         >     -----------
>     >         >     reader->SetFileName(fileNames[fni])
>     >         >     --------
>     >         >     unless you set fname somewhere else in the code that
>     >         isn't present.
>     >         >
>     >         >     Cheers,
>     >         >     Dan
>     >         >
>     >         >     Ashish Singh wrote:
>     >         >     > Hi,
>     >         >     >
>     >         >     > I am a newbie to ITK. I was trying to read a set of
>     >         dicom images
>     >         >     from
>     >         >     > a directory and then use reader->update() to process
>     >         each one of
>     >         >     them
>     >         >     > one after the other. But the reader->update()
>     gives me
>     >         an error
>     >         >     after
>     >         >     > reading the first image. Can anyone please tell
>     me how
>     >         to fix this
>     >         >     > problem?
>     >         >     >
>     >         >     > This is what my code looks like. It is not the
>     complete
>     >         code,
>     >         >     but only
>     >         >     > the relevant part of it.
>     >         >     > ---------------
>     >         >     >   typedef itk::GDCMSeriesFileNames
>     NamesGeneratorType;
>     >         >     >   NamesGeneratorType::Pointer nameGenerator =
>     >         >     NamesGeneratorType::New();
>     >         >     >   nameGenerator->SetInputDirectory(
>     "D:\\testimages"  );
>     >         >     >
>     >         >     >   typedef std::vector<std::string>
>     FileNamesContainer;
>     >         >     >   FileNamesContainer fileNames =
>     >         nameGenerator->GetInputFileNames();
>     >         >     >     unsigned int numberOfFilenames
>     =  fileNames.size();
>     >         >     >   std::cout << numberOfFilenames << std::endl;
>     >         >     >   unsigned int fni;
>     >         >     >
>     >         >     >   for(fni = 0; fni<numberOfFilenames; fni++)
>     >         >     >     {
>     >         >     >     std::cout << "filename # " << fni << " = ";
>     >         >     >     std::cout << fileNames[fni] << std::endl;
>     >         >     >     }
>     >         >     >
>     >         >     >   for(fni = 0; fni<numberOfFilenames; fni++)
>     >         >     >   {
>     >         >     >   reader->SetFileName( fname);
>     >         >     >
>     >         >     >   reader->SetImageIO( gdcmImageIO );
>     >         >     >
>     >         >     >   try
>     >         >     >     {
>     >         >     >         reader->Update();//this is where it
>     gives me an
>     >         error second
>     >         >     > time in the loop.
>     >         >     >     }
>     >         >     >   catch (itk::ExceptionObject & e)
>     >         >     >     {
>     >         >     >     std::cerr << "exception in file reader " <<
>     std::endl;
>     >         >     >     std::cerr << e << std::endl;
>     >         >     >     return EXIT_FAILURE;
>     >         >     >     }
>     >         >     > // code for processing the image //
>     >         >     > //code for writing the processed image//
>     >         >     > }// for loop complete
>     >         >     > --------------------------------------
>     >         >     > The filenames are correctly read, the code runs
>     fine
>     >         the first time
>     >         >     > through the for loop, but second time it gives
>     an error
>     >         at the
>     >         >     > reader->Update() line.
>     >         >     > Can anyone please tell me why is this happening
>     and how
>     >         to fix it?
>     >         >     >
>     >         >     > Thanks,
>     >         >     > Ashish
>     >         >     >
>     >         >     >
>     >         >     >
>     >         >
>     >        
>     ------------------------------------------------------------------------
>     >         >
>     >         >     >
>     >         >     > _______________________________________________
>     >         >     > Insight-users mailing list
>     >         >     > Insight-users at itk.org
>     <mailto:Insight-users at itk.org> <mailto:Insight-users at itk.org
>     <mailto:Insight-users at itk.org>>
>     >         <mailto: Insight-users at itk.org
>     <mailto:Insight-users at itk.org> <mailto:Insight-users at itk.org
>     <mailto:Insight-users at itk.org>>>
>     >         >     > http://www.itk.org/mailman/listinfo/insight-users
>     >         >     >
>     >         >
>     >         >
>     >
>     >
>     >
>
>



More information about the Insight-users mailing list