ITK图像数据读取:
1:设置图像数据类型例如常见的bmp、jpg、jpeg以及dicom、raw等
2:数据读取
下面的例子是在控制台下实现的png图像数据的读与写:
#include "itkRGBPixel.h" #include "itkImage.h" #include "itkImageFileReader.h" #include "itkImageFileWriter.h" #include "itkPNGImageIOFactory.h" //ITK file reader and writer test int main() { // Verify the number of parameters in the command line // Software Guide : BeginLatex // // The \doxygen{RGBPixel} class is templated over the type used to // represent each one of the red, green and blue components. A typical // instantiation of the RGB image class might be as follows. // // \index{itk::RGBPixel!Instantiation} // // Software Guide : EndLatex // Software Guide : BeginCodeSnippet typedef itk::RGBPixel< unsigned char > PixelType; typedef itk::Image< PixelType, 2 > ImageType;//set the type of pic // Software Guide : EndCodeSnippet // Software Guide : BeginLatex // // The image type is used as a template parameter to instantiate // the reader and writer. // // \index{itk::ImageFileReader!RGB Image} // \index{itk::ImageFileWriter!RGB Image} // // Software Guide : EndLatex // Software Guide : BeginCodeSnippet typedef itk::ImageFileReader< ImageType > ReaderType; typedef itk::ImageFileWriter< ImageType > WriterType; ReaderType::Pointer reader = ReaderType::New(); WriterType::Pointer writer = WriterType::New(); // Software Guide : EndCodeSnippet itk::PNGImageIOFactory::RegisterOneFactory(); // const char * inputFilename = argv[1]; // const char * outputFilename = argv[2]; // Software Guide : BeginLatex // // The filenames of the input and output files must be provided to the // reader and writer respectively. // // Software Guide : EndLatex // Software Guide : BeginCodeSnippet reader->SetFileName("1.png"); writer->SetFileName("2.png"); // Software Guide : EndCodeSnippet ImageType::Pointer image = reader->GetOutput(); writer->SetInput(image); // Software Guide : BeginLatex // // Finally, execution of the pipeline can be triggered by invoking the // Update() method in the writer. // // Software Guide : EndLatex // Software Guide : BeginCodeSnippet writer->Update(); // Software Guide : EndCodeSnippet // Software Guide : BeginLatex // // You may have noticed that apart from the declaration of the // \code{PixelType} there is nothing in this code specific to RGB // images. All the actions required to support color images are implemented // internally in the \doxygen{ImageIO} objects. // // Software Guide : EndLatex return EXIT_SUCCESS; }ITK与VTK实现图像的显示:
用ITK 实现图像数据的读取,然后通过连接器把ITK读取的图像数据传输到VTK 然后进行显示;
下面是一个在console下实现的ITK与VTK混合编程实现图像的显示:
#include "itkImage.h" #include "itkImageFileReader.h" #include "itkImageToVTKImageFilter.h" #include "itkPNGImageIOFactory.h" #include "vtkImageViewer.h" #include "vtkRenderWindowInteractor.h" #include "vtkAutoInit.h" VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2 VTK_MODULE_INIT(vtkInteractionStyle); //ITK and VTK to show picture test int main(int argc, char **argv) { typedef itk::Image<unsigned char, 2> ImageType;//the type of pic typedef itk::ImageFileReader<ImageType> ReaderType; //the class of read pic typedef itk::ImageToVTKImageFilter<ImageType> ConnectorType; //connenct itk and vtk ReaderType::Pointer reader = ReaderType::New(); ConnectorType::Pointer connector = ConnectorType::New(); reader->SetFileName("1.png"); itk::PNGImageIOFactory::RegisterOneFactory(); connector->SetInput(reader->GetOutput()); connector->Update(); vtkImageViewer *viewer = vtkImageViewer::New(); vtkRenderWindowInteractor *interactor = vtkRenderWindowInteractor::New(); viewer->SetInputData(connector->GetOutput()); viewer->SetupInteractor(interactor); viewer->GetRenderWindow()->SetSize(500, 500);//set window size viewer->SetColorWindow(255); //set window color viewer->SetColorLevel(128); //set the level of window viewer->Render(); interactor->Initialize(); interactor->Start(); return EXIT_SUCCESS; }