[vtkusers] VTKpolydata (2D polygon) to VTKimagedata
Neda
neda.marvasti at gmail.com
Wed Jul 4 19:03:44 EDT 2012
Dear all,
I searched a lot in the google but I could not solve my problem. I hope some
one helps me here.
what I want to do is creating the corresponding binary image of a polygon
(vtkPolydata) in a 2D image.
To do this I found a function called "vtkPolyDataToImageStencil" but it
gives me a totally black image.
I do not know what is wrong in my code (I attached my code here)
I will appreciate if some one tells me how can I solve this problem.
Did I choose a correct function to do this?
Regards,
Neda
/**************************************************************************************************************
#include <vtkVersion.h>
#include <vtkSmartPointer.h>
#include <vtkPolyData.h>
#include <vtkImageData.h>
#include <vtkSphereSource.h>
#include <vtkMetaImageWriter.h>
#include <vtkPolyDataToImageStencil.h>
#include <vtkImageStencil.h>
#include <vtkPointData.h>
#include <vtkCutter.h>
#include <vtkPlane.h>
#include <vtkStripper.h>
#include <vtkLinearExtrusionFilter.h>
#include <vtkXMLPolyDataWriter.h>
#include <vtkPolygon.h>
#include <vtkCellArray.h>
int main(int, char *[])
{
// Setup four points
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
points->InsertNextPoint(10.0, 10.0, 0.0);
points->InsertNextPoint(10.0, 50.0, 0.0);
points->InsertNextPoint(30.0, 10.0, 0.0);
points->InsertNextPoint(30.0, 20.0, 0.0);
// Create the polygon
vtkSmartPointer<vtkPolygon> polygon =
vtkSmartPointer<vtkPolygon>::New();
polygon->GetPointIds()->SetNumberOfIds(4); //make a quad
polygon->GetPointIds()->SetId(0, 0);
polygon->GetPointIds()->SetId(1, 1);
polygon->GetPointIds()->SetId(2, 2);
polygon->GetPointIds()->SetId(3, 3);
// Add the polygon to a list of polygons
vtkSmartPointer<vtkCellArray> polygons =
vtkSmartPointer<vtkCellArray>::New();
polygons->InsertNextCell(polygon);
// Create a PolyData
vtkSmartPointer<vtkPolyData> polygonPolyData
=vtkSmartPointer<vtkPolyData>::New();
polygonPolyData->SetPoints(points);
polygonPolyData->SetPolys(polygons);
vtkSmartPointer<vtkStripper> stripper =
vtkSmartPointer<vtkStripper>::New();
stripper->SetInput(polygonPolyData); // valid circle
stripper->Update();
vtkSmartPointer<vtkPolyData> circle = stripper->GetOutput();
// prepare the binary image's voxel grid
vtkSmartPointer<vtkImageData> whiteImage =
vtkSmartPointer<vtkImageData>::New();
double bounds[6];
circle->GetBounds(bounds);
double spacing[3]; // desired volume spacing
spacing[0] = 0.67;
spacing[1] = 0.67;
spacing[2] = 5;
whiteImage->SetSpacing(spacing);
// compute dimensions
int dim[3];
dim[0]=512;
dim[1]=512;
dim[2]=1;
whiteImage->SetDimensions(dim);
whiteImage->SetExtent(0, dim[0] - 1, 0, dim[1] - 1, 0, dim[2] - 1);
double origin[3];
// NOTE: I am not sure whether or not we had to add some offset!
origin[0] = bounds[0];
origin[1] = bounds[2];
origin[2] = bounds[4];
whiteImage->SetOrigin(origin);
whiteImage->SetScalarTypeToUnsignedChar();
whiteImage->AllocateScalars();
// fill the image with foreground voxels:
unsigned char inval = 255;
unsigned char outval = 0;
vtkIdType count = whiteImage->GetNumberOfPoints();
for (vtkIdType i = 0; i < count; ++i)
{
whiteImage->GetPointData()->GetScalars()->SetTuple1(i, inval);
}
// sweep polygonal data (this is the important thing with contours!)
vtkSmartPointer<vtkLinearExtrusionFilter> extruder =
vtkSmartPointer<vtkLinearExtrusionFilter>::New();
extruder->SetInput(circle);
extruder->SetScaleFactor(1.);
extruder->SetExtrusionTypeToNormalExtrusion();
extruder->SetVector(0, 0, 1);
extruder->Update();
// polygonal data --> image stencil:
vtkSmartPointer<vtkPolyDataToImageStencil> pol2stenc =
vtkSmartPointer<vtkPolyDataToImageStencil>::New();
pol2stenc->SetTolerance(0); // important if extruder->SetVector(0, 0, 1)
!!!
pol2stenc->SetInputConnection(extruder->GetOutputPort());
pol2stenc->SetOutputOrigin(origin);
pol2stenc->SetOutputSpacing(spacing);
pol2stenc->SetOutputWholeExtent(whiteImage->GetExtent());
pol2stenc->Update();
// cut the corresponding white image and set the background:
vtkSmartPointer<vtkImageStencil> imgstenc
=vtkSmartPointer<vtkImageStencil>::New();
imgstenc->SetInput(whiteImage);
imgstenc->SetStencil(pol2stenc->GetOutput());
imgstenc->ReverseStencilOff();
imgstenc->SetBackgroundValue(outval);
imgstenc->Update();
vtkSmartPointer<vtkMetaImageWriter> imageWriter =
vtkSmartPointer<vtkMetaImageWriter>::New();
imageWriter->SetFileName("labelImage.mhd");
imageWriter->SetInputConnection(imgstenc->GetOutputPort());
imageWriter->Write();
imageWriter->SetFileName("whiteimage.mhd");
imageWriter->SetInput(whiteImage);
imageWriter->Write();
return EXIT_SUCCESS;
}
--
View this message in context: http://vtk.1045678.n5.nabble.com/VTKpolydata-2D-polygon-to-VTKimagedata-tp5714456.html
Sent from the VTK - Users mailing list archive at Nabble.com.
More information about the vtkusers
mailing list