vtk 实现分割图像和原图叠加显示的两种方式

1.用vtkImageBlend实现

vtkLookupTable实现灰度图到彩色图的映射,vtkImageBlend叠加灰度图和彩色图

具体代码如下:

//分割后的图像   
vtkSmartPointer<vtkLookupTable> pColorTable = vtkSmartPointer<vtkLookupTable>::New();
	pColorTable->SetNumberOfColors(2);
	pColorTable->SetTableRange(0, 255);
	pColorTable->SetTableValue(0, 0.0, 0.0, 0.0, 0.0);
	pColorTable->SetTableValue(1, 0, 1, 0, 1.0);
	pColorTable->Build();

	vtkSmartPointer<vtkImageMapToColors> colorMap =
		vtkSmartPointer<vtkImageMapToColors>::New();
	colorMap->SetInputData(outImage);
	colorMap->SetLookupTable(pColorTable);
	colorMap->Update();
//原图
	vtkSmartPointer<vtkLookupTable> pLookupTable = vtkSmartPointer<vtkLookupTable>::New();
	pLookupTable->SetRange(-1000, 3095);
	pLookupTable->SetValueRange(0.0, 1.0);
	pLookupTable->SetSaturationRange(0.0, 0.0);
	pLookupTable->SetRampToLinear();
	pLookupTable->Build();

	vtkSmartPointer<vtkImageMapToColors> colorMap1 =
		vtkSmartPointer<vtkImageMapToColors>::New();
	colorMap1->SetInputData(inImage);
	colorMap1->SetLookupTable(pLookupTable);
	colorMap1->Update();
//合并
	vtkSmartPointer< vtkImageBlend> imageBlend = vtkSmartPointer<vtkImageBlend>::New();
	imageBlend->AddInputData(colorMap1->GetOutput());
	imageBlend->AddInputData(colorMap->GetOutput());
	imageBlend->SetOpacity(0, 0.8);
	imageBlend->SetOpacity(1, 0.4);
	imageBlend->Update();

2.用vtkImageMask实现

具体代码如下:

  // Apply mask
  vtkNew<vtkImageMask> masker;
  masker->SetImageInputData(input);
  //masker->SetMaskInputData(resampledMask);
  masker->SetMaskInputData(padder->GetOutput());
  //masker->SetMaskInputData(mask);
  masker->SetMaskedOutputValue(fillValue);
  masker->Update();

运行效果如下:

完整代码,使用了vtkImageViewer2显示

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType);
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkScalarToRGBPixelFunctor.h"
#include "itkUnaryFunctorImageFilter.h"
#include "itkVectorGradientAnisotropicDiffusionImageFilter.h"
#include "itkWatershedImageFilter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkScalarToRGBColormapImageFilter.h"
#include "itkGradientMagnitudeImageFilter.h"
#include "itkGradientMagnitudeRecursiveGaussianImageFilter.h"
#include "itkImage.h"
#include "itkImageFileReader.h"//读取头文件
#include "itkGDCMImageIO.h"//ImageIo子类头文件
#include "itkImageFileWriter.h"
#include "itkNrrdImageIO.h"
//颜色映射
//#include "itkBinaryImageToLabelMapFilter.h"
//#include "itkLabelMapToLabelImageFilter.h"
//#include "itkLabelOverlayImageFilter.h"
//#include "itkRescaleIntensityImageFilter.h"
//#include "itkScalarToRGBColormapImageFilter.h"

#include "itkVTKImageToImageFilter.h"
#include "itkImageToVTKImageFilter.h"
#include <vtkImageBlend.h>
#include <vtkLookupTable.h>
#include <vtkImageMapToColors.h>
#include <vtkImageViewer2.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkTextMapper.h>
using namespace std;
class StatusMessage {
public:
    static std::string Format(int slice, int maxSlice)
    {
        std::stringstream tmp;
        tmp << "Slice Number  " << slice + 1 << "/" << maxSlice + 1;

        return tmp.str();
    }

};
// Define own interation style;
class myVtkInteractorStyleImage : public vtkInteractorStyleImage
{
public:
    static myVtkInteractorStyleImage* New();
    vtkTypeMacro(myVtkInteractorStyleImage, vtkInteractorStyleImage);

protected:
    vtkImageViewer2* _ImageViewer;
    vtkTextMapper* _StatusMapper;
    int _Slice;
    int _MinSlice;
    int _MaxSlice;

public:
    void SetImageViewer(vtkImageViewer2* imageViwer)
    {
        _ImageViewer = imageViwer;
        _MinSlice = imageViwer->GetSliceMin();
        _MaxSlice = imageViwer->GetSliceMax();
        _Slice = _MinSlice;

        cout << "Slice : Min = " << _MinSlice << ", Max = " << _MaxSlice << endl;

    }

    //void SetStatusMapper(vtkTextMapper* statusMapper)
    //{
    //    _StatusMapper = statusMapper;
    //}

protected:
    void MoveSliceForward()
    {
        if (_Slice < _MaxSlice)
        {
            _Slice += 1;
            cout << " MoveSliceForward::Slice = " << _Slice << endl;
            _ImageViewer->SetSlice(_Slice);
            // std::string msg = StatusMessage::Format(_Slice, _MaxSlice);
            // _StatusMapper->SetInput(msg.c_str());
            _ImageViewer->Render();

        }
    }

    void MoveSliceBackward()
    {
        if (_Slice > _MinSlice)
        {
            _Slice -= 1;
            cout << "MoveSliceBackward::Slice = " << _Slice << endl;
            _ImageViewer->SetSlice(_Slice);
            // std::string msg = StatusMessage::Format(_Slice, _MaxSlice);
            // _StatusMapper->SetInput(msg.c_str());
            _ImageViewer->Render();

        }
    }

