Example #1
0
bool load_4d_nii(const char* file_name,boost::ptr_vector<DwiHeader>& dwi_files)
{
    gz_nifti analyze_header;
    if(!analyze_header.load_from_file(file_name))
        return false;
    std::cout << "loading 4d nifti" << std::endl;


    std::vector<double> bvals,bvecs;
    {
        QString bval_name,bvec_name;
        bval_name = QFileInfo(file_name).absolutePath() + "/bvals";
        if(!QFileInfo(bval_name).exists())
            bval_name = QFileInfo(file_name).absolutePath() + "/bvals.txt";

        bvec_name = QFileInfo(file_name).absolutePath() + "/bvecs";
        if(!QFileInfo(bvec_name).exists())
            bvec_name = QFileInfo(file_name).absolutePath() + "/bvecs.txt";


        if(QFileInfo(bval_name).exists() && QFileInfo(bvec_name).exists())
        {
            load_bval(bval_name.toLocal8Bit().begin(),bvals);
            load_bvec(bvec_name.toLocal8Bit().begin(),bvecs);
            if(analyze_header.dim(4) != bvals.size() ||
               bvals.size()*3 != bvecs.size())
            {
                bvals.clear();
                bvecs.clear();
            }
            else
                std::cout << "bvals and bvecs loaded" << std::endl;
        }

    }

    image::basic_image<float,4> grad_dev;
    image::basic_image<unsigned char,3> mask;
    if(QFileInfo(QFileInfo(file_name).absolutePath() + "/grad_dev.nii.gz").exists())
    {
        gz_nifti grad_header;
        if(grad_header.load_from_file(QString(QFileInfo(file_name).absolutePath() + "/grad_dev.nii.gz").toLocal8Bit().begin()))
        {
            grad_header.toLPS(grad_dev);
            std::cout << "grad_dev used" << std::endl;
        }
    }
    if(QFileInfo(QFileInfo(file_name).absolutePath() + "/nodif_brain_mask.nii.gz").exists())
    {
        gz_nifti mask_header;
        if(mask_header.load_from_file(QString(QFileInfo(file_name).absolutePath() + "/nodif_brain_mask.nii.gz").toLocal8Bit().begin()))
        {
            mask_header.toLPS(mask);
            std::cout << "mask used" << std::endl;
        }
    }
    {
        float vs[4];
        analyze_header.get_voxel_size(vs);
        for(unsigned int index = 0;check_prog(index,analyze_header.dim(4));++index)
        {
            std::auto_ptr<DwiHeader> new_file(new DwiHeader);
            if(!analyze_header.toLPS(new_file->image,false))
                break;
            image::lower_threshold(new_file->image,0);
            new_file->file_name = file_name;
            std::ostringstream out;
            out << index;
            new_file->file_name += out.str();
            std::copy(vs,vs+3,new_file->voxel_size);
            if(!bvals.empty())
            {
                new_file->bvalue = bvals[index];
                new_file->bvec[0] = bvecs[index*3];
                new_file->bvec[1] = bvecs[index*3+1];
                new_file->bvec[2] = bvecs[index*3+2];
                new_file->bvec.normalize();
                if(new_file->bvalue < 10)
                {
                    new_file->bvalue = 0;
                    new_file->bvec = image::vector<3>(0,0,0);
                }
            }
            if(index == 0 && !grad_dev.empty())
                new_file->grad_dev.swap(grad_dev);
            if(index == 0 && !mask.empty())
                new_file->mask.swap(mask);
            dwi_files.push_back(new_file.release());
        }
    }

    return true;
}
Example #2
0
int src(int ac, char *av[])
{
    po::options_description rec_desc("dicom parsing options");
    rec_desc.add_options()
    ("help", "help message")
    ("action", po::value<std::string>(), "src:dicom parsing")
    ("source", po::value<std::string>(), "assign the directory for the dicom files")
    ("recursive", po::value<std::string>(), "search subdirectories")
    ("b_table", po::value<std::string>(), "assign the b-table")
    ("bval", po::value<std::string>(), "assign the b value")
    ("bvec", po::value<std::string>(), "assign the b vector")
    ("output", po::value<std::string>(), "assign the output filename")
    ;
    if(!ac)
    {
        std::cout << rec_desc << std::endl;
        return 1;
    }
    po::variables_map vm;
    po::store(po::command_line_parser(ac, av).options(rec_desc).run(), vm);
    po::notify(vm);


    std::string source = vm["source"].as<std::string>();
    std::string ext;
    if(source.size() > 4)
        ext = std::string(source.end()-4,source.end());

    boost::ptr_vector<DwiHeader> dwi_files;
    QStringList file_list;
    if(ext ==".nii" || ext == ".dcm" || ext == "dseq" || ext == "i.gz")
    {
        std::cout << "image=" << source.c_str() << std::endl;
        file_list << source.c_str();
    }
    else
    {
        std::cout << "load files in directory " << source.c_str() << std::endl;
        QDir directory = QString(source.c_str());
        if(vm.count("recursive"))
        {
            std::cout << "search recursively in the subdir" << std::endl;
            file_list = search_files(source.c_str(),"*.dcm");
        }
        else
        {
            file_list = directory.entryList(QStringList("*.dcm"),QDir::Files|QDir::NoSymLinks);
            if(file_list.empty())
                file_list = directory.entryList(QStringList("*.nii.gz"),QDir::Files|QDir::NoSymLinks);
            for (unsigned int index = 0;index < file_list.size();++index)
                file_list[index] = QString(source.c_str()) + "/" + file_list[index];
        }
        std::cout << "A total of " << file_list.size() <<" files found in the directory" << std::endl;
    }

    if(file_list.empty())
    {
        std::cout << "No file found for creating src" << std::endl;
        return -1;
    }

    if(!load_all_files(file_list,dwi_files))
    {
        std::cout << "Invalid file format" << std::endl;
        return -1;
    }
    if(vm.count("b_table"))
    {
        std::string table_file_name = vm["b_table"].as<std::string>();
        std::ifstream in(table_file_name.c_str());
        if(!in)
        {
            std::cout << "Failed to open b-table" <<std::endl;
            return -1;
        }
        std::string line;
        std::vector<double> b_table;
        while(std::getline(in,line))
        {
            std::istringstream read_line(line);
            std::copy(std::istream_iterator<double>(read_line),
                      std::istream_iterator<double>(),
                      std::back_inserter(b_table));
        }
        if(b_table.size() != dwi_files.size()*4)
        {
            std::cout << "Mismatch between b-table and the loaded images" << std::endl;
            return -1;
        }
        for(unsigned int index = 0,b_index = 0;index < dwi_files.size();++index,b_index += 4)
        {
            dwi_files[index].set_bvalue(b_table[b_index]);
            dwi_files[index].set_bvec(b_table[b_index+1],b_table[b_index+2],b_table[b_index+3]);
        }
        std::cout << "B-table " << table_file_name << " loaded" << std::endl;
    }
    if(vm.count("bval") && vm.count("bvec"))
    {
        std::vector<double> bval,bvec;
        std::cout << "load bval=" << vm["bval"].as<std::string>() << std::endl;
        std::cout << "load bvec=" << vm["bvec"].as<std::string>() << std::endl;
        load_bval(vm["bval"].as<std::string>().c_str(),bval);
        load_bvec(vm["bvec"].as<std::string>().c_str(),bvec);
        if(bval.size() != dwi_files.size())
        {
            std::cout << "Mismatch between bval file and the loaded images" << std::endl;
            return -1;
        }
        if(bvec.size() != dwi_files.size()*3)
        {
            std::cout << "Mismatch between bvec file and the loaded images" << std::endl;
            return -1;
        }
        for(unsigned int index = 0;index < dwi_files.size();++index)
        {
            dwi_files[index].set_bvalue(bval[index]);
            dwi_files[index].set_bvec(bvec[index*3],bvec[index*3+1],bvec[index*3+2]);
        }
    }
    if(dwi_files.empty())
    {
        std::cout << "No file readed. Abort." << std::endl;
        return 1;
    }

    double max_b = 0;
    for(unsigned int index = 0;index < dwi_files.size();++index)
    {
        if(dwi_files[index].get_bvalue() < 100)
            dwi_files[index].set_bvalue(0);
        max_b = std::max(max_b,(double)dwi_files[index].get_bvalue());
    }
    if(max_b == 0.0)
    {
        std::cout << "Cannot find b-table from the header. You may need to load an external b-table using--b_table or --bval and --bvec." << std::endl;
        return 1;
    }
    std::cout << "Output src " << vm["output"].as<std::string>().c_str() << std::endl;
    DwiHeader::output_src(vm["output"].as<std::string>().c_str(),dwi_files,0);
    return 0;
}
Example #3
0
int src(void)
{
    std::string source = po.get("source");
    std::string ext;
    if(source.size() > 4)
        ext = std::string(source.end()-4,source.end());
    std::string output;
    if(po.has("output"))
        output = po.get("output");
    if(QFileInfo(output.c_str()).exists())
    {
        std::cout << output << " exists. skipping..." << std::endl;
        return 1;
    }
    std::vector<std::shared_ptr<DwiHeader> > dwi_files;
    QStringList file_list;
    if(ext ==".nii" || ext == ".dcm" || ext == "dseq" || ext == "i.gz")
    {
        std::cout << "image=" << source.c_str() << std::endl;
        std::istringstream ss(source);
        std::string token;
        while(std::getline(ss, token, ','))
            file_list << source.c_str();
        if(!po.has("output"))
            output = file_list.front().toStdString() + ".src.gz";
    }
    else
    {

        std::cout << "load files in directory " << source.c_str() << std::endl;
        QDir directory = QString(source.c_str());
        if(po.has("recursive"))
        {
            std::cout << "search recursively in the subdir" << std::endl;
            file_list = search_files(source.c_str(),"*.dcm");
        }
        else
        {
            file_list = directory.entryList(QStringList("*.dcm"),QDir::Files|QDir::NoSymLinks);
            if(file_list.empty())
                file_list = directory.entryList(QStringList("*.nii.gz"),QDir::Files|QDir::NoSymLinks);
            if(file_list.empty())
                file_list = directory.entryList(QStringList("*.fdf"),QDir::Files|QDir::NoSymLinks);
            for (unsigned int index = 0;index < file_list.size();++index)
                file_list[index] = QString(source.c_str()) + "/" + file_list[index];
        }
        std::cout << "A total of " << file_list.size() <<" files found in the directory" << std::endl;
        if(!po.has("output"))
            output = source + ".src.gz";
    }

    if(file_list.empty())
    {
        std::cout << "No file found for creating src" << std::endl;
        return 1;
    }

    if(!load_all_files(file_list,dwi_files))
    {
        std::cout << "Invalid file format" << std::endl;
        return 1;
    }
    if(po.has("b_table"))
    {
        std::string table_file_name = po.get("b_table");
        std::ifstream in(table_file_name.c_str());
        if(!in)
        {
            std::cout << "Failed to open b-table" <<std::endl;
            return 1;
        }
        std::string line;
        std::vector<double> b_table;
        while(std::getline(in,line))
        {
            std::istringstream read_line(line);
            std::copy(std::istream_iterator<double>(read_line),
                      std::istream_iterator<double>(),
                      std::back_inserter(b_table));
        }
        if(b_table.size() != dwi_files.size()*4)
        {
            std::cout << "Mismatch between b-table and the loaded images" << std::endl;
            return 1;
        }
        for(unsigned int index = 0,b_index = 0;index < dwi_files.size();++index,b_index += 4)
        {
            dwi_files[index]->set_bvalue(b_table[b_index]);
            dwi_files[index]->set_bvec(b_table[b_index+1],b_table[b_index+2],b_table[b_index+3]);
        }
        std::cout << "B-table " << table_file_name << " loaded" << std::endl;
    }
    if(po.has("bval") && po.has("bvec"))
    {
        std::vector<double> bval,bvec;
        std::cout << "load bval=" << po.get("bval") << std::endl;
        std::cout << "load bvec=" << po.get("bvec") << std::endl;
        load_bval(po.get("bval").c_str(),bval);
        load_bvec(po.get("bvec").c_str(),bvec);
        if(bval.size() != dwi_files.size())
        {
            std::cout << "Mismatch between bval file and the loaded images" << std::endl;
            return 1;
        }
        if(bvec.size() != dwi_files.size()*3)
        {
            std::cout << "Mismatch between bvec file and the loaded images" << std::endl;
            return 1;
        }
        for(unsigned int index = 0;index < dwi_files.size();++index)
        {
            dwi_files[index]->set_bvalue(bval[index]);
            dwi_files[index]->set_bvec(bvec[index*3],bvec[index*3+1],bvec[index*3+2]);
        }
    }
    if(dwi_files.empty())
    {
        std::cout << "No file readed. Abort." << std::endl;
        return 1;
    }

    double max_b = 0;
    for(unsigned int index = 0;index < dwi_files.size();++index)
    {
        if(dwi_files[index]->get_bvalue() < 100)
            dwi_files[index]->set_bvalue(0);
        max_b = std::max(max_b,(double)dwi_files[index]->get_bvalue());
    }
    if(max_b == 0.0)
    {
        std::cout << "Cannot find b-table from the header. You may need to load an external b-table using--b_table or --bval and --bvec." << std::endl;
        return 1;
    }
    std::cout << "Output src to " << output << std::endl;
    DwiHeader::output_src(output.c_str(),dwi_files,
                          po.get<int>("up_sampling",0),
                          po.get<int>("sort_b_table",0));
    return 0;
}