[Insight-users] dynamic_cast

Luis Ibanez luis.ibanez@kitware.com
Thu, 19 Dec 2002 14:35:22 -0500


Hi Lydia,

Again,...
your code is working for me.

I added a fake buffer for the labeled image
and the program compiles and runs ok.

I would suspect of the method you are currently
using for allocating the buffer of the labeled
image.  Are you obtaining this buffer from some
in-house code ?

Note that the size of the image must match exactly
the size of the buffer.

Remember also that the "Update()" method in Image
should not be called.

The output of the Connected filter will be
obtained in the form of an itk::Image with
the method "filter->GetOutput()".

I tried your code with Visual C++ 6.0 and didn't
have any problem with the filter->Update() method
or the image->SetOrigin().


What version of ITK are you using ?
What system (OS + compiler ) ?


    Please let us know.


     Thanks

       Luis


Here is the code I ran with the minor modifications.
------------------------------------------------

#include "itkImage.h"
#include "itkConnectedThresholdImageFilter.h"

int main ()
{

    int size[2];
    size[0] = 256;
    size[1] = 256;

    unsigned char * labeled_image2 = new unsigned char[ size[0] * size[1] ];

itk::ImportImageContainer<long unsigned int, unsigned char>::Pointer import;
    import = itk::ImportImageContainer<long unsigned int,   unsigned 
char>::New();
    import->Initialize();
    import->Reserve(size[0]*size[1]);
    import->SetImportPointer(labeled_image2,size[0]*size[1],false);

/****setting the pixel values in the itkimage****/

typedef itk::Image<unsigned char, 2> ImageType;
    ImageType:: Pointer img = ImageType::New();

    double values[]={2,2};
    double origin_x= 0;
    double origin_y=0;
    double origin[] = {origin_x, origin_y};
    ImageType::IndexType  index;
     img->SetOrigin(origin);
    img->SetSpacing(values);

    ImageType::SizeType         imagesize;
    imagesize[0] = 256;
    imagesize[1] = 256;

    ImageType::RegionType region;
    region.SetSize( imagesize );

    img->SetLargestPossibleRegion( region );
    img->SetBufferedRegion( region );
    img->SetRequestedRegion(region);
    img->Allocate();
     img->SetPixelContainer(import);
    img->Update();

/******** connectedThresholdImageFilter***********/

typedef itk::ConnectedThresholdImageFilter<ImageType,ImageType> FilterType;

  FilterType::Pointer filter = FilterType::New();
     filter->SetInput(img);
     FilterType::IndexType seed; seed[0] = 165; seed[1] = 90;
     filter->SetSeed(seed);
     filter->SetLower(0);
     filter->SetUpper(255);
     filter->SetReplaceValue(255);
     filter-> Update();

  ImageType::Pointer outputImage = filter->GetOutput();

  return 0;

}

------------------------------------------------


lydia coin wrote:
> /*** this is for importing pixel values from an unsigned char type 
> pointer   **/
> 
> itk::ImportImageContainer<long unsigned int, unsigned char>::Pointer import;
>    import = itk::ImportImageContainer<long unsigned int,   unsigned 
> char>::New();
>    import->Initialize();
>    import->Reserve(size[0]*size[1]);
>    import->SetImportPointer(labeled_image2,size[0]*size[1],false);
> 
> /****setting the pixel values in the itkimage****/ 
> 
> typedef itk::Image<unsigned char, 2> ImageType;
>    ImageType:: Pointer img = ImageType::New();
> 
>    double values[]={2,2};
>    double origin_x= 0;
>    double origin_y=0;
>    double origin[] = {origin_x, origin_y};
>    ImageType::IndexType  index;
>     img->SetOrigin(origin);
>    img->SetSpacing(values); // here I have already a problem at 
> RebuildTransform() method
> 
>    ImageType::SizeType         imagesize;
>    imagesize[0] = 256;
>    imagesize[1] = 256;
>    
>    ImageType::RegionType region;
>    region.SetSize( imagesize );
>    
>    img->SetLargestPossibleRegion( region );
>    img->SetBufferedRegion( region );
>    img->SetRequestedRegion(region);
>    img->Allocate();
>     img->SetPixelContainer(import);
>    img->Update();
> 
> /******** connectedThresholdImageFilter***********/
> 
> typedef itk::ConnectedThresholdImageFilter<ImageType,ImageType> FilterType;
> 
>  FilterType::Pointer filter = FilterType::New();
>     filter->SetInput(img);
>     FilterType::IndexType seed; seed[0] = 165; seed[1] = 90;
>     filter->SetSeed(seed);
>     filter->SetLower(0);
>     filter->SetUpper(255);
>     filter->SetReplaceValue(255);
>  filter-> Update();// here iot doesn't work because of the dynamic-cast 
> of copyInformation method.
> 
> thanks for your help
> 
>  */"Lorensen, William E (Research)" <lorensen@crd.ge.com>/* wrote:
> 
>     lydia,
> 
>     What is your ImageType? Could you report your typedef?
> 
>      
> 
>     Bill
> 
>         -----Original Message-----
>         *From:* lydia coin [mailto:lydia_coin3d@yahoo.fr]
>         *Sent:* Wednesday, December 18, 2002 6:27 AM
>         *To:* insight-users@public.kitware.com
>         *Subject:* [Insight-users] dynamic_cast
> 
>         Hi folks
> 
>         I'm a newbie at itk, I'm having many problems in using the
>         filters, I already posted a message about setspacing and
>         setorigin methods of an itkimage class. It doesn't work because
>         it says that the transfroms associated to this image are not
>         affine knowing that I associated no transform, it was created by
>         the class itself during the instanciation.
> 
>         Second, I'm using a connectedthresholdImageFilter , it takes as
>         input an image of unsigned char type of pixels, but doesn't give
>         any output. While using update() method of the filter it stops
>         with an error at this point of the itkImagebase.txx
> 
>         ImageBase<VImageDimension>
>         ::CopyInformation(const DataObject *data)
>         {
> 
>         imgData = dynamic_cast<const ImageBase<VImageDimension>*>(data);
> 
>         }
> 
>         and doesn't cast back the pointer.  
> 
>         I really need to know what I'm doing wrong and hope I will get
>         some help
> 
>         regards    
> 
>          
> 
>         typedef itk::ConnectedThresholdImageFilter<ImageType,ImageType>
>         FilterType;
> 
>          FilterType::Pointer filter = FilterType::New();
>             filter->SetInput(img);
>             FilterType::IndexType seed; seed[0] = 165; seed[1] = 90;
>             filter->SetSeed(seed);
>             filter->SetLower(0);
>             filter->SetUpper(255);
>             filter->SetReplaceValue(255);
>            filter-> Update();
> 
> 
>         ------------------------------------------------------------------------
>         Do You Yahoo!? -- Une adresse @yahoo.fr gratuite et en français !
>         Testez le nouveau Yahoo! Mail <http://fr.mail.yahoo.com/>
> 
> 
> ------------------------------------------------------------------------
> Do You Yahoo!? -- Une adresse @yahoo.fr gratuite et en français !
> Testez le nouveau Yahoo! Mail <http://fr.mail.yahoo.com>