    virtual void OnKeyDown()
    {
        string key = this->GetInteractor()->GetKeySym();
        if (key.compare("Up") == 0)
        {
            MoveSliceForward();
        }
        else if (key.compare("Down") == 0)
        {
            MoveSliceBackward();
        }
        vtkInteractorStyleImage::OnKeyDown();

    }

    virtual void OnMouseWheelForward()
    {
        MoveSliceForward();
    }

    virtual void OnMouseWheelBackward()
    {
        if (_Slice > _MinSlice)
        {
            MoveSliceBackward();
        }
    }
};


vtkStandardNewMacro(myVtkInteractorStyleImage);

int  main()
{
    typedef   float           InternalPixelType;
    const     unsigned int    Dimension = 3;
    typedef itk::Image< InternalPixelType, Dimension >  InternalImageType;

    typedef int                            OutputPixelType;
    typedef itk::Image< OutputPixelType, Dimension > OutputImageType;

    //图像读取与图像写类型定义
    typedef  itk::ImageFileReader< OutputImageType > ReaderType;
    //图像读取与图像写对象实例化
    ReaderType::Pointer nrrdReader = ReaderType::New();
    typedef itk::NrrdImageIO ImageIOType;
    ImageIOType::Pointer nnrdImageIO = ImageIOType::New();//创建gdcmImageIO对象
    nrrdReader->SetImageIO(nnrdImageIO);
    //reader->SetFileName( "BrainProtonDensitySlice.png" );
    nrrdReader->SetFileName("colormapImageFilter.nrrd");
    nrrdReader->Update();

    ReaderType::Pointer reader = ReaderType::New();
    typedef itk::GDCMImageIO GImageIOType;
    GImageIOType::Pointer gdcmImageIO = GImageIOType::New();//创建gdcmImageIO对象
    reader->SetImageIO(gdcmImageIO);
    reader->SetFileName("C:/I3.dcm");
    reader->Update();
    typedef itk::VTKImageToImageFilter<OutputImageType> vtkToitkFilterType;
    typedef itk::ImageToVTKImageFilter< OutputImageType> itkTovtkFilterType;

    //原图转vtk
    itkTovtkFilterType::Pointer itkTovtkImageFilter = itkTovtkFilterType::New();
    itkTovtkImageFilter->SetInput(reader->GetOutput());//设置图像数据从ITK转向VTK
    itkTovtkImageFilter->Update();

    itkTovtkFilterType::Pointer itkTovtkImageFilter1 = itkTovtkFilterType::New();
    itkTovtkImageFilter1->SetInput(nrrdReader->GetOutput());//设置图像数据从ITK转向VTK
    itkTovtkImageFilter1->Update();

    vtkSmartPointer<vtkLookupTable> pColorTable = vtkSmartPointer<vtkLookupTable>::New();
    pColorTable->SetNumberOfColors(2);
    pColorTable->SetTableRange(0, 1);
    pColorTable->SetTableValue(0, 0.0, 0.0, 0.0, 0.0);
    pColorTable->SetTableValue(1, 0, 1, 0, 1.0);
    pColorTable->Build();

    vtkSmartPointer<vtkImageMapToColors> colorMap =
        vtkSmartPointer<vtkImageMapToColors>::New();
    colorMap->SetInputData(itkTovtkImageFilter1->GetOutput());
    colorMap->SetLookupTable(pColorTable);
    colorMap->Update();

    vtkSmartPointer<vtkLookupTable> pLookupTable = vtkSmartPointer<vtkLookupTable>::New();
    pLookupTable->SetRange(-1000, 3095);
    pLookupTable->SetValueRange(0.0, 1.0);
    pLookupTable->SetSaturationRange(0.0, 0.0);
    pLookupTable->SetRampToLinear();
    pLookupTable->Build();

    vtkSmartPointer<vtkImageMapToColors> colorMap1 =
        vtkSmartPointer<vtkImageMapToColors>::New();
    colorMap1->SetInputData(itkTovtkImageFilter->GetOutput());
    colorMap1->SetLookupTable(pLookupTable);
    colorMap1->Update();

    vtkSmartPointer< vtkImageBlend> imageBlend = vtkSmartPointer<vtkImageBlend>::New();
    imageBlend->AddInputData(colorMap1->GetOutput());
    imageBlend->AddInputData(colorMap->GetOutput());
    imageBlend->SetOpacity(0, 0.8);
    imageBlend->SetOpacity(1, 0.4);
    imageBlend->Update();
    vtkSmartPointer<myVtkInteractorStyleImage> myInteractorStyle =
        vtkSmartPointer<myVtkInteractorStyleImage>::New();

    vtkSmartPointer<vtkImageViewer2> imageViewer = vtkSmartPointer<vtkImageViewer2>::New();
    imageViewer->SetInputData(imageBlend->GetOutput());

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    imageViewer->SetupInteractor(renderWindowInteractor);

    //imageViewer->SetColorLevel(500);//设置窗位为500
    //imageViewer->SetColorWindow(2000);//设置窗宽为2000
    imageViewer->SetSlice(80);//设置切片索引
    imageViewer->SetSliceOrientationToXY();//设置切片方向
    imageViewer->Render();
    myInteractorStyle->SetImageViewer(imageViewer);
    //imageViewer->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
    imageViewer->SetSize(640, 480);
    imageViewer->GetRenderWindow()->SetWindowName("DisplayImageExample");
    renderWindowInteractor->SetInteractorStyle(myInteractorStyle);
    renderWindowInteractor->Start();

    return  0;
}

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值