Quantcast

Extracting cells from vtkImageData

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Extracting cells from vtkImageData

majinsaha
Hello!

I am trying to extract a certain number of cells from vtkImageData object and make them a separate actor. If I had vtkPolyData instead, that would be easy to do as follows:


vtkSmartPointer<vtkIdTypeArray> ids = vtkSmartPointer<vtkIdTypeArray>::New();
ids->SetNumberOfComponents(1);

// TODO: Filling ids with the cell indices of interest

vtkSmartPointer<vtkSelectionNode> selectionNode = vtkSmartPointer<vtkSelectionNode>::New();
selectionNode->SetFieldType(vtkSelectionNode::CELL);
selectionNode->SetContentType(vtkSelectionNode::INDICES);
selectionNode->SetSelectionList(ids);

vtkSmartPointer<vtkSelection> selection = vtkSmartPointer<vtkSelection>::New();
selection->AddNode(selectionNode);

vtkSmartPointer<vtkExtractSelectedPolyDataIds> extractSelection = vtkSmartPointer<vtkExtractSelectedPolyDataIds>::New();
extractSelection->SetInputData(0, myPolyData);
extractSelection->SetInputData(1, selection);
extractSelection->Update();

vtkSmartPointer<vtkPolyDataMapper> cellsMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
cellsMapper->SetInputData(extractSelection->GetOutput());

Is there a way to do it similarly for vtkImageData?

If not, I assume I should then transform vtkImageData to vtkPolyData to use all the shown benefits, right? However, what is a nice and painless way to do this transformation? I know there is ImageDataGeometryFilter that's supposed to do it, but I've only seen how it works for sources such as here:


vtkSmartPointer<vtkImageCanvasSource2D> source1 =
    vtkSmartPointer<vtkImageCanvasSource2D>::New();
  // TODO: DO SOMETHING WITH SOURCE

  // Convert the image to a polydata
  vtkSmartPointer<vtkImageDataGeometryFilter> imageDataGeometryFilter =
    vtkSmartPointer<vtkImageDataGeometryFilter>::New();
  imageDataGeometryFilter->SetInputConnection(source1->GetOutputPort());
  imageDataGeometryFilter->Update();


Is there a way to attach vtkImageData itself to a pipeline like this instead of a source? It doesn't have GetOutputPort() method so I can't seem to be able to use this approach.

The answers to both questions are greatly appreciated!

P.S. I realize I can do this in brute-force way by remaking entire vtkImageData from the selected cells, but that would take a long time. Don't want to reinvent the wheel here.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Extracting cells from vtkImageData

Bill Lorensen
Yes, use
SetInputData

On Mar 29, 2017 6:43 PM, "majinsaha" <[hidden email]> wrote:
Hello!

I am trying to extract a certain number of cells from vtkImageData object
and make them a separate actor. If I had vtkPolyData instead, that would be
easy to do as follows:

vtkSmartPointer<vtkIdTypeArray> ids =
vtkSmartPointer<vtkIdTypeArray>::New();
ids->SetNumberOfComponents(1);

// TODO: Filling ids with the cell indices of interest

vtkSmartPointer<vtkSelectionNode> selectionNode =
vtkSmartPointer<vtkSelectionNode>::New();
selectionNode->SetFieldType(vtkSelectionNode::CELL);
selectionNode->SetContentType(vtkSelectionNode::INDICES);
selectionNode->SetSelectionList(ids);

vtkSmartPointer<vtkSelection> selection =
vtkSmartPointer<vtkSelection>::New();
selection->AddNode(selectionNode);

vtkSmartPointer<vtkExtractSelectedPolyDataIds> extractSelection =
vtkSmartPointer<vtkExtractSelectedPolyDataIds>::New();
extractSelection->SetInputData(0, myPolyData);
extractSelection->SetInputData(1, selection);
extractSelection->Update();

vtkSmartPointer<vtkPolyDataMapper> cellsMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
cellsMapper->SetInputData(extractSelection->GetOutput());

Is there a way to do it similarly for vtkImageData?

If not, I assume I should then transform vtkImageData to vtkPolyData to use
all the shown benefits, right? However, what is a nice and painless way to
do this transformation? I know there is ImageDataGeometryFilter that's
supposed to do it, but I've only seen how it works for sources such as here:


vtkSmartPointer<vtkImageCanvasSource2D> source1 =
    vtkSmartPointer<vtkImageCanvasSource2D>::New();
  // TODO: DO SOMETHING WITH SOURCE

  // Convert the image to a polydata
  vtkSmartPointer<vtkImageDataGeometryFilter> imageDataGeometryFilter =
    vtkSmartPointer<vtkImageDataGeometryFilter>::New();
  imageDataGeometryFilter->SetInputConnection(source1->GetOutputPort());
  imageDataGeometryFilter->Update();


Is there a way to attach vtkImageData itself to a pipeline like this instead
of a source? It doesn't have GetOutputPort() method so I can't seem to be
able to use this approach.

The answers to both questions are greatly appreciated!

P.S. I realize I can do this in brute-force way by remaking entire
vtkImageData from the selected cells, but that would take a long time. Don't
want to reinvent the wheel here.



--
View this message in context: http://vtk.1045678.n5.nabble.com/Extracting-cells-from-vtkImageData-tp5742666.html
Sent from the VTK - 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

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/vtkusers

_______________________________________________
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 VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/vtkusers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Extracting cells from vtkImageData

majinsaha
Thanks.
The following seems to be producing no errors:


                vtkSmartPointer<vtkImageDataGeometryFilter> imageDataGeometryFilter =
                        vtkSmartPointer<vtkImageDataGeometryFilter>::New();
                imageDataGeometryFilter->SetInputData(myImageData);
                imageDataGeometryFilter->Update();

                vtkSmartPointer<vtkExtractSelectedPolyDataIds> extractSelection =   vtkSmartPointer<vtkExtractSelectedPolyDataIds>::New();

                extractSelection->SetInputData(0, imageDataGeometryFilter->GetOutput());
                extractSelection->SetInputData(1, selection);

                extractSelection->Update();


However, this only gives what I want if my vtkImageData is plane, i.e. has no third dimension. For a general 3D vtkImageData, the result of the geometry filter is just a bunch of vertices. I checked that with


                std::cout<< extractSelection->GetOutput()->GetNumberOfVerts()<<std::endl;
                std::cout<< extractSelection->GetOutput()->GetNumberOfLines()<<std::endl;
                std::cout<< extractSelection->GetOutput()->GetNumberOfPolys()<<std::endl;
                std::cout<< extractSelection->GetOutput()->GetNumberOfStrips()<<std::endl;
</h3>
and the output was that only the first out of four was non-zero. It just ignores everything except vertices while doing the conversion in 3D.
Is there currently a working strategy in VTK to convert 3D vtkImageData to vtkPolyData by keeping track of quad cells, for example?

Loading...