[Insight-users] Extract image in a loop, I don't understand why it does not work

Jim Miller millerjv at gmail.com
Mon Jun 5 19:12:40 EDT 2006


Robert,

You can call UpdateLargestPossibleRegion() on an entire pipeline.
UpdateLargestPossibleRegion() forces the pipeline to act like as if it were
being executed for the first time.

You need to call UpdateLargestPossibleRegion() (or explictly set an
RequestedRegion) whenever a change in filter parameters may result
in a cached region to fall outside the "new" LargestPossibleRegion.
There are only a few instances where this can happen. Filters like
ExtractImageFilter, filters like ShrinkImageFilter, etc.

The RegionOfInterestImageFilter may not exhibit this issue because
of the differences between it and the ExtractImageFilter.

ExtractImageFilter maintains the "index" relationship between the input
and output image.  Thus, the StartIndex of the output image is exactly
the index specified as the extraction region.  A side effect of this is that
the origin is unchanged.

RegionOfInterestImageFilter shifts the "index" relationship between the
input
and output image.  Here, the StartIndex  of the output image is always
[0,0,...].
As a side effect, the origin of the output is also modified.

The output of the ExtractImageFilter is always pixel aligned with the input
image.  The output of the RegionOfInterestImageFilter is always aligned
in physical space (say mm) with the input image.

Jim


