VTK图像显示方式

用VTK显示图像的几种方式:

方式1:

读取数据-----映射(mapper)------演员(actor)-------渲染(render)-----渲染窗口(renwin)----交互方式

在console下实现的代码如下:

#include "vtkActor.h"

#include "vtkRenderer.h"

#include "vtkRenderWindow.h"

#include "vtkRenderWindowInteractor.h"

#include "vtkImageViewer.h"

#include "vtkImageViewer2.h"

#include"vtkPNGReader.h"

#include "vtkImageActor.h"

#include "vtkInteractorStyleImage.h"

#include "vtkActor2D.h"

#include "vtkAutoInit.h"

VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2
VTK_MODULE_INIT(vtkInteractionStyle);
//vtk show pic with the way of one
int main()

{

	int extent[6];

	vtkSmartPointer< vtkPNGReader>m_reader = vtkSmartPointer::New();

	m_reader->SetFileName("1.png");

	m_reader->SetDataByteOrderToLittleEndian();//the type of data reading 

	m_reader->SetDataScalarTypeToUnsignedChar();//the type of data

	m_reader->SetFileDimensionality(3);

	vtkSmartPointermapper = vtkSmartPointer::New();

	mapper->SetInputConnection(m_reader->GetOutputPort());

	mapper->SetColorLevel(128);

	mapper->SetColorWindow(256);

	vtkSmartPointeractor = vtkSmartPointer::New();

	actor->SetMapper(mapper);

	vtkSmartPointerm_Renderer = vtkSmartPointer::New();

	vtkSmartPointerm_RenWin = vtkSmartPointer::New();

	m_RenWin->AddRenderer(m_Renderer);

	vtkSmartPointeriren = vtkSmartPointer::New();

	iren->SetRenderWindow(m_RenWin);

	vtkSmartPointerinteractor = vtkSmartPointer::New();

	iren->SetInteractorStyle(interactor);

	m_Renderer->AddActor(actor);

	m_Renderer->SetBackground(0, 0, 0);

	m_RenWin->Render();

	m_reader->GetDataExtent(extent);

	iren->Initialize();

	iren->Start();

	return 0;

}

VTK图像显示方式_第1张图片

方式二:

图像数据读取----数据给imageactor----渲染显示

在console下实现如下

#include "vtkActor.h"

#include "vtkRenderer.h"

#include "vtkRenderWindow.h"

#include "vtkRenderWindowInteractor.h"

#include "vtkImageViewer.h"

#include "vtkImageViewer2.h"

#include"vtkPNGReader.h"

#include "vtkImageActor.h"

#include "vtkInteractorStyleImage.h"

#include "vtkActor2D.h"

#include "vtkAutoInit.h"

VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2
VTK_MODULE_INIT(vtkInteractionStyle);
//vtk show pic with the way of 2
int main()

{

	vtkSmartPointer m_reader = vtkSmartPointer::New();

	m_reader->SetFileName("1.png");

	m_reader->SetDataByteOrderToLittleEndian();

	m_reader->SetDataScalarTypeToUnsignedChar();

	m_reader->SetFileDimensionality(3);
	m_reader->Update();

	vtkSmartPointerm_Actor = vtkSmartPointer::New();

	m_Actor->SetInputData(m_reader->GetOutput());


	vtkSmartPointerm_Renderer = vtkSmartPointer::New();

	vtkSmartPointerm_RenWin = vtkSmartPointer::New();

	m_RenWin->AddRenderer(m_Renderer);

	vtkSmartPointeriren = vtkSmartPointer::New();

	iren->SetRenderWindow(m_RenWin);

	vtkSmartPointerinteractor = vtkSmartPointer::New();

	iren->SetInteractorStyle(interactor);

	m_RenWin->SetSize(300, 300);

	m_Renderer->AddActor(m_Actor);

	m_Renderer->SetBackground(0.0, 0.0, 0.0);

	m_RenWin->Render();

	iren->Initialize();

	iren->Start();

	return 0;

}

VTK图像显示方式_第2张图片

方式3:

用ImageView2

console实现如下:

#include "vtkActor.h"

#include "vtkRenderer.h"

#include "vtkRenderWindow.h"

#include "vtkRenderWindowInteractor.h"

#include "vtkImageViewer.h"

#include "vtkImageViewer2.h"

#include"vtkPNGReader.h"

#include "vtkImageActor.h"

#include "vtkInteractorStyleImage.h"

#include "vtkActor2D.h"

#include "vtkAutoInit.h"

VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2
VTK_MODULE_INIT(vtkInteractionStyle);
//vtk show pic 3
int main()
{
	vtkSmartPointer reader =
		vtkSmartPointer::New();
	reader->SetFileName("1.png");
	reader->SetDataByteOrderToLittleEndian();
	reader->SetDataScalarTypeToUnsignedChar();
	reader->Update();

	vtkSmartPointer imageViewer =
		vtkSmartPointer::New();
	imageViewer->SetInputConnection(reader->GetOutputPort());

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

	imageViewer->SetColorLevel(128);
	imageViewer->SetColorWindow(256);
//	imageViewer->SetSlice(40);
//	imageViewer->SetSliceOrientationToXY();
	imageViewer->Render();

	imageViewer->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
//	imageViewer->SetSize(640, 480);
	imageViewer->GetRenderWindow()->SetSize(640, 480);
	imageViewer->GetRenderWindow()->SetWindowName("DisplayImageExample");

	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}
VTK图像显示方式_第3张图片





你可能感兴趣的:(VTK)