Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • f99a844
  • /
  • lib
  • /
  • itkReadDirectoryFilter.h
Raw File Download
Permalinks

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge Iframe embedding
swh:1:cnt:f02eabef2863ad804fb3b1934ae53182a4f5dd53
directory badge Iframe embedding
swh:1:dir:7f21c9a4b7dfafd29925be862d8bd559b684364d
Citations

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
itkReadDirectoryFilter.h
//
//  ReadDirectoryFilter
//  OxShmolliLib
//
//  Created by Konrad Werys on 27/4/17.
//  Copyright © 2017 Konrad Werys. All rights reserved.
//

#ifndef OXSHMOLLI_READDIRECTORY_H
#define OXSHMOLLI_READDIRECTORY_H

#include "CmakeConfigForTomato.h"
#ifdef USE_ITK

#include <iostream>
#include "itkGDCMImageIO.h"
#include "itkGDCMSeriesFileNames.h"
#include "itkImageSeriesReader.h"
#include "itkImageSource.h"
#include "itkStatisticsImageFilter.h"

#include "KWUtil.h"

namespace itk
{
    /** \class ReadDirectoryFilter
     * \brief Scans the Dicom files in a given directory and reads the FIRST series found.
     * Puts the succeeding images in the 3rd dimension.
     * Needs dirName set.
     *
     * Based on:
     * https://itk.org/ITKExamples/src/IO/DICOM/DicomSeriesReadImageWrite3/Documentation.html
     * https://itk.org/Wiki/ITK/Examples/Developer/ImageSource
     * */

    template< typename TImage>
    class ReadDirectoryFilter :public ImageSource< TImage >
    {
    public:

        /** Standard class typedefs. */
        typedef ReadDirectoryFilter         Self;
        typedef ImageSource< TImage > Superclass;
        typedef SmartPointer< Self >  Pointer;

        /** Method for creation through the object factory. */
        itkNewMacro(Self);

        /** Run-time type information (and related methods). */
        itkTypeMacro(ReadDirectoryFilter, ImageSource);

        typedef TImage ImageType;
        typedef typename ImageType::PixelType PixelType;
        typedef itk::ImageSeriesReader< ImageType > ReaderType;
        typedef typename ReaderType::DictionaryRawPointer DictionaryRawPointer;

        //itkGetClassMacro(Dictionary, DictionaryRawPointer);
        itkGetObjectMacro(Reader, ReaderType);

        itkGetMacro(DirName, std::string);
        itkSetMacro(DirName, std::string);

		itkGetMacro(InvTimes, vnl_vector<double>);
		itkSetMacro(InvTimes, vnl_vector<double>);

        itkSetMacro(Verbose, bool);
        itkGetMacro(Verbose, bool);

    protected:
        ReadDirectoryFilter(){
            m_Verbose = false;
        };
        ~ReadDirectoryFilter(){};

        /** Does the real work. */
        virtual void GenerateData() ITK_OVERRIDE;

		/** looks for dicom Tags*/
		virtual vnl_vector<double> FindInversionTimes(ReaderType* reader);

    private:
        ITK_DISALLOW_COPY_AND_ASSIGN(ReadDirectoryFilter);

        std::string m_DirName;
		vnl_vector<double> m_InvTimes;
        // DictionaryRawPointer m_Dictionary;
        typename ReaderType::Pointer m_Reader;
        bool m_Verbose;
    };

