[vtkusers] run time error during convert itkimage to vtkimagedata

alaa ahmed alaamegawer at yahoo.com
Mon Mar 12 07:13:11 EDT 2012



 
hi all

I'm trying to send itkimage pointer to a function then convert it to vtkimagedata but after that i 
have run time exception during display them . but if i use the same code of conversion in the function i use to open this image all things are going good . actually i want to ask what's the different between this code 

void ImageWidget::openWithITK()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath());
    
if (!fileName.isEmpty()) {
// read the image
typedef itk::ImageFileReader <ImageType> ReaderType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(fileName.toAscii().data());
reader->Update();
this->displayImage(ConvertITKImageToVtkImage(reader->GetOutput()));
}
}
void ImageWidget::displayImage(vtkImageData *image)
{
    int *dim= image->GetDimensions();        
    double *spacing = image->GetSpacing(); 
    double *origin = image->GetOrigin();  
    
    float Cx = (dim[0] * spacing[0])/2. + origin[0];
    float Cy = (dim[1] * spacing[1])/2. + origin[1];
    camera->ParallelProjectionOn();
    camera->SetFocalPoint(Cx,Cy,0);    
    camera->SetPosition(Cx,Cy,1);
    //
    //    // to flip de image
    //    camera->SetViewUp (0, 1, 0);  
    //    
    // set actor properties
    actor->SetInput(image);
actor->InterpolateOff();    

renderer->AddActor(actor);
    renderer->SetActiveCamera(camera);
    renderer->ResetCamera();

qvtkWidget->SetRenderWindow(renderWindow);

    // window interactor style for display images 
vtkSmartPointer<vtkInteractorStyleImage> style =vtkSmartPointer<vtkInteractorStyleImage>::New();
// set interactor style to the qvtkWidget Interactor
qvtkWidget->GetInteractor()->SetInteractorStyle(style);
qvtkWidget->update();
this->update();
}

vtkImageData* ImageWidget::ConvertITKImageToVtkImage(ImageType * image)
{
vtkSmartPointer <vtkImageData> vtkImageFromItkImage;
vtkConnectorType::Pointer connector = vtkConnectorType::New();
connector->GetExporter()->SetInput(image); 
connector->GetImporter()->Update();

// flip image in Y axis
vtkSmartPointer<vtkImageFlip> flipYFilter = vtkSmartPointer<vtkImageFlip>::New();
flipYFilter->SetFilteredAxis(1); // flip Y axis
flipYFilter->SetInput(connector->GetImporter()->GetOutput());
flipYFilter->Update();

// create vtk image
vtkImageFromItkImage = vtkSmartPointer<vtkImageData>::New();
vtkImageFromItkImage->DeepCopy(flipYFilter->GetOutput());
vtkImageFromItkImage->SetScalarTypeToUnsignedChar();
vtkImageFromItkImage->Update();

//reader = NULL;
flipYFilter = NULL;
connector = NULL;
return vtkImageFromItkImage;
}

and that code 

void ImageWidget::openWithITK()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath());
    
if (!fileName.isEmpty()) {
// read the image
typedef itk::ImageFileReader <ImageType> ReaderType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(fileName.toAscii().data());
reader->Update();

vtkSmartPointer <vtkImageData> vtkImageFromItkImage;
vtkConnectorType::Pointer connector = vtkConnectorType::New();
connector->GetExporter()->SetInput( reader->GetOutput() ); 
connector->GetImporter()->Update();
// flip image in Y axis
vtkSmartPointer<vtkImageFlip> flipYFilter = vtkSmartPointer<vtkImageFlip>::New();
flipYFilter->SetFilteredAxis(1); // flip Y axis
flipYFilter->SetInput(connector->GetImporter()->GetOutput());
flipYFilter->Update();
// create vtk image
vtkImageFromItkImage = vtkSmartPointer<vtkImageData>::New();
vtkImageFromItkImage->DeepCopy(flipYFilter->GetOutput());
vtkImageFromItkImage->SetScalarTypeToUnsignedChar();
vtkImageFromItkImage->Update();

displayImage (vtkImageFromItkImage );
//reader = NULL;
flipYFilter = NULL;
connector = NULL;
}
}
void ImageWidget::displayImage(vtkImageData *image)
{
    int *dim= image->GetDimensions();        
    double *spacing = image->GetSpacing(); 
    double *origin = image->GetOrigin();  
    
    float Cx = (dim[0] * spacing[0])/2. + origin[0];
    float Cy = (dim[1] * spacing[1])/2. + origin[1];
    camera->ParallelProjectionOn();
    camera->SetFocalPoint(Cx,Cy,0);    
    camera->SetPosition(Cx,Cy,1);
    //
    //    // to flip de image
    //    camera->SetViewUp (0, 1, 0);  
    //    
    // set actor properties
    actor->SetInput(image);
actor->InterpolateOff();    

renderer->AddActor(actor);
    renderer->SetActiveCamera(camera);
    renderer->ResetCamera();

qvtkWidget->SetRenderWindow(renderWindow);

    // window interactor style for display images 
vtkSmartPointer<vtkInteractorStyleImage> style =vtkSmartPointer<vtkInteractorStyleImage>::New();
// set interactor style to the qvtkWidget Interactor
qvtkWidget->GetInteractor()->SetInteractorStyle(style);
qvtkWidget->update();
this->update();
}

the second works good but the first give me run time error at display function.

keep Blessed

Regards

Alaa
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120312/d66d5d8d/attachment.htm>


More information about the vtkusers mailing list