1、ITK提取等高线(区域轮廓) itk::ContourExtractor2DImageFilter
typedef itk::ContourExtractor2DImageFilter<FSliceType> ContourExtractorType;
ContourExtractorType::Pointer contourFilter = ContourExtractorType::New();
contourFilter->SetInput(distanceFilter->GetOutput());
contourFilter->SetContourValue(0);
try
{contourFilter->Update();
}
catch(itk::ExceptionObject& er)
{cout<<er<<endl;
}
//cout<<"contour num is :"<<contourFilter->GetNumberOfOutputs()<<endl;//找出最大轮廓,并依次保存轮廓点
vtkPolyData* myContour = vtkPolyData::New();if (contourFilter->GetNumberOfOutputs()!=0)
{vtkCellArray* cell = vtkCellArray::New();vtkPoints* points = vtkPoints::New();int pointId = 0 ; int max = contourFilter->GetOutput(0)->GetVertexList()->size();int ind = 0;for(unsigned int i = 0; i < contourFilter->GetNumberOfOutputs(); ++i){ if(contourFilter->GetOutput(i)->GetVertexList()->size()>max){max = contourFilter->GetOutput(i)->GetVertexList()->size();ind = i;}}//获取轮廓的物理坐标//cout<<"contour size is :"<<contourFilter->GetOutput(ind)->GetVertexList()->size()<<endl;vtkPolyLine* polyLine = vtkPolyLine::New();ContourExtractorType::VertexListType::ConstIterator vertexIterator = contourFilter->GetOutput(ind)->GetVertexList()->Begin(); while(vertexIterator != contourFilter->GetOutput(ind)->GetVertexList()->End()){SliceImageType::PointType pointTemp;itk::ContinuousIndex<double,2> continousIndex;continousIndex[0] = vertexIterator->Value()[0];continousIndex[1] = vertexIterator->Value()[1];slicedata->TransformContinuousIndexToPhysicalPoint(continousIndex,pointTemp);double point[3];point[0] = pointTemp[0]*spacing[0];point[1] = pointTemp[1]*spacing[1];point[2] = znum; pointId = points->InsertNextPoint(point);polyLine->GetPointIds()->InsertNextId(pointId);++vertexIterator;}//polyLine->GetPointIds()->InsertNextId(0);cell->InsertNextCell(polyLine);polyLine->Delete();myContour->SetPoints(points);myContour->SetLines(cell);//myContour->SetPolys(cell);cell->Delete();myContour->Modified();//cout<<"end calc contour ..."<<endl;points->Delete();
}
参照:《itk实用demo》-轮廓提取_itk轮廓提取-CSDN博客
注意事项:一张图的提取结果会出现两种情况,如下所示
可通过 SetVertexConnectHighPixels 进行设置得到自己想要的结果,上图中左边是设置为true的结果
2、ITK进行区域分割 itk::ConnectedThresholdImageFilter
typedef itk::ConnectedThresholdImageFilter<YourImageType, YourImageType> ConnectedThresholdFilter;
ConnectedThresholdFilter::Pointer connectedThresholdF = ConnectedThresholdFilter::New();
connectedThresholdF->SetInput(YourImageData); // itk::SmartPointer<YourImageType>类型
connectedThresholdF->SetSeed(minIdx); // YourImageType::IndexType类型
connectedThresholdF->SetLower(254); // 提取区域的最小像素值(不包括在结果中)
connectedThresholdF->SetUpper(255); // 提取区域的最大像素值(包括在结果中)
connectedThresholdF->SetConnectivity(ConnectedThresholdFilter::FullConnectivity); // 8近旁连通提取
connectedThresholdF->SetReplaceValue(200); // 提取结果中的替代值
connectedThresholdF->Update();