医站点医维基

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2227|回复: 6

[VTK] VTK例子1-实现DICOM格式图片的批量读取和切片显示

[复制链接]

336

主题

410

回帖

2806

积分

管理员

积分
2806

热心会员推广达人优秀版主荣誉管理论坛元老

发表于 2015-12-4 22:08:08 | 显示全部楼层 |阅读模式
本帖最后由 medimage 于 2015-12-4 22:17 编辑

实现DICOM格式图片的批量读取和切片显示
#include "stdafx.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkVolume16Reader.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkOutlineFilter.h"
#include "vtkCamera.h"
#include "vtkStripper.h"
#include "vtkLookupTable.h"
#include "vtkImageDataGeometryFilter.h"
#include "vtkMarchingCubes.h"
#include "vtkImageShrink3D.h"
#include "vtkProperty.h"
#include "vtkDICOMImageReader.h"

void main( int argc, char *argv[] )
{
        // create the renderer stuff   
        vtkRenderer *aRenderer = vtkRenderer::New();
        vtkRenderWindow *renWin = vtkRenderWindow::New();
        renWin->AddRenderer(aRenderer);
        vtkRenderWindowInteractor *iren =vtkRenderWindowInteractor::New();
        iren->SetRenderWindow(renWin);
        // read the volume读取函数
       vtkDICOMImageReader   *reader =  vtkDICOMImageReader::New();
        reader->SetDataByteOrderToLittleEndian();
        //reader->SetDirectoryName("e://ct/head40/");
         reader->SetDirectoryName("E://VC_Project/DATA");//设置读取的文件的目录路径

         //
        reader->SetDataOrigin(0.0, 0.0, 0.0);
        reader->Update();
        int extent[6];
        reader->GetDataExtent(extent);          //FOV:210*189  MAT:256*161    383*344
        double spacing [3];  
        reader->GetDataSpacing(spacing);
        int xx,yy,zz;
        xx=(int)((extent[1]+1)*spacing[0]);     //左右方向
        yy=(int)((extent[3]+1)*spacing[1]);     //前后方向
        zz=(int)((extent[5]+1)*spacing[2]);     //上下方向 (-1.6)*70
         cout<<xx<<"  "<<yy<<"  "<<zz<<"  ";
     vtkImageShrink3D *shrink=vtkImageShrink3D::New();
         shrink->SetInput((vtkDataObject *)reader->GetOutput());
        shrink->SetShrinkFactors(4,4,1);        //Set/Get the shrink factors

        // extract the skin   提取表面
        vtkMarchingCubes *skinExtractor = vtkMarchingCubes::New();
        skinExtractor->SetInputConnection(shrink->GetOutputPort());
        skinExtractor->SetValue(0, 900);
        vtkStripper *skinStripper = vtkStripper::New();
        skinStripper->SetInput(skinExtractor->GetOutput());
        vtkPolyDataMapper *skinMapper = vtkPolyDataMapper::New();
        skinMapper->SetInput(skinStripper->GetOutput());
        skinMapper->ScalarVisibilityOff();
        vtkActor *skin = vtkActor::New();
        skin->SetMapper(skinMapper);
        skin->GetProperty()->SetDiffuseColor(1, .49, .25);
        skin->GetProperty()->SetSpecular(.3);
        skin->GetProperty()->SetSpecularPower(20);
        // extract the bone
        vtkMarchingCubes *boneExtractor = vtkMarchingCubes::New();
        boneExtractor->SetInputConnection(shrink->GetOutputPort());
        boneExtractor->SetValue(0, 500);
        vtkStripper *boneStripper = vtkStripper::New();
        boneStripper->SetInput(boneExtractor->GetOutput());
        vtkPolyDataMapper *boneMapper = vtkPolyDataMapper::New();
        boneMapper->SetInput(boneStripper->GetOutput());
        boneMapper->ScalarVisibilityOff();
        vtkActor *bone = vtkActor::New();
        bone->SetMapper(boneMapper);
        bone->GetProperty()->SetDiffuseColor(1, 1, .9412);
        // get an outline
        vtkOutlineFilter *outlineData = vtkOutlineFilter::New();
        outlineData->SetInputConnection(shrink->GetOutputPort());
        vtkPolyDataMapper *mapOutline = vtkPolyDataMapper::New();
        mapOutline->SetInput(outlineData->GetOutput());
        vtkActor *outline = vtkActor::New();
        outline->SetMapper(mapOutline);
        outline->GetProperty()->SetColor(0,0,0);
        // create a b/w lookup table
        vtkLookupTable *bwLut = vtkLookupTable::New();
        bwLut->SetTableRange (0, 2000);
        bwLut->SetSaturationRange (0, 0);
        bwLut->SetHueRange (0, 0);
        bwLut->SetValueRange (0, 1);
        // create a hue lookup table
        vtkLookupTable *hueLut = vtkLookupTable::New();
        hueLut->SetTableRange (0, 2000);
        hueLut->SetHueRange (0, 1);
        hueLut->SetSaturationRange (1, 1);
        hueLut->SetValueRange (1, 1);
        // create a saturation lookup table
        vtkLookupTable *satLut = vtkLookupTable::New();
        satLut->SetTableRange (0, 2000);
        satLut->SetHueRange (.6, .6);
        satLut->SetSaturationRange (0, 1);
        satLut->SetValueRange (1, 1);
        // sagittal
        vtkImageDataGeometryFilter *saggitalSection =
        vtkImageDataGeometryFilter::New();
        saggitalSection->SetExtent (65,65, 0,yy, 0, zz);
        saggitalSection->SetInputConnection(shrink->GetOutputPort());
        vtkPolyDataMapper *saggitalMapper = vtkPolyDataMapper::New();
        saggitalMapper->SetInput(saggitalSection->GetOutput());
        saggitalMapper->ScalarVisibilityOn();
        saggitalMapper->SetScalarRange (-1024, 2000);
        saggitalMapper->SetLookupTable (bwLut);
        vtkActor *sagittal = vtkActor::New();
        sagittal->SetMapper(saggitalMapper);
        // axial
        vtkImageDataGeometryFilter *axialSection =
        vtkImageDataGeometryFilter::New();
        axialSection->SetExtent (0,xx, 0,yy, 22, 22);
        axialSection->SetInputConnection(shrink->GetOutputPort());
        vtkPolyDataMapper *axialMapper = vtkPolyDataMapper::New();
        axialMapper->SetInput(axialSection->GetOutput());
        axialMapper->ScalarVisibilityOn();
        axialMapper->SetScalarRange (-1024, 2000);
        axialMapper->SetLookupTable (hueLut);
        vtkActor *axial = vtkActor::New();
        axial->SetMapper(axialMapper);
        // coronal
        vtkImageDataGeometryFilter *coronalSection =
        vtkImageDataGeometryFilter::New();
        coronalSection->SetExtent (0,xx, 65, 65, 0, zz);
        coronalSection->SetInputConnection(shrink->GetOutputPort());
        vtkPolyDataMapper *coronalMapper =
        vtkPolyDataMapper::New();
        coronalMapper->SetInput(coronalSection->GetOutput());
        coronalMapper->ScalarVisibilityOn();
        coronalMapper->SetScalarRange (-1024, 2000);
        coronalMapper->SetLookupTable (satLut);
        vtkActor *coronal = vtkActor::New();
        coronal->SetMapper(coronalMapper);
        // create a camera with the correct view up
        vtkCamera *aCamera = vtkCamera::New();
        aCamera->SetViewUp (0, 0, -1);
        aCamera->SetPosition (0, 1, 0);
        aCamera->SetFocalPoint (0, 0, 0);
        // now, tell the renderer our actors
        aRenderer->AddActor(outline);
        aRenderer->AddActor(sagittal);
        aRenderer->AddActor(axial);
        aRenderer->AddActor(coronal);
        aRenderer->AddActor(skin);
        aRenderer->AddActor(bone);
        // turn off bone for this examples
//        bone->VisibilityOff();
        // set skin to semi-transparent
        skin->GetProperty()->SetOpacity(0.5);
        bone->GetProperty()->SetOpacity(0.3);
        aRenderer->SetActiveCamera(aCamera);
        aRenderer->ResetCamera ();
        aCamera->Dolly(1.5);
        aRenderer->SetBackground(1,1,1);
        aRenderer->ResetCameraClippingRange();
        // interact with data
        renWin->SetSize(600, 600);
        renWin->Render();
//        SAVEIMAGE( renWin );
        iren->Start();
1.png
回复

使用道具 举报

110

主题

429

回帖

1428

积分

金牌会员

积分
1428

热心会员推广达人优秀版主荣誉管理论坛元老

发表于 2016-5-13 21:31:38 | 显示全部楼层
谢谢你的辛苦劳动了!!!
回复

使用道具 举报

15

主题

428

回帖

1137

积分

版主

积分
1137

热心会员推广达人优秀版主荣誉管理论坛元老

发表于 2016-5-14 17:59:13 | 显示全部楼层
谢谢你的辛苦劳动了!!!
回复

使用道具 举报

37

主题

407

回帖

1137

积分

版主

积分
1137

热心会员推广达人优秀版主荣誉管理论坛元老

发表于 2016-5-23 01:37:13 | 显示全部楼层
学习了!!!!!!!!!
回复

使用道具 举报

0

主题

3

回帖

23

积分

新手上路

积分
23
发表于 2016-7-3 10:32:12 | 显示全部楼层
您好,这个例子的图片来源是公开的吗?
回复

使用道具 举报

0

主题

3

回帖

23

积分

新手上路

积分
23
发表于 2016-7-3 10:33:17 | 显示全部楼层
您好,这个例子的图片来源是公开的吗?
回复

使用道具 举报

0

主题

3

回帖

23

积分

新手上路

积分
23
发表于 2016-7-3 10:33:40 | 显示全部楼层
您好,这个例子的图片来源是公开的吗?
还有CMakeLists文件里面代码怎么写呢?
谢谢您啦
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|关于我们|医维基|网站地图|Archiver|手机版|医疗之家 ( 沪ICP备2023001278号-1 )  

GMT+8, 2024-5-2 19:00 , Processed in 0.198045 second(s), 36 queries .

Designed by Medical BBS

快速回复 返回顶部 返回列表