[vtkusers] Extracting regions... What's wrong with this code?

Michnay Balázs michnay at freemail.hu
Tue Nov 23 03:59:57 EST 2004


Dear VTK Users,
Please look at the following code, which should find all connected 
components and extracts one of them (my final goal is to extract all).
It does the following:

- it uses vtkContourFilter to convert vtkImageData to vtkPolyData.
- then I use vtkPolyDataConnectivityFilter with 
SetExtractionModeToAllRegions to find how many connected regions 
are in the image
- then I use SetExtractionModeToSpecifiedRegions (with 
AddSpecifiedRegion(0); ) to extract the first region.
- the output of the extraction is then given to a vtkPolyDataMapper and 
to a vtkActor.
- When I add the actor to one of the renderers nothing gets displayed...

What's wrong? Thanks in advance.
Regards,
 MB

The code:

void CAnalyzeItView::OnExtractregions() 
{
	// TODO: Add your command handler code here
	CAnalyzeItDoc* pDoc = GetDocument();

	vtkContourFilter *contour;
		contour = vtkContourFilter::New();
		contour->SetInput(pDoc->MainImageContainer
[m_lastsel].threshImage);
		contour->SetValue(0, 255.0f);	
		contour->Update();

	vtkPolyDataConnectivityFilter *connectivity = 
vtkPolyDataConnectivityFilter::New();
		connectivity->SetInput(contour->GetOutput());
		//connectivity->ScalarConnectivityOff();
		connectivity->SetExtractionModeToAllRegions();
		connectivity->Update();
		int i = connectivity->GetNumberOfExtractedRegions
();

	connectivity->SetExtractionModeToSpecifiedRegions();
	connectivity->AddSpecifiedRegion(0);
	connectivity->Update();

	vtkPolyData *extracted = vtkPolyData::New();
	extracted = connectivity->GetOutput();
	
	vtkPolyDataMapper *extractedMapper = 
vtkPolyDataMapper::New();
	extractedMapper->SetInput(extracted);
	extractedMapper->SetScalarRange(0,0);
	extractedMapper->SetNumberOfPieces(1);
	extractedMapper->SetColorMode(0);
	extractedMapper->SetResolveCoincidentTopology(0);
	extractedMapper->SetUseLookupTableScalarRange(0);

	vtkActor *extractedActor = vtkActor::New();
	extractedActor->SetMapper(extractedMapper);
	//extractedActor->SetPosition(0,0,0);
	pDoc->renderer3->AddActor2D(extractedActor);
	pDoc->renWin->Render();

	connectivity->Delete();
	contour->Delete();

	CString str;
	str.Format(_T("Number of connected regions: %i"), i);
	pDoc->textMapperExtractedRegions->SetInput(str);
}



More information about the vtkusers mailing list