[Insight-users] reading unknown transform - how to itk::TransformFactory<TransformType>::RegisterTransform(); ???
Luis Ibanez
luis.ibanez at kitware.com
Thu Oct 22 13:56:11 EDT 2009
Hi Darren,
In principle, with a factory you only need to register
the additional transform types that you expect to read.
For example:
itk::TransformFactory<BSplineTransformType>::RegisterTransform();
itk::TransformFactory<TransformTypeA>::RegisterTransform();
itk::TransformFactory<TransformTypeB>::RegisterTransform();
itk::TransformFactory<TransformTypeC>::RegisterTransform();
...
and then read the transform file:
reader->SetFileName( filename );
reader->Update();
However, that still leaves you with the need of doing dynamic casts
on the list of transforms that is returned by the reader...
Luis
---------------------------------------
On Wed, Oct 21, 2009 at 5:17 PM, Darren Weber
<darren.weber.lists at gmail.com> wrote:
>
> A solution is to define the supported transform types as global typedefs,
> e.g.:
>
> typedef itk::AffineTransform< double, iImgDimension > AffineTransformType;
> const unsigned int SpaceDimension = iImgDimension;
> const unsigned int SplineOrder = 3;
> typedef double CoordinateRepType;
> typedef
> itk::BSplineDeformableTransform<CoordinateRepType,SpaceDimension,SplineOrder>
> BSplineTransformType;
> itk::TransformFactory<BSplineTransformType>::RegisterTransform();
>
>
> These types can be used in main() and other functions (there can be a
> function for each transform type). This is one of those functions:
>
> BSplineTransformType::Pointer getBSplineXFM(
> itk::TransformFileReader::TransformListType *transforms )
> {
> BSplineTransformType::Pointer xfm, xfmRead;
> // Then use an STL iterator on the list of transforms and apply
> // the proper casting of the resulting transform.
> itk::TransformFileReader::TransformListType::const_iterator transformIt;
> transformIt = --(transforms->end()); // get last transform in list
> if( ! strcmp(
> (*transformIt)->GetNameOfClass(),"BSplineDeformableTransform"))
> {
> xfmRead = static_cast<BSplineTransformType*>(
> (*transformIt).GetPointer() );
> xfm = dynamic_cast<BSplineTransformType*>( xfmRead.GetPointer() );
> }
> return xfm;
> }
>
>
> This is a small snippet of the main() code that chooses which function to
> call:
>
> // The transform reader is not template and therefore it returns a list
> // of transforms. However, the reader can instantiate the appropriate
> // transform class while reading the file, but it is up to the user to
> // do the appropriate cast.
> AffineTransformType::Pointer xfmAffine;
> BSplineTransformType::Pointer xfmBspline;
> if( ! affinePath.empty() )
> xfmAffine = getAffineXFM( transformReader->GetTransformList() );
> if( ! bsplinePath.empty() )
> xfmBspline = getBSplineXFM( transformReader->GetTransformList() );
>
> ...
>
>
> What would this look like using a factory method? Can the factory
> programming style be easily gleaned from the IO classes or examples?
>
> Thanks,
> Darren
>
>
>
>
>
>
> On Mon, Oct 19, 2009 at 10:48 AM, Luis Ibanez <luis.ibanez at kitware.com>
> wrote:
>>
>> Hi Darren,
>>
>> What do you call an "Unkown" transform ?
>>
>> An ITK transform of a well defined type, but whose actual type is not
>> currently
>> known to the application user ?
>
>
> Yes.
>
>
>>
>> If so, then you (as a developer) will have to implement a cascade of
>> reading attempts for all the Transform types that are supported by
>> your application.
>>
>> The factory makes easier for you to register all those potential
>> readers, and to try them in sequence.
>>
>> This is a similar situation to reading an image from a file without
>> knowing in advance what is the specific pixel type of that image.
>>
>>
>> I have to admit that the resulting code is not going to be pretty....
>>
>> However, the only way to simplify it would be to have a predefined
>> list of the transform that your application support, and then package
>> the reading process into a single function.
>>
>>
>> Regards,
>>
>>
>> Luis
>>
>>
>>
>> ---------------------------------------------------------------------------------------------
>> On Tue, Oct 13, 2009 at 6:01 PM, Darren Weber
>> <darren.weber.lists at gmail.com> wrote:
>> >
>> > With regard to reading a transform file, using itk::TransformFileReader,
>> > given the example:
>> > examples/IO/TransformReadWrite.cxx
>> >
>> > This example uses the following code to register and read a known
>> > BSplineTransformType, i.e.:
>> >
>> > typedef itk::BSplineDeformableTransform<double,3,5>
>> > BSplineTransformType;
>> > ...
>> > itk::TransformFileReader::Pointer reader;
>> > reader = itk::TransformFileReader::New();
>> > // Some transforms (like the BSpline transform) might not be registered
>> > // with the factory so we add them manually.
>> > itk::TransformFactory<BSplineTransformType>::RegisterTransform();
>> > reader->SetFileName( "Transforms.meta" );
>> > try
>> > {
>> > reader->Update();
>> > }
>> > catch( itk::ExceptionObject & excp )
>> > {
>> > std::cerr << "Error while reading the transform file" << std::endl;
>> > std::cerr << excp << std::endl;
>> > std::cerr << "[FAILED]" << std::endl;
>> > return EXIT_FAILURE;
>> > }
>> >
>> >
>> > Now let's assume a transform file contains an unknown transform type.
>> >
>> > What is the role of the transform factory? Is it required to read a
>> > transform file? Is it possible to read a unknown transform file and
>> > register a given transform type with the transform factory after reading
>> > the
>> > file?
>> >
>> > Suppose the transform file contains a BSplineTransformType, but the
>> > parameters are unknown. Is it possible to read this transform file?
>> >
>> > TIA and take care,
>> > Darren
>> >
>> >
>> > _____________________________________
>> > Powered by www.kitware.com
>> >
>> > Visit other Kitware open-source projects at
>> > http://www.kitware.com/opensource/opensource.html
>> >
>> > Please keep messages on-topic and check the ITK FAQ at:
>> > http://www.itk.org/Wiki/ITK_FAQ
>> >
>> > Follow this link to subscribe/unsubscribe:
>> > http://www.itk.org/mailman/listinfo/insight-users
>> >
>> >
>
>
More information about the Insight-users
mailing list