On 6/5/06, Atwood, Robert C <r.atwood at imperial.ac.uk> wrote:
>
> Jim:
>
> I take it  you mean when updating the ExtractImageFilter itself ? What
> about when the filter is part of a pipeline, can you call
> UpdateLargestPossibleRegion() on the end of the pipeline or do you need
> to explicity call it on the ExtractImageFilter? The example I sent was
> minimal to illustrate my problem, in the end I want to apply some other
> filters in a pipeline. The example on p277 of the software guide does
> not call this method, but the filter is part of a pipeline ... And it is
> not in a loop, or used more than once!
>
> Is this the case for RegionOfInterestImageFilter? I tried that since
> posting and it does not seem to generate such an exception for my
> program. Is that just luck? Should I use UpdateLargestPossibleRegion()
> in this case?
>
>
> Thanks again!
>
> Robert
>
> -----Original Message-----
> From: Jim Miller [mailto:millerjv at gmail.com]
> Sent: 05 June 2006 21:25
> To: Atwood, Robert C
> Cc: Insight Users
> Subject: Re: [Insight-users] Extract image in a loop, I don't understand
> why it does not work
>
> Robert,
>
> When you update the ExtractImageFilter, you should call
> UpdateLargestPossibleRegion() instead of
> calling Update(). When a region is changed, there is a possibility that
> the data cached in pipeline
> may not be valid.  This is what the exception is reporting.  A call to
> UpdateLargestPossibleRegion()
> tells the pipeline to ignore any cached region sizes.
>
> Jim
>
>
>
>
> On 6/5/06, Atwood, Robert C <r.atwood at imperial.ac.uk > wrote:
>
>         Hi,
>         I am trying to obtain the statistics for some specified regions
> of an
>         image (eventualy each of several images)
>         I seem to be getting an exception thrown the second time I try
> to
>         extract a region. Is there something that needs to be done to
> reset the
>         extractImagefilter?
>
>         In this case I defined the pixel type to unsigned short and the
> input
>         image is 1015x512. With this example the size of the region is
> 20x20 and
>         the index starts at 0,200 and increases by 5,0 until 20,200 so
> there
>         should be lots of room in the image for the region.
>
>
>
>         Thanks,
>         Robert
>
>         *****Some output ********************
>
>         ...reading
>         Region One: ImageRegion (0x7fbfffde70)
>           Dimension: 2
>           Index: [0, 0]
>           Size: [1015, 512]
>
>         Region Two: ImageRegion (0x602118)
>           Dimension: 2
>           Index: [0, 0]
>           Size: [1015, 512]
>
>         Region Three: ImageRegion (0x7fbfffde70)
>           Dimension: 2
>           Index: [0, 200]
>           Size: [20, 20]
>
>         Region Four: ImageRegion (0x602118)
>           Dimension: 2
>           Index: [0, 0]
>           Size: [1015, 512]
>
>         Sample mean = [18226.6]
>         Region Two: ImageRegion (0x602118)
>           Dimension: 2
>           Index: [0, 0]
>           Size: [1015, 512]
>
>         Region Three: ImageRegion (0x7fbfffde70)
>           Dimension: 2
>           Index: [5, 200]
>           Size: [20, 20]
>
>         Exception caught chopper update !
>
>         itk::InvalidRequestedRegionError (0x614500)
>         Location: "virtual void
> itk::DataObject::PropagateRequestedRegion()"
>         File: /sources/local/ITK_cvs/Code/Common/itkDataObject.cxx
>         Line: 397
>         Description: Requested region is (at least partially) outside
> the
>         largest possible region.
>
>
>         Region Four: ImageRegion (0x602118)
>           Dimension: 2
>           Index: [0, 0]
>           Size: [1015, 512]
>
>         Sample mean = [18226.6]
>
>
>
>
>         ****************** The Code
> *************************************
>
>         #include <stdio.h>
>
>         #include " itkImage.h"
>         #include "itkImageFileReader.h"
>         #include "itkScalarImageToListAdaptor.h"
>         #include "itkScalarToArrayCastImageFilter.h"
>         #include "itkMeanCalculator.h"
>         #include "itkExtractImageFilter.h"
>         #include "itkRegionOfInterestImageFilter.h"
>
>         #ifdef T_FLOAT
>           typedef float PixelType;
>         #elif defined T_BYTE
>           typedef unsigned char PixelType;
>         #elif defined T_USHORT
>           typedef unsigned short PixelType;
>         #elif defined T_UINT
>           typedef u_int32_t PixelType;
>         #else
>         #warning NO TYPE DEFINED, defaulting to FLOAT
>           typedef float PixelType;
>         #endif
>
>
>           typedef itk::Image< PixelType, 2 > ImageType;
>
>           // definitions for writing the image
>           typedef itk::ImageFileReader<ImageType> ReaderType;
>           typedef itk::FixedArray< PixelType, 1 > MeasurementVectorType;
>           typedef itk::Image< MeasurementVectorType, 2 > ArrayImageType;
>
>           typedef itk::Statistics::ScalarImageToListAdaptor< ImageType >
>         SampleType;
>           typedef itk::Statistics::MeanCalculator< SampleType >
>         MeanAlgorithmType;
>           typedef itk::ExtractImageFilter<ImageType,ImageType>
> ChopperType;
>         /*********************************/
>         /* beginning of MAIN routine     */
>         /*********************************/
>         int main(int argc, char ** argv) {
>           int i,j;
>
>           /* ITK objects needed for processing the volume  */
>           ReaderType::Pointer reader = ReaderType::New();
>           ImageType::Pointer inputImage ;
>
>           ChopperType::Pointer chopper = ChopperType::New();
>           SampleType::Pointer sample = SampleType::New();
>           MeanAlgorithmType::Pointer meanAlgorithm =
> MeanAlgorithmType::New();
>
>              ImageType::RegionType inputRegion;
>              ImageType::SizeType inputRegionSize;
>              ImageType::IndexType inputRegionIndex;
>
>           if (argc != 2){
>               printf("Little program to use itk to calculate means of
> image
>         area\n");
>               printf("Usage: %s input.xxx \n",argv[0]);
>               printf("where xxx is an ITK registered file type
> extension\n");
>               return(0);
>           }
>
>           /* Read in the  image */
>           printf("...reading\n");
>           reader->SetFileName(argv[1]);
>           inputImage=reader->GetOutput();
>           try {
>              inputImage->Update();
>           }catch( itk::ExceptionObject & exp ) {
>              std::cerr << "Exception caught inputImage update !" <<
> std::endl;
>              std::cerr << exp << std::endl;
>           }
>           inputImage->DisconnectPipeline();
>
>           /* select the specified region to get the statistics */
>           inputRegion = inputImage->GetLargestPossibleRegion ();
>           inputRegionSize = inputRegion.GetSize ();
>           inputRegionIndex = inputRegion.GetIndex ();
>
>           /* debugging output */
>           std::cout << "Region One: " << inputRegion << std::endl;
>
>           /* loop through regions across the sample */
>           chopper->SetInput(inputImage);
>           for(i=0;i<20;i+=5){
>           std::cout << "**********************************" <<
> std::endl;
>           std::cout << "* Region    " << i << "*********************" <<
>
>         std::endl;
>           std::cout << "**********************************" <<
> std::endl;
>              inputRegionIndex[0]=i;
>              inputRegionIndex[1]=200;
>              inputRegionSize[0]=20;
>              inputRegionSize[1]=20;
>              inputRegion.SetSize (inputRegionSize);
>              inputRegion.SetIndex (inputRegionIndex);
>
>              chopper->SetExtractionRegion (inputRegion);
>
>           /* debugging output */
>              std::cout << "Region Two: " <<
>         inputImage->GetLargestPossibleRegion() << std::endl;
>              std::cout << "Region Three: " << inputRegion << std::endl;
>              //std::cout << chopper << std::endl;
>
>              try {
>                 chopper->Update();
>              }catch( itk::ExceptionObject & exp ) {
>                 std::cerr << "Exception caught chopper update !" <<
> std::endl;
>                 std::cerr << exp << std::endl;
>              }
>           /* debugging output */
>              std::cout << "Region Four: " <<
>         chopper->GetInput()->GetLargestPossibleRegion() << std::endl;
>
>              /* pipe the image to the statistics package */
>              sample->SetImage(chopper->GetOutput());
>              //sample->SetImage(inputImage);
>              meanAlgorithm->SetInputSample( sample);
>
>              /* get the mean greylevel */
>              try {
>                 meanAlgorithm->Update();
>              }catch( itk::ExceptionObject & exp ) {
>                 std::cerr << "Exception caught subImage update !" <<
> std::endl;
>                 std::cerr << exp << std::endl;
>              }
>              std::cout << "Sample mean = " <<
> *(meanAlgorithm->GetOutput()) <<
>         std::endl;
>           }
>           printf("All done\n");
>           return (0);
>         }
>
>         _______________________________________________
>         Insight-users mailing list
>         Insight-users at itk.org
>         http://www.itk.org/mailman/listinfo/insight-users
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://public.kitware.com/pipermail/insight-users/attachments/20060605/1ce1c746/attachment.htm


More information about the Insight-users mailing list