Documentation‎ > ‎

2. IO

DICOM

    The DICOM parser is able to get the image data and also the header information, including Siemens CSA header content. The following example shows how to read the DICOM image using the input operator >>. 

tipl::io::dicom dicom_parser;
tipl::image<short,2> image_data;

if(dicom_parser.load_from_file("dicom_file.dcm"))
    dicom_parser >> image_data;

  You may also use the following codes to load DICOM files.

tipl::image<short,2> image_data;
image_data.load_from_file<tipl::io::dicom>("dicom_file.dcm");

    The get other information such as the name of the patient, manufacturer, or spatial resolution, you have to look up the group and element number from DICOM manual. This number can be used to get the specific information.

DICOM Volume

    To load the an image volume from a series of DICOM files, you may use the volume loader.

std::vector<std::string> files;
tipl::image<float,3> source_image;
tipl::io::volume volume;

if(!volume.load_from_files(files,files.size()))
    return false;
volume >> source_images;

NIFTI and Analyze 

    The same input operator can be used to read NIfTI and Analyze images. Note that this two image formats share the same parser.

tipl::io::nifti nifti_parser;
tipl::image<short,2> image_data;


if(nifti_parser.load_from_file("nifti_file.nii"))
    nifti_parser >> image_data;

    To get other header information, just refer to the nif_header structure, and use it.

// output the description of the data
std::cout << nifti_parser.nif_header.descrip << std::endl;

MATLAB MAT V4

    TIPL also supports mat format. Just the input/output operator to read/write the mat file.  Note that the supported mat format is the version 4 format. In matlab, you have to specific -v4 to save the data in v4 format.

tipl::image<short,2> image_data;
tipl::io::mat mat_file;
if(mat_file.load_from_file("mat_image.mat"))
    mat_file >> image_data;
    
    *The mat v4 file should contain two matrices 1) one named "dimension", storing the image dimension 2) one named "image", storing the image data. To confirm the detail, save an image data to a mat file using save_to_file function.

Bitmap 

tipl::io::bitmap bitmap_parser;
tipl::color_image image_data;

if(bitmap.load_from_file("bitmap.bmp"))
    bitmap >> image_data;

Bruker 2dseq files

The following codes parse the bruker 2dseq files. The dimension information will be obtained from reco and 3dproc file (placed in the same directory).

tipl::io::bruker_2dseq file;
tipl::image<short,2> image_data;

if(bitmap.load_from_file("/1/2dseq"))
     file  >> image_data;

Example

Convert DICOM to NIFTI 

tipl::io::dicom dicom_file;
if(!dicom_file.load_from_file("dicom_2d.dcm"))
{
      std::cout << "load dicom_2d failed" << std::endl;
      return;
}

tipl::image<short,2> image_data;
dicom_file >> image_data;

tipl::io::nifti nifti_file;
nifti_file << image_data;
nifti_file.save_to_file("result_dicom_converted_to.nii");

Convert DICOM to BITMAP file

tipl::io::dicom dicom_file;
if(!dicom_file.load_from_file("dicom_2d.dcm"))
{
      std::cout << "load dicom_2d failed" << std::endl;
      return;
}
// save bitmap
tipl::io::bitmap bitmap_file;
tipl::divide_constant(image_data.begin(),image_data.end(),
            *std::max_element(image_data.begin(),image_data.end())/255);
bitmap_file << image_data;
bitmap_file.save_to_file("converted_dicom_2d.bmp");


1. Load DICOM image and save it as the Nifti format


#include "tipl/tipl.hpp"

int main()
{
    // Load DICOM image
    tipl::io::dicom dicom_file;
    dicom_file.load_from_file("dicom_file.dcm");

    // Copy the DICOM data to image data
    tipl::image<short,2> image_data;
    dicom_file >> image_data;

    // Save image to Nifti 
    tipl::io::nifti nii_file;
    nii_file<< image_data;
    nii_file.save_to_file("nifti_file.nii");
        
    return 0;
}


2 Load a series of DICOM files as a 3D image volume

#include "tipl/tipl.hpp"

int main()
{
    std::vector<std::string> file_names;
    tipl::image<float,3> source_image;
    tipl::io::volume volume;
    if(!volume.load_from_files(file_names,files.size()))
        return -1;
    volume >> source_images;
    return 0;
}


Comments