VTK之图像处理

在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 
 

转载于:https://www.cnblogs.com/JohnShao/archive/2012/07/29/2614309.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值