vtk資料的讀寫

MissZhou要努力發表於2016-05-03

1.單張圖片的讀取:

vtkPNGReader

不過為啥儲存的顏色變了呢

#include <vtkSmartPointer.h>
#include <vtkPNGReader.h>
#include <vtkJPEGWriter.h>
#include <vtkImageViewer2.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
int main()
{
	vtkSmartPointer<vtkPNGReader>reader = vtkSmartPointer<vtkPNGReader>::New();
	reader->SetFileName("../VTK-logo.png");

	//vtkSmartPointer<vtkImageView2>imageViewer = vtkSmartPointer<vtkImageView2>::New();這裡由於少寫了一個r  導致CE==
	vtkSmartPointer<vtkImageViewer2> imageViewer = vtkSmartPointer<vtkImageViewer2>::New();
	imageViewer->SetInputConnection(reader->GetOutputPort());//寫setinput不可以
	vtkSmartPointer<vtkRenderWindowInteractor>renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
	imageViewer->SetupInteractor(renderWindowInteractor);
	imageViewer->Render();
	imageViewer->GetRenderer()->ResetCamera();
	imageViewer->Render();

	vtkSmartPointer<vtkJPEGWriter>writer = vtkSmartPointer<vtkJPEGWriter>::New();
	writer->SetFileName("vtk-logo.jpg");
	writer->SetInputConnection(reader->GetOutputPort());
	writer->Write();

	renderWindowInteractor->Start();
}

2.一系列影象資料的讀取

多了vtkStringArray  和正常的string不一樣~~

#include <vtkSmartPointer.h>
#include <vtkJPEGReader.h>
#include <vtkImageViewer2.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkStringArray.h>
#include <vtkRenderWindow.h>
int main()
{
	/*vtkSmartPointer<vtkStringArray>fileArray = vtkSmartPointer<vtkStringArray>::New();
	char fileName[128];
	for (int i = 1; i < 100; i++)
	{
		sprintf(fileName, "../data/Head/head%03d.jpg", i);
		vtkstd::string fileStr(fileName);
		fileArray->InsertNextValue(fileStr);
	}*/
	vtkSmartPointer< vtkStringArray > fileArray = vtkSmartPointer< vtkStringArray >::New();
	char fileName[128];
	for (int i = 1; i < 100; i++)
	{
		sprintf(fileName, "../data/Head/head%03d.jpg", i);
		vtkstd::string fileStr(fileName);
		fileArray->InsertNextValue(fileStr);
	}

	vtkSmartPointer<vtkJPEGReader>reader = vtkSmartPointer<vtkJPEGReader>::New();
	reader->SetFileNames(fileArray);

	vtkSmartPointer<vtkImageViewer2>imageViewer = vtkSmartPointer<vtkImageViewer2>::New();
	imageViewer->SetInputConnection(reader->GetOutputPort());
	vtkSmartPointer<vtkRenderWindowInteractor>renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
	imageViewer->SetSlice(50);//預設顯示第50 切片
	imageViewer->SetSliceOrientationToXY();
	imageViewer->SetupInteractor(renderWindowInteractor);
	imageViewer->Render();

	renderWindowInteractor->Start();
	return 0;
}
記得檔名需要是有規律的

相關文章