[Insight-users] Problem with passing images to functions

Sergio Vera sergio.vera at alma3d.com
Tue Aug 10 03:44:08 EDT 2010


This is a digest from the original post from Luis Ibañez:

>
> 1) When writing functions that take an image as an argument,

    we usually use a Raw pointer instead of a Smart Pointer.


>     The reason is that SmartPointers will not do polymorphism.


>     That is, if you use smart pointers, you can call that function

    later with an argument that is a derived class of that image

    type.


    If you look at most ITK filters, you will find that raw pointers

    are used in the API, for passing and receiving images.


>     However, when we call those functions we pass a SmartPointer

    to them (since SmartPointers know how to cast themselves as

    raw pointers).



2) The only case in which you may want to pass an image

    SmartPointer as argument to a function is when you are

    creating that image inside the function.

    In that case, passing the SmartPointer by reference is

    a reasonable choice.



3) Please note that the construction


>           const    Image::ConstPointer &  ptr ....;


>     prevents the internal mechanisms of the SmartPointer

    from working, since the "const" keyword prevents the "ptr"

    variable from changing. (strictly speaking it prevents the

    smart pointer from calling the non-const method Register()

    on the object that it points to).



And then examples of usage when a Image is an input to a function

     template<typename TITKImgType>
     bool
     WriteFile(
         const std::string& a_fullFilename,
         const TITKImgType * );


and you will be able to call it as

    ImgType::Pointer image = filter->GetOutput();

    WriteFile<ImgType>("out.mhd", image );

Notice how the function is defined to receive a raw pointer but when you
call it we pass a smartpointer.

Regards


On Mon, Aug 9, 2010 at 9:42 PM, habibbaluwala2010
<habibbaluwala at gmail.com>wrote:

>
> Hello Sergio,
>                 Can you give me an example of how to use this in context
> with the syntax? I am really thankful for the help.
>
>
> Kind regards,
>
> Habib Baluwala
>
>
>
>
> Sergio Vera wrote:
> >
> > Following advices from Luis Ibañez in a previous mail of the list, we now
> > use the following rules:
> >
> > const ITKImgType *ImgIn  when images are input to a method and
> >
> > ITKImgType::Pointer &ImgOut  when the image will be created inside the
> > function
> >
> > regards
> >
> > 2010/8/6 Dženan Zukić <dzenanz at gmail.com>
> >
> >> Use ImageType::Pointer. Here is an example:
> >>
> >> void calc2DJointHistogram(VisualizingImageType::Pointer x,
> >> VisualizingImageType::Pointer y, std::string savefilename)
> >> {
> >> ...
> >> }
> >>
> >> //calling it
> >> VisualizingImageType::Pointer lVis, hVis;
> >> ...
> >> hVis=hReader->GetOutput();
> >> calc2DJointHistogram(lVis, hVis, fnNoExt+"_LH.png");
> >>
> >> Dženan
> >>
> >> On Fri, Aug 6, 2010 at 15:32, habibbaluwala2010
> >> <habibbaluwala at gmail.com>wrote:
> >>
> >>>
> >>> Hi Everyone,
> >>>                 I am a biot confused on how can i pass an image with
> >>> smart
> >>> pointers to an outside function . I basically need this to avoid
> >>> reproducing
> >>> the code again and again and also to avoid composite filters. It would
> >>> be
> >>> very helpful if you can provide an example which does the job!!!
> Anyways
> >>> looking forward to your replies.
> >>> --
> >>> View this message in context:
> >>>
> http://old.nabble.com/Problem-with-passing-images-to-functions-tp29356451p29356451.html
> >>> Sent from the ITK - Users mailing list archive at Nabble.com.
> >>>
> >>> _____________________________________
> >>> Powered by www.kitware.com
> >>>
> >>> Visit other Kitware open-source projects at
> >>> http://www.kitware.com/opensource/opensource.html
> >>>
> >>> Kitware offers ITK Training Courses, for more information visit:
> >>> http://www.kitware.com/products/protraining.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
> >>>
> >>
> >>
> >> _____________________________________
> >> Powered by www.kitware.com
> >>
> >> Visit other Kitware open-source projects at
> >> http://www.kitware.com/opensource/opensource.html
> >>
> >> Kitware offers ITK Training Courses, for more information visit:
> >> http://www.kitware.com/products/protraining.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
> >>
> >>
> >
> >
> > --
> > Sergio Vera
> >
> >  Alma IT Systems
> >  C/ Vilana, 4B, 4º 1ª
> >  08022 Barcelona
> >  T. (+34) 932 380 592
> >  www.alma3d.com
> >
> > _____________________________________
> > Powered by www.kitware.com
> >
> > Visit other Kitware open-source projects at
> > http://www.kitware.com/opensource/opensource.html
> >
> > Kitware offers ITK Training Courses, for more information visit:
> > http://www.kitware.com/products/protraining.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
> >
> >
>
> --
> View this message in context:
> http://old.nabble.com/Problem-with-passing-images-to-functions-tp29356451p29391440.html
> Sent from the ITK - Users mailing list archive at Nabble.com.
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.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
>



-- 
Sergio Vera

 Alma IT Systems
 C/ Vilana, 4B, 4º 1ª
 08022 Barcelona
 T. (+34) 932 380 592
 www.alma3d.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20100810/9559f845/attachment-0001.htm>


More information about the Insight-users mailing list