VTK:轮廓最小凸包提取与显示

     vtk提取凸包使用类 PointsPrjectHull。该类可以获取任意点集的最小凸包。该类输入为点集。输出为包围该点集的最小凸包轮廓点集。前面一章讲了如何寻找图像轮廓并对其进行连通性分析(连通区域分析)。现在以某一连通域的轮廓点集作为输入提取其最小凸闭包。将提取到的凸闭包用vtkPlolyLine来保存显示

关键代码:

vtkSmartPointer point0=
            vtkSmartPointer::New();
    for(int i=0;iGetOutput()->GetNumberOfPoints();++i)
    {
        int regionId=connectFilter1->GetOutput()->GetPointData()->GetScalars()->GetTuple1(i);
        double *xyz=connectFilter1->GetOutput()->GetPoint(i);

        if(regionId==0)//选取区域标号为0的轮廓点集
            point0->InsertNextPoint(xyz);
    }
    //[6] --提取轮廓凸包
    vtkSmartPointer hull =
            vtkSmartPointer::New();
    hull->DeepCopy(point0);
    int zSize =hull->GetSizeCCWHullZ();

    double *pts=new double[zSize*2];
    hull->GetCCWHullZ(pts,zSize);

    vtkSmartPointer zHullPoints=
            vtkSmartPointer::New();
    for(int i=0;iInsertNextPoint(xval,yval,0);
    }
    // Insert the first point again to close the loop
    zHullPoints->InsertNextPoint(pts[0],pts[1],0.0);
    //[6]

    //[7] --画出凸包
    // Display the x hull
    vtkSmartPointer zPolyLine =
            vtkSmartPointer::New();
    zPolyLine->GetPointIds()->SetNumberOfIds(zHullPoints->GetNumberOfPoints());

    for(vtkIdType i = 0; i < zHullPoints->GetNumberOfPoints(); i++)
    {
        zPolyLine->GetPointIds()->SetId(i,i);
    }

    // Create a cell array to store the lines in and add the lines to it
    vtkSmartPointer cells =
            vtkSmartPointer::New();
    cells->InsertNextCell(zPolyLine);

    // Create a polydata to store everything in
    vtkSmartPointer polyData =
            vtkSmartPointer::New();

    // Add the points to the dataset
    polyData->SetPoints(zHullPoints);

    // Add the lines to the dataset
    polyData->SetLines(cells);
    //[7]
 vtkSmartPointer zHullMapper =
            vtkSmartPointer::New();
    zHullMapper->SetInputData(polyData);

    vtkSmartPointer zHullActor =
            vtkSmartPointer::New();
    zHullActor->SetMapper(zHullMapper);
    zHullActor->GetProperty()->SetLineWidth(1);
    zHullActor->GetProperty()->SetColor(1,0,0);

    vtkSmartPointer renderer =
            vtkSmartPointer::New();
    renderer->AddActor(zHullActor);
    renderer->SetBackground(1.0, 1.0, 1.0);

    vtkSmartPointer renderWindow =
            vtkSmartPointer::New();
    renderWindow->AddRenderer(renderer);

    vtkSmartPointer renderWindowInteractor =
            vtkSmartPointer::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    renderWindow->SetSize(640, 320);
    renderWindow->Render();
    renderWindow->SetWindowName("contour extract");
    renderWindow->Render();

    renderWindowInteractor->Start();
  输出的凸闭包最后是以vtlPlotLine形式输出。如果向最后以vtkImageData形式显示 需要继续一步格式类型转换  通过类

vtkPolyDataToImageStencil和vtkImageStencil配合完成。这里提供一个vtk官网例子:vtkpolydata=>vtkimagedata


最后效果如下:

      VTK:轮廓最小凸包提取与显示_第1张图片

灰色为提取的原图轮廓  

红色边框是获取的0号连通区域的最小凸闭包

  白色是将凸闭包填充以vtkImageData形式显示

  

参考文献:

   1 hull

   2 polydata=>imagedata

   3 点击打开链接

你可能感兴趣的:(图像处理,VTK)