Medical Image Processing applications often encounter the operation of reading a sequence of images. For example, CT, Mr, and other images are stored in one plane and one plane, and medical image processing programs need to process the data, the first step is to import the data from external storage media such as disks to the memory.
You can use VTK to read image files in multiple formats and read a single two-dimensional image (for example *. BMP ,*. JPEG ,*. PNG, etc.) or 3D image files (*. VTK ,*. MHD ,*. (MHA). It also supports the import of sequential image files. Next we will explain in detail how to read sequential image files in VTK (we use us visual human data as the test data, the data can be read fromHereDownload ).
Before reading a VTK sequence image, note that the file name of the sequence image to be read must be regular, as shown in the following figure.
Tips: It involves modifying the file name. If you are familiar with the doscommand, you can directly use the RENAME command to complete the modification, or use a small software renamer to modify the file name.
Figure 1 sequential image naming example
Method 1: Use setfilenames () to read sequential images.
1 [CPP] view plaincopy 2 // array of file names for generating image sequences 3 vtksmartpointer <vtkstringarray> filearray = 4 vtksmartpointer <vtkstringarray>: New (); 5 charfilename [128]; 6 For (INTI = 1; I <20; I ++) // Several images are cyclically several times 7 {8 sprintf (filename ,".. /visiblewomanhead/visiblewomanhead_10902d.jpg ", I); 9 vtkstd: stringfilestr (filename); 10 filearray-> insertnextvalue (filestr ); 11} 12 // read JPG sequence image 13 vtksmartpointer <vtkw.reader> reader = 14 vtksmartpointer <vtkw.reader >:: new (); 15 reader-> setfilenames (filearray ); 16 reader-> Update ();
Note: The sequential files to be read are stored in the visiblewomanhead folder, and the naming rules for each image are: visiblewomanhead_01.jpg,visiblewomanhead_02.jpg ...... 1. Method 1 we use vtkstringarray to first construct an array of file names containing the sequential image files to be read, and then call the setfilenames () method in vtkw.reader. The setfilenames () method is implemented in class vtkimagereader2. Therefore, most VTK image reading classes can use this method to read sequential image files.
Method 2: Use setfileprefix ()/setfilepattern () to read the sequence image.
As we have mentioned earlier, when reading a sequence image file, it is required that the file names in the sequence be named regularly (1). Since these file names are regular, we can use the setfileprefix ()/setfilepattern () method in vtkimagereader2 to read the sequential image.
1 vtksmartpointer <vtkw.reader> reader = 2 vtksmartpointer <vtkw.reader >:: new (); 3 reader-> setfileprefix ("D:/data/visiblewomanhead _"); 4 reader-> setfilepattern ("%s000002d.jpg"); 5 reader-> setdataextent (0,511, 0,511, 512); // The image size is 5126 * reader-> Update ();
As for the reason for writing this, you will understand it, especially the parameters of the setfileprefix (), setfilepattern (), and setdataextent () functions.
Method 3: Read data one by one, and then merged into a three-dimensional data body.
1 vtkSmartPointer<vtkImageAppend > append = 2 vtkSmartPointer<vtkImageAppend >::New(); 3 append->SetAppendAxis(2); 4 5 vtkSmartPointer<vtkJPEGReader>reader = 6 vtkSmartPointer<vtkJPEGReader>::New(); 7 char fileName[128]; 8 for(int i = 1; i < 21; i++) 9 {10 sprintf(fileName,"D:/Data/VisibleWomanHead/VisibleWomanHead_%02d.jpg", i);11 reader->SetFileName(fileName);12 append->AddInputConnection(reader->GetOutputPort());13 }
We use the vtkimageappend class for merge operations. The method setappendaxis (2) specifies the Z axis as the stack direction of the image data read from each layer. The rest of the code will not be described.
============ You are welcome to reprint the statement. Please keep this statement information ============
Copyright belongs to @ Dong Ling studio. For more information, visit Dong Ling studio.
Tutorials navigation: http://blog.csdn.net/www_doling_net/article/details/8763686
========================================================== ==========
Here we add another method to read DICOM files using the vtkvolume16reader class:
vtkVolume16Reader *v16 = vtkVolume16Reader::New();v16->SetFilePrefix( "E:/MedImage Data/Simon/SIMON_" );v16->SetFilePattern("%s%02d.dcm");v16->SetDataDimensions (512,512);v16->SetImageRange (0,54);v16->SetDataByteOrderToLittleEndian();v16->SetDataSpacing (0.25, 0.25, 1.0);