[vtkusers] vtk polydata to image data
AIs
royalmatador at gmail.com
Tue Jun 10 04:59:09 EDT 2014
Hi VTK Users,
I am trying to convert vtk Polydata in to image data.
I have followed this example
http://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/PolyDataToImageData
and it works. But when I use a polydata generated from a images I am unable
to generate a image data from the polydata. Following is my code,
vtkSmartPointer<vtkDICOMImageReader> dimg =
vtkSmartPointer<vtkDICOMImageReader>::New( );
dimg->SetDirectoryName( "C:/Users/Desktop/DS2" );
dimg->SetDataExtent( 0, 511, 0, 511, 1, 62 );
dimg->SetDataOrigin( 0.0, 0.0, 0.0 );
dimg->SetDataSpacing( 1.0, 1.0, 1.0 );
dimg->SetDataByteOrderToLittleEndian( );
dimg->Update( );
//defining contour filter
vtkSmartPointer<vtkContourFilter> cont =
vtkSmartPointer<vtkContourFilter>::New( );
cont->SetInputConnection( dimg->GetOutputPort( ) );
cont->SetValue( 0, 148 );
vtkSmartPointer<vtkPolyData> pd = cont->GetOutput( );
cont->Update( );
// defining image data
vtkSmartPointer<vtkImageData> idata = vtkSmartPointer<vtkImageData>::New( );
// defining bounda for the image data
double bounds [ 6 ];
pd->GetBounds( bounds );
// define spacing
double spacing [ 3 ];
spacing [ 0 ] = 0.5;
spacing [1 ] = 0.5;
spacing [ 2 ] = 0.5;
idata->SetSpacing( spacing );
// computing dimensions
int dim [ 3 ];
for ( int i = 0; i < 3; i++ ) {
dim [ i ] = static_cast<int>( ceil ( ( bounds[ i * 2 + 1 ] - bounds [ i *
2 ] ) / spacing [ i ] ) );
}
idata->SetDimensions( dim );
idata->SetExtent( 0, dim [ 0 ] - 1, 0, dim [ 1 ] - 1, 0, dim [ 2 ] - 1 );
double origin [ 3 ];
origin[ 0 ] = bounds [ 0 ] + spacing [ 0 ] / 2 ;
origin [ 1 ] = bounds [ 2 ] + spacing [ 1 ] / 2 ;
origin [ 2 ] = bounds [ 4 ] + spacing [ 2 ] / 2 ;
idata->SetOrigin( origin );
idata->SetScalarTypeToUnsignedChar( );
idata->AllocateScalars( );
// fill image with foreground voxels
unsigned char inval = 255;
unsigned char outval = 0;
vtkIdType cnt = idata->GetNumberOfPoints( );
for ( vtkIdType i = 0; i < cnt; i++ ) {
idata->GetPointData( )->GetScalars( )->SetTuple1( i, inval );
}
// polydata to image stencil
vtkSmartPointer<vtkPolyDataToImageStencil> pol2istenc =
vtkSmartPointer<vtkPolyDataToImageStencil>::New( );
pol2istenc->SetInput( pd );
pol2istenc->SetOutputOrigin( origin );
pol2istenc->SetOutputSpacing( spacing );
pol2istenc->SetOutputWholeExtent( idata->GetExtent( ) );
pol2istenc->Update( );
// defining image stencil ( image data )
vtkSmartPointer<vtkImageStencil> istenc =
vtkSmartPointer<vtkImageStencil>::New( );
istenc->SetInput( idata );
istenc->SetStencil( pol2istenc->GetOutput( ) );
istenc->ReverseStencilOff( );
istenc->SetBackgroundValue( outval );
istenc->Update( );
// visualization
vtkSmartPointer<vtkPolyDataMapper> spoly =
vtkSmartPointer<vtkPolyDataMapper>::New( );
spoly->SetInputConnection( source->GetOutputPort( ) );
spoly->ScalarVisibilityOff( );
vtkSmartPointer<vtkActor> sact = vtkSmartPointer<vtkActor>::New( );
sact->SetMapper( spoly );
vtkSmartPointer<vtkImageActor> iact = vtkSmartPointer<vtkImageActor>::New(
);
iact->SetInput( istenc->GetOutput( ) );
vtkSmartPointer<vtkRenderer> ren1 = vtkSmartPointer<vtkRenderer>::New( );
ren1->AddActor( iact );
ren1->SetBackground( 0.2, 0.3, 0.4 );
vtkSmartPointer<vtkRenderWindow> renWin1 =
vtkSmartPointer<vtkRenderWindow>::New( );
renWin1->AddRenderer( ren1 );
vtkSmartPointer<vtkRenderWindowInteractor> iren1 =
vtkSmartPointer<vtkRenderWindowInteractor>::New( );
iren1->SetRenderWindow( renWin1 );
renWin1->Render( );
iren1->Start( );
On implementing this code all I get is a black slice as 2D image data.
I have already tried by changing spacing and bounds but is not able to get
an image data from my generated polydata.
But the same example code works for polydata obtained using sphere source as
followed by implementing the example.
Can some one please guide me as to what am I doing wrong.
Thanking you in advance.
Kind regards.
AIs
--
View this message in context: http://vtk.1045678.n5.nabble.com/vtk-polydata-to-image-data-tp5727408.html
Sent from the VTK - Users mailing list archive at Nabble.com.
More information about the vtkusers
mailing list