[vtkusers] VTK memory control while importing data

NIE JUNXIAO njx937 at gmail.com
Mon May 25 23:21:54 EDT 2015


Hello everyone,
      I am now trying to render some models in front of an active 
background image referring to the BackgroundImage sample. But I think I 
have came up with some memory leak problem.
http://www.vtk.org/Wiki/VTK/Examples/Cxx/Images/BackgroundImage

I create a model renderer and a background renderer, and they are both 
added to a renderwindow.
When I want to change my background, I create new vtkImageData and new 
vtkImageImport(to import a cv::mat).
Then I create a new vtkImageactor and add it to backgroundrenderer.
I can see the image after finishing this but I notice that the memory 
increases very rapidly.
I think that it is because I didnot release memory correctly.
Could anybody help me with this memory problem.
Thank you very much.
Best regards,
NIE JUNXIAO

void MyGlkview::setupBG(cv::Mat _img){
//import image
     assert(_img.data != NULL);
     vtkImageData *imgData = vtkImageData::New();
     vtkImageImport *importer = vtkImageImport::New();
     importer->SetOutput(imgData);
     importer->SetDataSpacing(1, 1, 1);
     importer->SetDataOrigin(0, 0, 0);
     importer->SetWholeExtent(0, _img.size().width - 1, 0,
         _img.size().height - 1, 0, 0);
     importer->SetDataExtentToWholeExtent();
     importer->SetDataScalarTypeToUnsignedChar();
     importer->SetNumberOfScalarComponents(_img.channels());
     importer->SetImportVoidPointer(_img.data);
     importer->Update();
     double origin[3];
     double spacing[3];
     int extent[6];
     imgData->GetOrigin(origin);
     imgData->GetSpacing(spacing);
     imgData->GetExtent(extent);
//resize image to fit window
     vtkCamera* camera = myBackgroundRenderer->GetActiveCamera();
     camera->ParallelProjectionOn();
     double xc = origin[0] + 0.5*(extent[0] + extent[1])*spacing[0];
     double yc = origin[1] + 0.5*(extent[2] + extent[3])*spacing[1];
     //double xd = (extent[1] - extent[0] + 1)*spacing[0];
     double yd = (extent[3] - extent[2] + 1)*spacing[1];
     double d = camera->GetDistance();
     camera->SetParallelScale(0.5*yd);
     camera->SetFocalPoint(xc, yc, 0.0);
     camera->SetPosition(xc, yc, d);
//here I want to avoid multi-imageactor in the renderer, but the actors 
number is always 0
//so there is always a new actor is created.
     if (myBackgroundRenderer->GetActors()->GetNumberOfItems() < 1){
         vtkImageActor *imageActor = vtkImageActor::New();
         imageActor->SetInputData(imgData);
         myBackgroundRenderer->AddActor(imageActor);
         imageActor->Update();
     }else{
         vtkImageActor *imageActor = (vtkImageActor 
*)myBackgroundRenderer->GetActors()->GetLastActor();
         //myBackgroundRenderer->
         imageActor->SetInputData(imgData);
         imageActor->Update();
     }
     // Render again to set the correct view
     refresh();
     imgData->Delete();
     importer->Delete();
}


More information about the vtkusers mailing list