// 本例子可用于读取单幅CT图像并显示相应DICOM文件中头信息。
// First include the required header files for the VTK classes we are using.
#include
using namespace std;
#include "vtkDICOMImageReader.h"
#include "vtkBMPReader.h"
#include "vtkImageViewer.h"
#include "vtkRenderWindowInteractor.h"
//滤波函数
#include "vtkImageSobel2D.h"
#include "vtkImageLaplacian.h"
#include "vtkImageHybridMedian2D.h"
#include "vtkImageFFT.h"
#include "vtkImageRFFT.h"
#include "vtkImageButterworthHighPass.h"
#include "vtkImageButterworthLowPass.h"
#include "vtkImageIdealLowPass.h"
#include "vtkImageIdealHighPass.h"
#include "vtkImageAnisotropicDiffusion2D.h"
#include "vtkImageAccumulate.h"
#include "vtkImageConvolve.h"
#include "vtkImageExtractComponents.h"
#include "vtkImageGaussianSmooth.h"
#include "vtkImageShiftScale.h"
#include "vtkImageSkeleton2D.h"
#include "vtkImageShrink3D.h"
#include "vtkImageFlip.h"
#include "vtkImagePermute.h"
#include "vtkImageResample.h"
#include "vtkImageConstantPad.h"
#include "vtkImageMirrorPad.h"
#include "vtkImageMagnify.h"
#include "vtkImageGradient.h"
int _tmain(int argc, _TCHAR* argv[])
{
vtkDICOMImageReader *dr = vtkDICOMImageReader::New();
dr->SetFileName("d://1/3/91879664");
//vtkBMPReader *dr = vtkBMPReader::New();
// dr->SetFileName("E://CT/lena.bmp");
//vtkMedicalImageReader2 *dr = vtkMedicalImageReader2::New();
dr->SetDataByteOrderToLittleEndian();
dr->SetDataOrigin(0.0,0.0,0.0);//Set/Get the origin of the data (location of first pixel in the file).
//dr->SetDataVOI(100,500,100,200,1,1);//Set/get the data VOI. You can limit the reader to only read a subset of the data.
//=================================================//滤波操作
//计算直方图
vtkImageAccumulate *acc=vtkImageAccumulate::New();//Generalized histograms up to 4 dimensions.
acc->SetInputConnection(dr->GetOutputPort());
acc->SetComponentOrigin (0.0 ,0.0,0.0);
acc->SetComponentSpacing(1.0 ,1.0,0);
//卷积
const double kernel[9]={1,1,1,1,-9,1,1,1,1};
vtkImageConvolve *conv=vtkImageConvolve::New();
conv->SetInputConnection(dr->GetOutputPort());
conv->SetKernel3x3(kernel) ;
//计算梯度
vtkImageGradient *grad=vtkImageGradient::New();
grad->SetInputConnection(dr->GetOutputPort());
grad->SetDimensionality(2) ; //Determines how the input is interpreted (set of 2d slices ...)
//
vtkImageSkeleton2D *wrap=vtkImageSkeleton2D ::New();
wrap->SetInputConnection(dr->GetOutputPort());
//图像放大,不会用
vtkImageMagnify *mfy=vtkImageMagnify::New();
mfy->SetInputConnection(dr->GetOutputPort());
mfy->SetMagnificationFactors(2,2,2);//只能放大整数倍,对DCM图像无效
//图像放大,不会用
vtkImageMirrorPad *mirror=vtkImageMirrorPad::New();
mirror->SetInputConnection(dr->GetOutputPort());
//图像放大,不会用
vtkImageConstantPad *pad=vtkImageConstantPad::New();
pad->SetInputConnection(dr->GetOutputPort());
pad->SetConstant( 0.5 );
vtkImagePermute *per=vtkImagePermute::New();
per->SetInputConnection(dr->GetOutputPort());
per->SetFilteredAxes (3,3,0) ;
//图像数据的缩放,但我不知怎么用
vtkImageResample *resample=vtkImageResample::New();
resample->SetInputConnection(dr->GetOutputPort());
resample->SetDimensionality (2);
resample->SetAxisMagnificationFactor (2,2);
resample->SetOutputExtent (100,400,100,400,0,0);
vtkImageFlip *flip=vtkImageFlip::New();//左右
flip->SetInputConnection(dr->GetOutputPort());
vtkImageExtractComponents *eu=vtkImageExtractComponents::New();
eu->SetInputConnection(dr->GetOutputPort());
eu->SetComponents (0);
vtkImageShrink3D *shrink=vtkImageShrink3D::New();
shrink->SetInputConnection(dr->GetOutputPort());
shrink->SetShrinkFactors (8,8,1);
vtkImageShiftScale *swept=vtkImageShiftScale::New();
swept->SetInputConnection(dr->GetOutputPort());
swept->SetScale (2);
vtkImageGaussianSmooth *gauss=vtkImageGaussianSmooth::New();
gauss->SetInputConnection(dr->GetOutputPort());
gauss->SetDimensionality(2);
gauss->SetRadiusFactors (2,2,0) ;
vtkImageHybridMedian2D *hy=vtkImageHybridMedian2D::New();
hy->SetInputConnection(dr->GetOutputPort());
vtkImageFFT *fft=vtkImageFFT::New();
fft->SetDimensionality(2);
fft->SetInputConnection(dr->GetOutputPort());
vtkImageButterworthHighPass *highPass=vtkImageButterworthHighPass::New();
highPass->SetInputConnection(fft->GetOutputPort());
highPass->SetOrder(2);
highPass->SetXCutOff(0.2);
highPass->SetYCutOff(0.1);
highPass->ReleaseDataFlagOff();
vtkImageIdealLowPass *low=vtkImageIdealLowPass::New();//理想低通滤波
low->SetInputConnection(fft->GetOutputPort());
low->SetXCutOff(0.2);
low->SetYCutOff(0.1);
low->ReleaseDataFlagOff();
vtkImageIdealHighPass *high=vtkImageIdealHighPass::New();////理想高通滤波
high->SetInputConnection(fft->GetOutputPort());
high->SetXCutOff(0.2);
high->SetYCutOff(0.1);
high->ReleaseDataFlagOff();
vtkImageButterworthLowPass *lowPass=vtkImageButterworthLowPass::New();
lowPass->SetInputConnection (fft->GetOutputPort());
lowPass->SetOrder(2);
lowPass->SetXCutOff(0.2);
lowPass->SetYCutOff(0.1);
lowPass->ReleaseDataFlagOff();//控制是否数据在被一个来源用之后被释放的这个物体
vtkImageRFFT *rfft=vtkImageRFFT::New();
rfft->SetDimensionality(2);
rfft->SetInputConnection(low->GetOutputPort());
vtkImageSobel2D *sobel=vtkImageSobel2D::New();//sobel滤波
sobel->SetInputConnection(dr->GetOutputPort());
sobel->ReleaseDataFlagOff();
vtkImageHybridMedian2D *middle=vtkImageHybridMedian2D::New();//中值滤波
middle->SetInputConnection(dr->GetOutputPort());
middle->ReleaseDataFlagOff();
vtkImageAnisotropicDiffusion2D *diff=vtkImageAnisotropicDiffusion2D::New();
diff->SetInputConnection(dr->GetOutputPort());
diff->SetCorners(2);
diff->SetEdges(3);
vtkImageLaplacian *lacian=vtkImageLaplacian::New();
lacian->SetInputConnection(dr->GetOutputPort());
//=================显示部分 将上面任何一个结果放到线面======================================
vtkImageViewer *viewer = vtkImageViewer::New();
viewer->SetInput(resample->GetOutput());//*********修改这里
viewer->SetColorWindow(1000);
viewer->SetColorLevel(400);
viewer->SetPosition(200,100);
viewer->Render();
vtkRenderWindowInteractor *viewerinter = vtkRenderWindowInteractor::New();
viewer->SetupInteractor(viewerinter);
viewerinter->Initialize();
viewerinter->Start();
cout<
return 1;
}
本文来源:
http://hi.baidu.com/abenmao32032/item/794c11b5f2c2689518469717