ANTsX / ANTsPy

A fast medical imaging analysis library in Python with algorithms for registration, segmentation, and more.
https://antspyx.readthedocs.io
Apache License 2.0
647 stars 163 forks source link

Dicom read #84

Closed ryancinsight closed 4 years ago

ryancinsight commented 5 years ago

Hello I’ve tried the dicom read function but I don’t want the origin of my images to be the first slice.

Is there any way to implement the imageseriesreader with gdcm series I’d and filenames that itk has for dicom read?

ntustison commented 5 years ago

It doesn't appear that the dicom_read function is meant to be a robust dicom reader and deal with all the associated nuances and possibilities. You're probably much better off using something like [dcm2nii] or implementing your own reader.

ryancinsight commented 5 years ago

Well I'm trying to avoid the reading and writing to disk as it seems something happens to my pixel data randomly. currently I'm doing this with simpleitk. def read_images(direc, filename=None): series_IDs = sitk.ImageSeriesReader.GetGDCMSeriesIDs(direc) series_file_names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(direc, series_IDs[0]) series_reader = sitk.ImageSeriesReader() series_reader.SetNumberOfThreads(mp.cpu_count()) series_reader.SetFileNames(series_file_names) series_reader.MetaDataDictionaryArrayUpdateOn() series_reader.LoadPrivateTagsOn() image=series_reader.Execute() sitk.WriteImage(image, filename, True) return ants.image_read(filename,dimension=3,pixeltype='float', reorient='RAS')

I've been trying to find it but is their a script for itk (if i convert to the python itk instead of simpleitk) to antsimage in python like I'm seeing in the pybind11 examples?

ntustison commented 5 years ago

What about going through numpy as an intermediary?

ryancinsight commented 5 years ago

That would be fine, I believe I saw something similar image read which seems to be doing great for nifti images. I was trying to modify something similar to that but using the itk image series reader with pybind 11 but got stuck when it came to the ITK GDCM wrapping, which is probably do to my lack of understanding of pybind11 and c.

`#include <pybind11/pybind11.h> // header for wrapping code

include <pybind11/stl.h> // header for implicitly casting btwn python-cpp types

include "itkImage.h"

include "itkGDCMImageIO.h"

include "itkGDCMSeriesFileNames.h"

include "itkImageSeriesReader.h"

include "LOCAL_antsImage.h"

namespace py = pybind11; // needed to use "py:"

template py::capsule GDCMHelper( std::string Directory ) { //py::print("at imagereadhelper"); typedef typename NameGeneratorType::Pointer NameGeneratorPointerType; typedef itk::GDCMSeriesFileNames NameGeneratorType;

typename NameGeneratorType::Pointer nameGenerator = NameGeneratorType::New() ;
nameGenerator->SetUseSeriesDetails(True);
nameGenerator->AddSeriesRestriction("0008|0021");
nameGenerator->SetGlobalWarningDisplay(False);
nameGenerator->SetDirectory(Directory.c_str() );

using SeriesIdContainer = std::vector< std::string >;
const SeriesIdContainer & seriesUID = nameGenerator->GetSeriesUIDs();
auto seriesItr = seriesUID.begin();
auto seriesEnd = seriesUID.end();
seriesItr = seriesUID.begin();
seriesIdentifier = seriesItr->c_str();
seriesItr++;
using FileNamesContainer = std::vector< std::string >;
return FileNamesContainer fileNames =nameGenerator->GetFileNames(seriesIdentifier);

} template py::capsule GDCMIOHelper() { typedef typename ImageIOType::Pointer ImagePointerType; typedef itk::GDCMImageIO ImageIOType; typename ImageIOType::Pointer dicomIO = ImageIOType::New(); return dicomIO }

template py::capsule imageSeriesHelper( std::string Directory ) { //py::print("at imagereadhelper"); typedef typename ImageType::Pointer ImagePointerType; typedef itk::ImageSeriesReader< ImageType > ImageReaderType;

typename ImageReaderType::Pointer image_reader = ImageReaderType::New() ;
image_reader->SetImageIO(dicomIO<ImageIOType>);
image_reader->SetFilesName( GDCMHelper<NameGeneratorType>(Directory)) ;
image_reader->Update();

ImagePointerType itkImage = image_reader->GetOutput();
return wrap<ImageType>( itkImage );

}

template py::capsule DicomRead( std::string Directory ) { py::capsule antsImage; antsImage = imageSeriesHelper( Directory ); return antsImage; }`

stnava commented 5 years ago

what was the problem with this approach? looks like you have most of the code needed. did you follow this:

https://github.com/ANTsX/ANTsPy/blob/master/CONTRIBUTING.md

it would most certainly be useful to have this fleshed out.

stnava commented 4 years ago

stale.