    template< class TImage >
    void 
	ReadDirectoryFilter< TImage >
    ::GenerateData(){
        typedef itk::GDCMSeriesFileNames NamesGeneratorType;
        NamesGeneratorType::Pointer nameGenerator = NamesGeneratorType::New();

        nameGenerator->SetUseSeriesDetails(true);
        nameGenerator->AddSeriesRestriction("0008|0021");
        nameGenerator->SetGlobalWarningDisplay(false);
        nameGenerator->SetDirectory(m_DirName);

        try {

            //typedef std::vector< typename ImageType::Pointer >     ImagePointerContainer;
            typedef std::vector< typename std::string >    SeriesIdContainer;
            const SeriesIdContainer & seriesUID = nameGenerator->GetSeriesUIDs();
            SeriesIdContainer::const_iterator seriesItr = seriesUID.begin();
            SeriesIdContainer::const_iterator seriesEnd = seriesUID.end();


            if (seriesItr != seriesEnd) {
                if (GetVerbose()) {
                    std::cout << "The directory: ";
                    std::cout << m_DirName << std::endl;
                    std::cout << "Contains the following DICOM Series: ";
                    std::cout << std::endl;
                }
            } else {
                std::cout << "No DICOMs in: " << m_DirName << std::endl;
            }

            while (seriesItr != seriesEnd) {
                if (GetVerbose()) {
                    std::cout << seriesItr->c_str() << std::endl;
                }
                ++seriesItr;
            }

            seriesItr = seriesUID.begin();
            //while (seriesItr != seriesUID.end()) {
            std::string seriesIdentifier;

            seriesIdentifier = seriesItr->c_str();
            seriesItr++;

            if (GetVerbose()) {
                std::cout << "Reading: ";
                std::cout << seriesIdentifier << std::endl;
            }

            typedef std::vector<std::string> FileNamesContainer;
            FileNamesContainer fileNames;
            fileNames = nameGenerator->GetFileNames(seriesIdentifier);

            m_Reader = ReaderType::New();
            GDCMImageIO::Pointer dicomIO = GDCMImageIO::New();
            m_Reader->SetImageIO(dicomIO);
            m_Reader->SetFileNames(fileNames);

            typename ImageType::Pointer image;
            image = m_Reader->GetOutput();
            image->Update();

            if (GetVerbose()) {
                // rescale image according to Rescale Intercept and Rescale Slope
                typename ImageType::PixelType rescaleIntercept = dicomIO->GetRescaleIntercept();
                typename ImageType::PixelType rescaleSlope = dicomIO->GetRescaleSlope();

                typedef itk::StatisticsImageFilter<ImageType> StatisticsImageFilterType;
                typename StatisticsImageFilterType::Pointer statisticsImageFilter = StatisticsImageFilterType::New();
                statisticsImageFilter->SetInput(image);
                statisticsImageFilter->Update();

                std::cout << "Mean: " << statisticsImageFilter->GetMean();
                std::cout << ", Std.: " << statisticsImageFilter->GetSigma();
                std::cout << ", Min: " << statisticsImageFilter->GetMinimum();
                std::cout << ", Max: " << statisticsImageFilter->GetMaximum() << std::endl;

                std::cout << "rescaleIntercept: " << rescaleIntercept << ", rescaleSlope: " << rescaleSlope
                          << std::endl;

            }

			//typedef itk::ImageRegionIterator< ImageType> IteratorType;
			//IteratorType myiterator(image, image->GetLargestPossibleRegion());
			//myiterator.GoToBegin();
			//while (!myiterator.IsAtEnd()) {
			//	myiterator.Set(rescaleSlope * myiterator.Get() + rescaleIntercept);
			//	++myiterator;
			//}

            // get inversion times
            m_InvTimes = FindInversionTimes(m_Reader);
            if (GetVerbose()) {
                std::cout << "Inversion Times: " << m_InvTimes << std::endl << std::endl;
            }

            //this->m_Dictionary = (*(reader->GetMetaDataDictionaryArray()))[0];

			this->GraftOutput(image);

        } catch (itk::ExceptionObject &ex){
            std::cout << ex << std::endl;
        }
	};

	template< class TImage>
	vnl_vector<double>
	ReadDirectoryFilter< TImage>
	::FindInversionTimes(ReaderType* reader) {

		typedef itk::MetaDataDictionary   DictionaryType;
		typedef itk::MetaDataObject< std::string > MetaDataStringType;

		std::vector<double> invTimesStdVector;

		for (unsigned int i = 0; i < reader->GetMetaDataDictionaryArray()->size(); ++i) {

			const  DictionaryType & dictionary = *reader->GetMetaDataDictionaryArray()->at(i);

			std::string entryId = "0018|0082";
			DictionaryType::ConstIterator tagItr = dictionary.Find(entryId);
			MetaDataStringType::ConstPointer entryvalueStr =
				dynamic_cast<const MetaDataStringType *>(tagItr->second.GetPointer());

			if (entryvalueStr){
				std::string tagvalue = entryvalueStr->GetMetaDataObjectValue();
				invTimesStdVector.push_back(KWUtil::StringToNumber<double>(tagvalue));
			}
		}
		vnl_vector<double> invTimesVnl(invTimesStdVector.data(), invTimesStdVector.size());
		return invTimesVnl;
	};
}// end namespace

#endif // USE_ITK
#endif //OXSHMOLLI_READDIRECTORY_H

Software Heritage — Copyright (C) 2015–2025, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Contact— JavaScript license information— Web API

back to top