目录
环境
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();
}