在VTK中,图像数据被作为一种特别的数据类型来处理,由vtkImageData表示,这种数据类型是结构化的,这意味着每个数据点的位置可隐含由图像数据的原点(第一张图像左下角在世界坐标系中的位置)、数据点间距(沿坐标方向相邻两个数据点的距离,或体素的长宽高)和维数(沿坐标方向体素或数据点的数目)确定。
VTK为图像数据提供了很多处理类,这里给出运行成功的代码示例(pReader读取图像文件):
(1)计算梯度:
vtkImageGradient* pGradient = vtkImageGradient::New();
pGradient->SetInput((vtkDataObject*)pReader->GetOutput());
pGradient->SetDimensionality(3);
vtkImageViewer* pViewer = vtkImageViewer::New();
pViewer->SetInput(pGradient->GetOutput());
pViewer->SetColorLevel(127.5);
pViewer->SetColorWindow(256);
pViewer->GetActor2D()->SetPosition(0,0);
vtkRenderWindow* pRenWnd = vtkRenderWindow::New();
pRenWnd->AddRenderer(pViewer->GetRenderer());
pRenWnd->SetSize(512, 512);
vtkRenderWindowInteractor* pRenWndInact = vtkRenderWindowInteractor::New();
pRenWndInact->SetRenderWindow(pRenWnd);
pRenWndInact->Initialize();
pRenWndInact->Start();
(2)高斯平滑:
vtkImageGaussianSmooth* pGaussian = vtkImageGaussianSmooth::New();
pGaussian->SetInput((vtkDataObject*)pReader->GetOutput());
pGaussian->SetDimensionality(2);
pGaussian->SetStandardDeviations(1, 1);
vtkImageViewer* pViewer = vtkImageViewer::New();
pViewer->SetInput(pGaussian->GetOutput());
pViewer->SetColorLevel(127.5);
pViewer->SetColorWindow(256);
pViewer->GetActor2D()->SetPosition(0,0);
vtkRenderWindow* pRenWnd = vtkRenderWindow::New();
pRenWnd->AddRenderer(pViewer->GetRenderer());
pRenWnd->SetSize(512, 512);
vtkRenderWindowInteractor* pRenWndInact = vtkRenderWindowInteractor::New();
pRenWndInact->SetRenderWindow(pRenWnd);
pRenWndInact->Initialize();
pRenWndInact->Start();
(3)直方图:
double range[2];
vtkImageData* pImg = (vtkImageData*)pReader->GetOutput();
pImg->Update();
pImg->GetScalarRange(range);
vtkImageAccumulate* pHistgram = vtkImageAccumulate::New();
pHistgram->SetInput((vtkDataObject*)pReader->GetOutput());
pHistgram->SetComponentExtent(0, static_cast<int>(range[1] - range[0]), 0, 0, 0, 0);
pHistgram->SetComponentOrigin(range[0], 0, 0);
pHistgram->UpdateWholeExtent();
vtkXYPlotActor* plot = vtkXYPlotActor::New();
plot->ExchangeAxesOff();
plot->SetLabelFormat("%g");
plot->SetXRange(range[0], range[1]);
plot->SetYRange(0, 4096);
plot->SetXTitle("Gray Level");
plot->SetYTitle("");
plot->AddInput(pHistgram->GetOutput());
plot->SetXValuesToValue();
vtkRenderer* pRender = vtkRenderer::New();
pRender->AddActor(plot);
vtkRenderWindow* pRenWnd = vtkRenderWindow::New();
pRenWnd->AddRenderer(pRender);
pRenWnd->SetSize(512, 512);
vtkRenderWindowInteractor* pRenWndInact = vtkRenderWindowInteractor::New();
pRenWndInact->SetRenderWindow(pRenWnd);
pRenWndInact->Initialize();
pRenWndInact->Start();
(4)布尔逻辑计算:
vtkImageEllipsoidSource* psrc1 = vtkImageEllipsoidSource::New();
vtkImageEllipsoidSource* psrc2 = vtkImageEllipsoidSource::New();
psrc1->SetCenter(100, 100, 0);
psrc2->SetCenter(170, 100, 0);
psrc1->SetRadius(70, 70, 70);
psrc2->SetRadius(70, 70, 70);
vtkImageLogic* pxor = vtkImageLogic::New();
pxor->SetInput1((vtkDataObject *)psrc1->GetOutput());
pxor->SetInput2((vtkDataObject *)psrc2->GetOutput());
pxor->SetOperationToXor();
pxor->SetOutputTrueValue(150);
vtkImageViewer* pViewer = vtkImageViewer::New();
pViewer->SetInput(pxor->GetOutput());
pViewer->SetColorWindow(255);
pViewer->SetColorLevel(127.5);
vtkRenderWindow* pRenWnd = vtkRenderWindow::New();
pRenWnd->AddRenderer(pViewer->GetRenderer());
pRenWnd->SetSize(270, 200);
vtkRenderWindowInteractor* pRenWndInact = vtkRenderWindowInteractor::New();
pRenWndInact->SetRenderWindow(pRenWnd);
pRenWndInact->Initialize();
pRenWndInact->Start();
trackback:http://blog.csdn.net/bingo_gmwu/article/details/4747038