[vtkusers] vtkMarchingSquares to vtkContourWidget
Chuck Lloyd
chuck.lloyd at gmail.com
Wed Aug 8 14:35:57 EDT 2012
I have a rather late follow-up to the question from Nicolas.
I'm having the same problem and I have not been able to figure
out how to fix it. I did write up a small example program and
included a picture of the output.
Hopefully someone can help me figure this out and we can add a
nice new example to the vtk wiki page.
The problem is that the contours from vtkMarchingSquares
do not feed cleanly into vtkContourWidget.
Here is the output image with the contours drawn in green
and the ContourWidget representation in red.
http://imagebin.org/223789
The code to reproduce is here:
http://paste.ubuntu.com/1136372/
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkImageActor.h>
#include <vtkMarchingSquares.h>
#include <vtkStripper.h>
#include <vtkActor.h>
#include <vtkInteractorStyleTrackball.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkProperty.h>
#include <vtkContourWidget.h>
#include <vtkContourRepresentation.h>
#include <vtkOrientedGlyphContourRepresentation.h>
#include <vtkRendererCollection.h>
#include <vtkLookupTable.h>
#include <vtkLinearContourLineInterpolator.h>
int main (int, char *[])
{
// Make primitive image
vtkSmartPointer<vtkImageData> tempImage =
vtkSmartPointer<vtkImageData>::New();
tempImage->SetDimensions( 10, 10, 1 );
tempImage->SetScalarTypeToUnsignedChar();
tempImage->AllocateScalars();
// Make view of image
unsigned char *ptr = (unsigned char*)tempImage->GetScalarPointer();
// Clear to black
for( int i=0; i<100; i++) ptr[i] = 0;
// Draw a little shape
for( int i=32; i<35; i++) ptr[i] = 255;
for( int i=41; i<47; i++) ptr[i] = 255;
ptr[52] = 255; ptr[54] = 255;
// Find contour of image
vtkSmartPointer<vtkMarchingSquares> msf =
vtkSmartPointer<vtkMarchingSquares>::New();
msf->SetInputConnection( tempImage->GetProducerPort() );
msf->SetNumberOfContours( 1 );
msf->SetValue( 0, 128 );
msf->Update();
// Simplify it (Does this do anything?)
vtkSmartPointer<vtkStripper> vs = vtkSmartPointer<vtkStripper>::New();
vs->SetInputConnection( msf->GetOutputPort() );
vs->Update();
vtkSmartPointer<vtkPolyData> pd = vs->GetOutput();
// Debug Visualization
vtkSmartPointer<vtkImageActor> imageActor =
vtkSmartPointer<vtkImageActor>::New();
imageActor->SetInput( tempImage );
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
vtkSmartPointer<vtkLookupTable> lut =
vtkSmartPointer<vtkLookupTable>::New();
//set up LUT for contour polyline
lut->SetNumberOfTableValues(1);
lut->Build();
lut->SetTableValue(0, 0.0, 1.0, 0.0, 1.0 );// Show contour in Green
//add the lut to the mapper, then add to actor
mapper->SetLookupTable( lut );
mapper->SetColorModeToMapScalars();
mapper->SetInput( pd );
vtkSmartPointer<vtkActor> contourActor = vtkSmartPointer<vtkActor>::New();
contourActor->SetMapper(mapper);
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor
= vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
vtkSmartPointer<vtkOrientedGlyphContourRepresentation> contourRep =
vtkSmartPointer<vtkOrientedGlyphContourRepresentation>::New();
contourRep->GetLinesProperty()->SetColor(1, 0, 0); // Show contour
widget in red
vtkSmartPointer<vtkContourWidget> contourWidget =
vtkSmartPointer<vtkContourWidget>::New();
contourWidget->SetInteractor(renderWindowInteractor);
contourWidget->SetRepresentation(contourRep);
contourWidget->On();
renderer->AddActor(contourActor);
renderer->AddActor( imageActor );
renderer->SetBackground( 0.0, 0.0, 0.45 );
//set line interpolator
vtkLinearContourLineInterpolator *lcli =
vtkLinearContourLineInterpolator::New();
contourRep->SetLineInterpolator(lcli);
contourWidget->Initialize(pd);
contourWidget->Render();
renderWindow->GetRenderers()->GetFirstRenderer()->ResetCamera();
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
From: Nicolas Rannou <nicolas_rannou <at> hms.harvard.edu>
Subject: vtkMarchingSquares to vtkContourWidget
Newsgroups: gmane.comp.lib.vtk.user
Date: 2010-05-05 22:26:24 GMT (2 years, 13 weeks, 4 days, 7 hours and
52 minutes ago)
Hello,
I'm trying to extract contours using marching squares then to use this
contour to initialize a vtkContourWidget.
But I couldn't get good results yet.
What I do:
/***************************************************************************/
vtkMarchingSquares* contours = vtkMarchingSquares::New();
contours->SetInput( image );
contours->GenerateValues ( 1, 0, 0 );
vtkStripper * stripper = vtkStripper::New();
stripper->SetInput( contours->GetOutput() );
stripper->Update();
vtkPolyData* testPolyD = vtkPolyData::New();
testPolyD->SetPoints( stripper->GetOutput()->GetPoints() );
vtkTransform *t = vtkTransform::New();
t->Translate( seed_pos[0], seed_pos[1], seed_pos[2] );
vtkTransformPolyDataFilter *tf = vtkTransformPolyDataFilter::New();
tf->SetTransform( t );tf->SetInput( testPolyD );
tf->Update();
vtkOrientedGlyphContourRepresentation* contourRep =
vtkOrientedGlyphContourRepresentation::New();
vtkContourWidget* contourWidget = vtkContourWidget::New();
contourWidget->SetInteractor(this->m_ImageView->GetImageViewer(0)->GetInteractor());
contourWidget->SetRepresentation(contourRep);
contourWidget->On();
contourWidget->Initialize(iView);
vtkPolyData* contour = contourRep->GetContourRepresentationAsPolyData();
/***************************************************************************/
then, if I plot this polydata, I get something like in the enclosed png
in blue.
I roughly added in green the nodes I get while using the vtkContourWidget.
The nodes are in the good position but the data hasn't the good "shape".
I also roughly draw in red the polydata I would like to have in my
vtkContourWidget.
The expected shape would be the same as the one I get just after
applying the marching squares but I can't use directly the output of the
marching square filter since it contains no point and no usable line.
I must be missing something but what...
Thanks,
Nicolas
_______________________________________________
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
Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers
More information about the vtkusers
mailing list