VTK 圈取不规则ROI区域

这篇博客介绍了如何使用VTK库在Qt环境中实现图像的ROI(感兴趣区域)选择与提取。通过vtkContourWidget绘制轮廓,然后利用vtkPolyDataToImageStencil和vtkImageStencilToImage将轮廓转换为二值图像,最后通过vtkImageMask提取ROI区域。博客提供了详细的代码实现和步骤解释。
摘要由CSDN通过智能技术生成

目录

环境

1、实现效果图

 2、实现思路 

2.1 绘制轮廓

2.2 提取 ROI 区域


环境

QT 5.14.2

VTK 8.2.0 (vs2019编译,64位 release 版本)

构建方式:Qt5.14.2  MSVC2017 64bit Release

1、实现效果图

 2、实现思路 

1、利用 vtkContourWidget 类在原图片上框选出一块闭合区域。

2、利用 vtkPolyDataToImageStencil 类和 vtkImageStencilToImage 类将 vtkContourWidget 形成的 polydata 数据转换为一张二值图像。

3、利用 vtkImageMask 类提取 ROI 区域。

2.1 绘制轮廓

void Widget::on_pbDrawContour_clicked()
{
    m_contourWidget = vtkSmartPointer<vtkContourWidget>::New();
    m_rep = vtkSmartPointer<vtkOrientedGlyphContourRepresentation>::New();
    m_rep->GetLinesProperty()->SetColor(1, 0, 0); // Set color to red
    m_rep->GetLinesProperty()->SetLineWidth(2);
    m_contourWidget->SetRepresentation(m_rep);

    vtkSmartPointer<vtkImageActorPointPlacer> p_w_picpathActorPointPlacer = vtkSmartPointer<vtkImageActorPointPlacer>::New();
    p_w_picpathActorPointPlacer->SetImageActor(m_sourceImageView->GetImageActor());
    m_rep->SetPointPlacer(p_w_picpathActorPointPlacer);

    m_contourWidget->SetInteractor(ui->sourceWidget->GetRenderWindow()->GetInteractor());
    m_contourWidget->SetEnabled(true);
    m_contourWidget->ProcessEventsOn();
}

2.2 提取 ROI 区域

void Widget::on_pbROI_clicked()
{
    double *spacing = m_imageData->GetSpacing();
    double *origin  = m_imageData->GetOrigin();
    int *dim        = m_imageData->GetDimensions();

    // 将 polydata 转换为 二值图像
    // 1、从 ContourWidget 提取 polydata 数据并处理
    vtkPolyData * polydata = m_rep->GetContourRepresentationAsPolyData();
    vtkSmartPointer<vtkPolyDataToImageStencil> polyDataToImageStencil = vtkSmartPointer<vtkPolyDataToImageStencil>::New();
    polyDataToImageStencil->SetInputData(polydata);
    polyDataToImageStencil->SetOutputOrigin(origin);
    polyDataToImageStencil->SetOutputSpacing(spacing);
    polyDataToImageStencil->SetOutputWholeExtent(0, dim[0]-1, 0, dim[1]-1, 0, 0);
    polyDataToImageStencil->Update();

    // 2、将 polydata 转换为 二值图像
    vtkSmartPointer<vtkImageStencilToImage> imageStencilToImage = vtkSmartPointer<vtkImageStencilToImage>::New();
    imageStencilToImage->SetInputConnection(polyDataToImageStencil->GetOutputPort());
    imageStencilToImage->SetInsideValue(255);     // 封闭图像内部为白色
    imageStencilToImage->SetOutsideValue(0);      // 封闭图像外部为黑色
    imageStencilToImage->Update();

    // 3、提取 ROI 区域
    vtkNew<vtkImageMask> maskFilter;
    maskFilter->SetInputData(0, m_imageData);
    maskFilter->SetInputData(1, imageStencilToImage->GetOutput());
    maskFilter->SetMaskedOutputValue(0,0,0);
    maskFilter->Update();

    // 显示
    m_roiImageView->SetInputData(maskFilter->GetOutput());
    m_roiImageView->Render();
}

完整工程:VTK圈取不规则 ROI-其它文档类资源-CSDN下载

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
VTK 中,可以使用 `vtkExtractVOI` 滤波器来提取感兴趣区域(VOI)的数据,并将其用于三维重建。下面是一个示例: ```python import vtk # 读取 DICOM 文件 reader = vtk.vtkDICOMImageReader() reader.SetDirectoryName('path/to/dicom/files') reader.Update() # 获取数据范围 data_range = reader.GetOutput().GetScalarRange() # 创建感兴趣区域 voi = vtk.vtkExtractVOI() voi.SetInputConnection(reader.GetOutputPort()) voi.SetVOI(0, 63, 0, 63, 0, 31) # 在这里设置 VOI 的坐标范围 voi.SetSampleRate(1, 1, 1) voi.Update() # 创建可视化管线 mapper = vtk.vtkSmartVolumeMapper() mapper.SetInputConnection(voi.GetOutputPort()) volume = vtk.vtkVolume() volume.SetMapper(mapper) # 创建渲染窗口和交互器 renderer = vtk.vtkRenderer() renderer.AddVolume(volume) render_window = vtk.vtkRenderWindow() render_window.AddRenderer(renderer) interactor = vtk.vtkRenderWindowInteractor() interactor.SetRenderWindow(render_window) # 开始渲染 render_window.Render() interactor.Start() ``` 在上述代码中,我们首先使用 `vtkDICOMImageReader` 读取 DICOM 文件,并获取数据范围。接下来,我们使用 `vtkExtractVOI` 滤波器创建感兴趣区域,并设置 VOI 的坐标范围。然后,我们使用 `vtkSmartVolumeMapper` 和 `vtkVolume` 创建一个体积可视化,并将其添加到渲染器中。最后,我们创建一个渲染窗口和交互器,并开始渲染。注意,在实际应用中,您需要根据自己的数据格式和需求进行相应的调整。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lucky_sunshine

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值