std::vector<itk::SmartPointer<mitk::BaseData> > mitk::DummyLsetFileReader::Read() { std::vector<itk::SmartPointer<mitk::BaseData> > result; typedef itk::ImageFileReader<ImageType> FileReaderType; FileReaderType::Pointer reader = FileReaderType::New(); reader->SetFileName(this->GetInputLocation()); itk::NrrdImageIO::Pointer io = itk::NrrdImageIO::New(); reader->SetImageIO(io); reader->Update(); mitk::Image::Pointer img; mitk::CastToMitkImage(reader->GetOutput(),img); result.push_back(img.GetPointer()); return result; }
void NrrdQBallImageReader ::GenerateData() { if ( m_FileName == "") { throw itk::ImageFileReaderException(__FILE__, __LINE__, "Sorry, the filename of the vessel tree to be read is empty!"); } else { try { const std::string& locale = "C"; const std::string& currLocale = setlocale( LC_ALL, NULL ); if ( locale.compare(currLocale)!=0 ) { try { setlocale(LC_ALL, locale.c_str()); } catch(...) { MITK_INFO << "Could not set locale " << locale; } } typedef itk::VectorImage<float,3> ImageType; itk::NrrdImageIO::Pointer io = itk::NrrdImageIO::New(); typedef itk::ImageFileReader<ImageType> FileReaderType; FileReaderType::Pointer reader = FileReaderType::New(); reader->SetImageIO(io); reader->SetFileName(this->m_FileName); reader->Update(); ImageType::Pointer img = reader->GetOutput(); typedef itk::Image<itk::Vector<float,QBALL_ODFSIZE>,3> VecImgType; VecImgType::Pointer vecImg = VecImgType::New(); vecImg->SetSpacing( img->GetSpacing() ); // Set the image spacing vecImg->SetOrigin( img->GetOrigin() ); // Set the image origin vecImg->SetDirection( img->GetDirection() ); // Set the image direction vecImg->SetLargestPossibleRegion( img->GetLargestPossibleRegion()); vecImg->SetBufferedRegion( img->GetLargestPossibleRegion() ); vecImg->Allocate(); itk::ImageRegionIterator<VecImgType> ot (vecImg, vecImg->GetLargestPossibleRegion() ); ot = ot.Begin(); itk::ImageRegionIterator<ImageType> it (img, img->GetLargestPossibleRegion() ); typedef ImageType::PixelType VarPixType; typedef VecImgType::PixelType FixPixType; for (it = it.Begin(); !it.IsAtEnd(); ++it) { VarPixType vec = it.Get(); FixPixType fixVec(vec.GetDataPointer()); ot.Set(fixVec); ++ot; } this->GetOutput()->InitializeByItk(vecImg.GetPointer()); this->GetOutput()->SetVolume(vecImg->GetBufferPointer()); try { setlocale(LC_ALL, currLocale.c_str()); } catch(...) { MITK_INFO << "Could not reset locale " << currLocale; } } catch(std::exception& e) { throw itk::ImageFileReaderException(__FILE__, __LINE__, e.what()); } catch(...) { throw itk::ImageFileReaderException(__FILE__, __LINE__, "Sorry, an error occurred while reading the requested vessel tree file!"); } } }
std::vector<itk::SmartPointer<BaseData> > NrrdTensorImageReader::Read() { std::vector<itk::SmartPointer<mitk::BaseData> > result; std::string location = GetInputLocation(); if ( location == "") { throw itk::ImageFileReaderException(__FILE__, __LINE__, "Sorry, the filename is empty!"); } else { try { mitk::LocaleSwitch localeSwitch("C"); try { std::string fname3 = mitk::IOUtil::GetTempPath()+"/temp_dti.nii.gz"; int c = 0; while( itksys::SystemTools::FileExists(fname3) ) { fname3 = mitk::IOUtil::GetTempPath()+"/temp_dti_" + boost::lexical_cast<std::string>(c) + ".nii.gz"; ++c; } itksys::SystemTools::CopyAFile(location.c_str(), fname3.c_str()); typedef itk::VectorImage<float,3> ImageType; itk::NiftiImageIO::Pointer io = itk::NiftiImageIO::New(); typedef itk::ImageFileReader<ImageType> FileReaderType; FileReaderType::Pointer reader = FileReaderType::New(); reader->SetImageIO(io); reader->SetFileName(fname3); reader->Update(); ImageType::Pointer img = reader->GetOutput(); TensorImage::ItkTensorImageType::Pointer vecImg = TensorImage::ItkTensorImageType::New(); vecImg->SetSpacing( img->GetSpacing() ); // Set the image spacing vecImg->SetOrigin( img->GetOrigin() ); // Set the image origin vecImg->SetDirection( img->GetDirection() ); // Set the image direction vecImg->SetRegions( img->GetLargestPossibleRegion()); vecImg->Allocate(); itk::ImageRegionIterator<TensorImage::ItkTensorImageType> ot (vecImg, vecImg->GetLargestPossibleRegion() ); ot.GoToBegin(); itk::ImageRegionIterator<ImageType> it (img, img->GetLargestPossibleRegion() ); it.GoToBegin(); typedef ImageType::PixelType VarPixType; typedef TensorImage::PixelType FixPixType; int numComponents = img->GetNumberOfComponentsPerPixel(); if (numComponents==6) { MITK_INFO << "Trying to load dti as 6-comp nifti ..."; while (!it.IsAtEnd()) { VarPixType vec = it.Get(); FixPixType fixVec(vec.GetDataPointer()); TensorImage::PixelType tensor; tensor.SetElement(0, vec.GetElement(0)); tensor.SetElement(1, vec.GetElement(1)); tensor.SetElement(2, vec.GetElement(2)); tensor.SetElement(3, vec.GetElement(3)); tensor.SetElement(4, vec.GetElement(4)); tensor.SetElement(5, vec.GetElement(5)); fixVec = tensor; ot.Set(fixVec); ++ot; ++it; } } else if(numComponents==9) { MITK_INFO << "Trying to load dti as 9-comp nifti ..."; while (!it.IsAtEnd()) { VarPixType vec = it.Get(); TensorImage::PixelType tensor; tensor.SetElement(0, vec.GetElement(0)); tensor.SetElement(1, vec.GetElement(1)); tensor.SetElement(2, vec.GetElement(2)); tensor.SetElement(3, vec.GetElement(4)); tensor.SetElement(4, vec.GetElement(5)); tensor.SetElement(5, vec.GetElement(8)); FixPixType fixVec(tensor); ot.Set(fixVec); ++ot; ++it; } } else if (numComponents==1) { MITK_INFO << "Trying to load dti as 4D nifti ..."; typedef itk::Image<float,4> ImageType; typedef itk::ImageFileReader<ImageType> FileReaderType; FileReaderType::Pointer reader = FileReaderType::New(); reader->SetImageIO(io); reader->SetFileName(fname3); reader->Update(); ImageType::Pointer img = reader->GetOutput(); itk::Size<4> size = img->GetLargestPossibleRegion().GetSize(); while (!ot.IsAtEnd()) { TensorImage::PixelType tensor; ImageType::IndexType idx; idx[0] = ot.GetIndex()[0]; idx[1] = ot.GetIndex()[1]; idx[2] = ot.GetIndex()[2]; if (size[3]==6) { for (unsigned int te=0; te<size[3]; te++) { idx[3] = te; tensor.SetElement(te, img->GetPixel(idx)); } } else if (size[3]==9) { idx[3] = 0; tensor.SetElement(0, img->GetPixel(idx)); idx[3] = 1; tensor.SetElement(1, img->GetPixel(idx)); idx[3] = 2; tensor.SetElement(2, img->GetPixel(idx)); idx[3] = 4; tensor.SetElement(3, img->GetPixel(idx)); idx[3] = 5; tensor.SetElement(4, img->GetPixel(idx)); idx[3] = 8; tensor.SetElement(5, img->GetPixel(idx)); } else throw itk::ImageFileReaderException(__FILE__, __LINE__, "Unknown number of components for DTI file. Should be 6 or 9!"); FixPixType fixVec(tensor); ot.Set(fixVec); ++ot; } } OutputType::Pointer resultImage = OutputType::New(); resultImage->InitializeByItk( vecImg.GetPointer() ); resultImage->SetVolume( vecImg->GetBufferPointer() ); result.push_back( resultImage.GetPointer() ); } catch(...) { MITK_INFO << "Trying to load dti as nrrd ..."; typedef itk::VectorImage<float,3> ImageType; itk::NrrdImageIO::Pointer io = itk::NrrdImageIO::New(); typedef itk::ImageFileReader<ImageType> FileReaderType; FileReaderType::Pointer reader = FileReaderType::New(); reader->SetImageIO(io); reader->SetFileName(location); reader->Update(); ImageType::Pointer img = reader->GetOutput(); TensorImage::ItkTensorImageType::Pointer vecImg = TensorImage::ItkTensorImageType::New(); vecImg->SetSpacing( img->GetSpacing() ); // Set the image spacing vecImg->SetOrigin( img->GetOrigin() ); // Set the image origin vecImg->SetDirection( img->GetDirection() ); // Set the image direction vecImg->SetRegions( img->GetLargestPossibleRegion()); vecImg->Allocate(); itk::ImageRegionIterator<TensorImage::ItkTensorImageType> ot (vecImg, vecImg->GetLargestPossibleRegion() ); ot.GoToBegin(); itk::ImageRegionIterator<ImageType> it (img, img->GetLargestPossibleRegion() ); it.GoToBegin(); typedef ImageType::PixelType VarPixType; typedef TensorImage::PixelType FixPixType; int numComponents = img->GetNumberOfComponentsPerPixel(); itk::MetaDataDictionary imgMetaDictionary = img->GetMetaDataDictionary(); std::vector<std::string> imgMetaKeys = imgMetaDictionary.GetKeys(); std::vector<std::string>::const_iterator itKey = imgMetaKeys.begin(); std::string metaString; bool readFrame = false; double xx, xy, xz, yx, yy, yz, zx, zy, zz; MeasurementFrameType measFrame; measFrame.SetIdentity(); MeasurementFrameType measFrameTransp; measFrameTransp.SetIdentity(); for (; itKey != imgMetaKeys.end(); itKey ++) { itk::ExposeMetaData<std::string> (imgMetaDictionary, *itKey, metaString); if (itKey->find("measurement frame") != std::string::npos) { sscanf(metaString.c_str(), " ( %lf , %lf , %lf ) ( %lf , %lf , %lf ) ( %lf , %lf , %lf ) \n", &xx, &xy, &xz, &yx, &yy, &yz, &zx, &zy, &zz); if (xx>10e-10 || xy>10e-10 || xz>10e-10 || yx>10e-10 || yy>10e-10 || yz>10e-10 || zx>10e-10 || zy>10e-10 || zz>10e-10 ) { readFrame = true; measFrame(0,0) = xx; measFrame(0,1) = xy; measFrame(0,2) = xz; measFrame(1,0) = yx; measFrame(1,1) = yy; measFrame(1,2) = yz; measFrame(2,0) = zx; measFrame(2,1) = zy; measFrame(2,2) = zz; measFrameTransp = measFrame.GetTranspose(); } } } if (numComponents==6) { while (!it.IsAtEnd()) { // T'=RTR' VarPixType vec = it.Get(); FixPixType fixVec(vec.GetDataPointer()); if(readFrame) { TensorImage::PixelType tensor; tensor.SetElement(0, vec.GetElement(0)); tensor.SetElement(1, vec.GetElement(1)); tensor.SetElement(2, vec.GetElement(2)); tensor.SetElement(3, vec.GetElement(3)); tensor.SetElement(4, vec.GetElement(4)); tensor.SetElement(5, vec.GetElement(5)); tensor = ConvertMatrixTypeToFixedArrayType(tensor.PreMultiply(measFrame)); tensor = ConvertMatrixTypeToFixedArrayType(tensor.PostMultiply(measFrameTransp)); fixVec = tensor; } ot.Set(fixVec); ++ot; ++it; } } else if(numComponents==9) { while (!it.IsAtEnd()) { VarPixType vec = it.Get(); TensorImage::PixelType tensor; tensor.SetElement(0, vec.GetElement(0)); tensor.SetElement(1, vec.GetElement(1)); tensor.SetElement(2, vec.GetElement(2)); tensor.SetElement(3, vec.GetElement(4)); tensor.SetElement(4, vec.GetElement(5)); tensor.SetElement(5, vec.GetElement(8)); if(readFrame) { tensor = ConvertMatrixTypeToFixedArrayType(tensor.PreMultiply(measFrame)); tensor = ConvertMatrixTypeToFixedArrayType(tensor.PostMultiply(measFrameTransp)); } FixPixType fixVec(tensor); ot.Set(fixVec); ++ot; ++it; } } else if (numComponents==1) { typedef itk::Image<float,4> ImageType; itk::NrrdImageIO::Pointer io = itk::NrrdImageIO::New(); typedef itk::ImageFileReader<ImageType> FileReaderType; FileReaderType::Pointer reader = FileReaderType::New(); reader->SetImageIO(io); reader->SetFileName(location); reader->Update(); ImageType::Pointer img = reader->GetOutput(); itk::Size<4> size = img->GetLargestPossibleRegion().GetSize(); while (!ot.IsAtEnd()) { TensorImage::PixelType tensor; ImageType::IndexType idx; idx[0] = ot.GetIndex()[0]; idx[1] = ot.GetIndex()[1]; idx[2] = ot.GetIndex()[2]; if (size[3]==6) { for (unsigned int te=0; te<size[3]; te++) { idx[3] = te; tensor.SetElement(te, img->GetPixel(idx)); } } else if (size[3]==9) { idx[3] = 0; tensor.SetElement(0, img->GetPixel(idx)); idx[3] = 1; tensor.SetElement(1, img->GetPixel(idx)); idx[3] = 2; tensor.SetElement(2, img->GetPixel(idx)); idx[3] = 4; tensor.SetElement(3, img->GetPixel(idx)); idx[3] = 5; tensor.SetElement(4, img->GetPixel(idx)); idx[3] = 8; tensor.SetElement(5, img->GetPixel(idx)); } else throw itk::ImageFileReaderException(__FILE__, __LINE__, "Unknown number of komponents for DTI file. Should be 6 or 9!"); if(readFrame) { tensor = ConvertMatrixTypeToFixedArrayType(tensor.PreMultiply(measFrame)); tensor = ConvertMatrixTypeToFixedArrayType(tensor.PostMultiply(measFrameTransp)); } FixPixType fixVec(tensor); ot.Set(fixVec); ++ot; } } else { throw itk::ImageFileReaderException(__FILE__, __LINE__, "Image has wrong number of pixel components!"); } OutputType::Pointer resultImage = OutputType::New(); resultImage->InitializeByItk( vecImg.GetPointer() ); resultImage->SetVolume( vecImg->GetBufferPointer() ); result.push_back( resultImage.GetPointer() ); } } catch(std::exception& e) { throw itk::ImageFileReaderException(__FILE__, __LINE__, e.what()); } catch(...) { throw itk::ImageFileReaderException(__FILE__, __LINE__, "Sorry, an error occurred while reading the requested DTI file!"); } } return result; }
void NrrdTensorImageReader ::GenerateData() { if ( m_FileName == "") { throw itk::ImageFileReaderException(__FILE__, __LINE__, "Sorry, the filename is empty!"); } else { try { const std::string& locale = "C"; const std::string& currLocale = setlocale( LC_ALL, NULL ); if ( locale.compare(currLocale)!=0 ) { try { setlocale(LC_ALL, locale.c_str()); } catch(...) { MITK_INFO << "Could not set locale " << locale; } } try { MITK_INFO << "Trying to load dti as nifti ..."; std::string fname3 = "temp_dti.nii"; itksys::SystemTools::CopyAFile(m_FileName.c_str(), fname3.c_str()); typedef itk::Image<float,4> ImageType; itk::NiftiImageIO::Pointer io = itk::NiftiImageIO::New(); typedef itk::ImageFileReader<ImageType> FileReaderType; FileReaderType::Pointer reader = FileReaderType::New(); reader->SetImageIO(io); reader->SetFileName(fname3); reader->Update(); itksys::SystemTools::RemoveFile(fname3.c_str()); ImageType::Pointer img = reader->GetOutput(); itk::Size<4> size = img->GetLargestPossibleRegion().GetSize(); itk::ImageRegion< 3 > region; region.SetSize(0,size[0]); region.SetSize(1,size[1]); region.SetSize(2,size[2]); itk::Vector<double,3> spacing; spacing[0] = img->GetSpacing()[0]; spacing[1] = img->GetSpacing()[1]; spacing[2] = img->GetSpacing()[2]; itk::Point<double,3> origin; origin[0] = img->GetOrigin()[0]; origin[1] = img->GetOrigin()[1]; origin[2] = img->GetOrigin()[2]; itk::Matrix<double,3,3> direction; direction[0][0] = img->GetDirection()[0][0]; direction[1][0] = img->GetDirection()[1][0]; direction[2][0] = img->GetDirection()[2][0]; direction[0][1] = img->GetDirection()[0][1]; direction[1][1] = img->GetDirection()[1][1]; direction[2][1] = img->GetDirection()[2][1]; direction[0][2] = img->GetDirection()[0][2]; direction[1][2] = img->GetDirection()[1][2]; direction[2][2] = img->GetDirection()[2][2]; typedef itk::Image<itk::DiffusionTensor3D<float>,3> VecImgType; typedef VecImgType::PixelType FixPixType; VecImgType::Pointer vecImg = VecImgType::New(); vecImg->SetSpacing( spacing ); vecImg->SetOrigin( origin ); vecImg->SetDirection( direction ); vecImg->SetRegions( region ); vecImg->Allocate(); itk::ImageRegionIterator<VecImgType> ot (vecImg, vecImg->GetLargestPossibleRegion() ); ot = ot.Begin(); while (!ot.IsAtEnd()) { itk::DiffusionTensor3D<float> tensor; ImageType::IndexType idx; idx[0] = ot.GetIndex()[0]; idx[1] = ot.GetIndex()[1]; idx[2] = ot.GetIndex()[2]; if (size[3]==6) { for (int te=0; te<size[3]; te++) { idx[3] = te; tensor.SetElement(te, img->GetPixel(idx)); } } else if (size[3]==9) { idx[3] = 0; tensor.SetElement(0, img->GetPixel(idx)); idx[3] = 1; tensor.SetElement(1, img->GetPixel(idx)); idx[3] = 2; tensor.SetElement(2, img->GetPixel(idx)); idx[3] = 4; tensor.SetElement(3, img->GetPixel(idx)); idx[3] = 5; tensor.SetElement(4, img->GetPixel(idx)); idx[3] = 8; tensor.SetElement(5, img->GetPixel(idx)); } else throw itk::ImageFileReaderException(__FILE__, __LINE__, "Unknown number of komponents for DTI file. Should be 6 or 9!"); FixPixType fixVec(tensor); ot.Set(fixVec); ++ot; } this->GetOutput()->InitializeByItk(vecImg.GetPointer()); this->GetOutput()->SetVolume(vecImg->GetBufferPointer()); } catch(...) { MITK_INFO << "Trying to load dti as nrrd ..."; typedef itk::VectorImage<float,3> ImageType; itk::NrrdImageIO::Pointer io = itk::NrrdImageIO::New(); typedef itk::ImageFileReader<ImageType> FileReaderType; FileReaderType::Pointer reader = FileReaderType::New(); reader->SetImageIO(io); reader->SetFileName(this->m_FileName); reader->Update(); ImageType::Pointer img = reader->GetOutput(); typedef itk::Image<itk::DiffusionTensor3D<float>,3> VecImgType; VecImgType::Pointer vecImg = VecImgType::New(); vecImg->SetSpacing( img->GetSpacing() ); // Set the image spacing vecImg->SetOrigin( img->GetOrigin() ); // Set the image origin vecImg->SetDirection( img->GetDirection() ); // Set the image direction vecImg->SetRegions( img->GetLargestPossibleRegion()); vecImg->Allocate(); itk::ImageRegionIterator<VecImgType> ot (vecImg, vecImg->GetLargestPossibleRegion() ); ot = ot.Begin(); itk::ImageRegionIterator<ImageType> it (img, img->GetLargestPossibleRegion() ); it = it.Begin(); typedef ImageType::PixelType VarPixType; typedef VecImgType::PixelType FixPixType; int numComponents = img->GetNumberOfComponentsPerPixel(); itk::MetaDataDictionary imgMetaDictionary = img->GetMetaDataDictionary(); std::vector<std::string> imgMetaKeys = imgMetaDictionary.GetKeys(); std::vector<std::string>::const_iterator itKey = imgMetaKeys.begin(); std::string metaString; bool readFrame = false; double xx, xy, xz, yx, yy, yz, zx, zy, zz; MeasurementFrameType measFrame; measFrame.SetIdentity(); MeasurementFrameType measFrameTransp; measFrameTransp.SetIdentity(); for (; itKey != imgMetaKeys.end(); itKey ++) { itk::ExposeMetaData<std::string> (imgMetaDictionary, *itKey, metaString); if (itKey->find("measurement frame") != std::string::npos) { sscanf(metaString.c_str(), " ( %lf , %lf , %lf ) ( %lf , %lf , %lf ) ( %lf , %lf , %lf ) \n", &xx, &xy, &xz, &yx, &yy, &yz, &zx, &zy, &zz); if (xx>10e-10 || xy>10e-10 || xz>10e-10 || yx>10e-10 || yy>10e-10 || yz>10e-10 || zx>10e-10 || zy>10e-10 || zz>10e-10 ) { readFrame = true; measFrame(0,0) = xx; measFrame(0,1) = xy; measFrame(0,2) = xz; measFrame(1,0) = yx; measFrame(1,1) = yy; measFrame(1,2) = yz; measFrame(2,0) = zx; measFrame(2,1) = zy; measFrame(2,2) = zz; measFrameTransp = measFrame.GetTranspose(); } } } if (numComponents==6) { while (!it.IsAtEnd()) { // T'=RTR' VarPixType vec = it.Get(); FixPixType fixVec(vec.GetDataPointer()); if(readFrame) { itk::DiffusionTensor3D<float> tensor; tensor.SetElement(0, vec.GetElement(0)); tensor.SetElement(1, vec.GetElement(1)); tensor.SetElement(2, vec.GetElement(2)); tensor.SetElement(3, vec.GetElement(3)); tensor.SetElement(4, vec.GetElement(4)); tensor.SetElement(5, vec.GetElement(5)); tensor = ConvertMatrixTypeToFixedArrayType(tensor.PreMultiply(measFrame)); tensor = ConvertMatrixTypeToFixedArrayType(tensor.PostMultiply(measFrameTransp)); fixVec = tensor; } ot.Set(fixVec); ++ot; ++it; } } else if(numComponents==9) { while (!it.IsAtEnd()) { VarPixType vec = it.Get(); itk::DiffusionTensor3D<float> tensor; tensor.SetElement(0, vec.GetElement(0)); tensor.SetElement(1, vec.GetElement(1)); tensor.SetElement(2, vec.GetElement(2)); tensor.SetElement(3, vec.GetElement(4)); tensor.SetElement(4, vec.GetElement(5)); tensor.SetElement(5, vec.GetElement(8)); if(readFrame) { tensor = ConvertMatrixTypeToFixedArrayType(tensor.PreMultiply(measFrame)); tensor = ConvertMatrixTypeToFixedArrayType(tensor.PostMultiply(measFrameTransp)); } FixPixType fixVec(tensor); ot.Set(fixVec); ++ot; ++it; } } else if (numComponents==1) { typedef itk::Image<float,4> ImageType; itk::NrrdImageIO::Pointer io = itk::NrrdImageIO::New(); typedef itk::ImageFileReader<ImageType> FileReaderType; FileReaderType::Pointer reader = FileReaderType::New(); reader->SetImageIO(io); reader->SetFileName(this->m_FileName); reader->Update(); ImageType::Pointer img = reader->GetOutput(); itk::Size<4> size = img->GetLargestPossibleRegion().GetSize(); MITK_INFO << size; while (!ot.IsAtEnd()) { itk::DiffusionTensor3D<float> tensor; ImageType::IndexType idx; idx[0] = ot.GetIndex()[0]; idx[1] = ot.GetIndex()[1]; idx[2] = ot.GetIndex()[2]; if (size[3]==6) { for (int te=0; te<size[3]; te++) { idx[3] = te; tensor.SetElement(te, img->GetPixel(idx)); } // idx[3] = 0; // tensor.SetElement(0, img->GetPixel(idx)); // idx[3] = 1; // tensor.SetElement(1, img->GetPixel(idx)); // idx[3] = 3; // tensor.SetElement(2, img->GetPixel(idx)); // idx[3] = 2; // tensor.SetElement(3, img->GetPixel(idx)); // idx[3] = 4; // tensor.SetElement(4, img->GetPixel(idx)); // idx[3] = 5; // tensor.SetElement(5, img->GetPixel(idx)); } else if (size[3]==9) { idx[3] = 0; tensor.SetElement(0, img->GetPixel(idx)); idx[3] = 1; tensor.SetElement(1, img->GetPixel(idx)); idx[3] = 2; tensor.SetElement(2, img->GetPixel(idx)); idx[3] = 4; tensor.SetElement(3, img->GetPixel(idx)); idx[3] = 5; tensor.SetElement(4, img->GetPixel(idx)); idx[3] = 8; tensor.SetElement(5, img->GetPixel(idx)); } else throw itk::ImageFileReaderException(__FILE__, __LINE__, "Unknown number of komponents for DTI file. Should be 6 or 9!"); if(readFrame) { tensor = ConvertMatrixTypeToFixedArrayType(tensor.PreMultiply(measFrame)); tensor = ConvertMatrixTypeToFixedArrayType(tensor.PostMultiply(measFrameTransp)); } FixPixType fixVec(tensor); ot.Set(fixVec); ++ot; } } else { throw itk::ImageFileReaderException(__FILE__, __LINE__, "Image has wrong number of pixel components!"); } this->GetOutput()->InitializeByItk(vecImg.GetPointer()); this->GetOutput()->SetVolume(vecImg->GetBufferPointer()); } try { setlocale(LC_ALL, currLocale.c_str()); } catch(...) { MITK_INFO << "Could not reset locale " << currLocale; } } catch(std::exception& e) { throw itk::ImageFileReaderException(__FILE__, __LINE__, e.what()); } catch(...) { throw itk::ImageFileReaderException(__FILE__, __LINE__, "Sorry, an error occurred while reading the requested DTI file!"); } } }
int main(int argc, char **argv) { std::cout << "nrrd2vtk. Version 1.3. Works bboth ways."<< std::endl; if(argc<4) { std::cout << "nrrd2vtk input.nrrd output.vtk uint|short|uchar"<< std::endl; return 0; } const char* inp = argv[1]; const char* outp = argv[2]; const char* type = argv[3]; if(strcmp(type, "uint")==0) { typedef itk::Image< unsigned int, 3 > ImageType; typedef ImageType::Pointer ImageTypePointer; typedef itk::ImageFileReader<ImageType> FileReaderType; FileReaderType::Pointer reader = FileReaderType::New(); reader->SetFileName(inp); reader->Update(); typedef itk::ImageFileWriter<ImageType> FileWriterType; FileWriterType::Pointer writer = FileWriterType::New(); writer->SetFileName(outp); writer->SetInput( reader->GetOutput() ); writer->Update(); } else if(strcmp(type, "short")==0) { typedef itk::Image< short, 3 > ImageType; typedef ImageType::Pointer ImageTypePointer; typedef itk::ImageFileReader<ImageType> FileReaderType; FileReaderType::Pointer reader = FileReaderType::New(); reader->SetFileName(inp); reader->Update(); typedef itk::ImageFileWriter<ImageType> FileWriterType; FileWriterType::Pointer writer = FileWriterType::New(); writer->SetFileName(outp); writer->SetInput( reader->GetOutput() ); writer->Update(); } else if(strcmp(type, "uchar")==0) { typedef itk::Image< unsigned char, 3 > ImageType; typedef ImageType::Pointer ImageTypePointer; typedef itk::ImageFileReader<ImageType> FileReaderType; FileReaderType::Pointer reader = FileReaderType::New(); reader->SetFileName(inp); reader->Update(); typedef itk::ImageFileWriter<ImageType> FileWriterType; FileWriterType::Pointer writer = FileWriterType::New(); writer->SetFileName(outp); writer->SetInput( reader->GetOutput() ); writer->Update(); } /* ConnectorType::Pointer connector = ConnectorType::New(); connector->SetInput(reader->GetOutput()); connector->Update(); CommonTools::SaveImage( connector->GetOutput(), outp ); */ return 0; }
int main(int argc, char**argv) { int nx, ny, nz, nbytes, direction, nx8, ny8, nz8, k; // int n, x, y, z, dx, dy; long long width, height, depth, xysize; char *dataFile, *tiffFile; bool use_compression = true, data2tiff, compressdata = true; FILE *fpdata; if (argc != 4) { printf("Usage: maketiff dataFile tiffFile direction\n"); printf(" direction = 0 (tiff to data) = 1 (data to tiff)\n"); return 1; } dataFile = argv[1]; tiffFile = argv[2]; sscanf(argv[3],"%d",&direction); if (direction == 0) { data2tiff = false; printf("Output data file: %s\n",dataFile); } else { data2tiff = true; printf("Output image file: %s\n",tiffFile); } if (data2tiff) { // create tiff file fpdata = fopen(dataFile,"rb"); if (fpdata == NULL) { return 2; } fread(&nx,4,1,fpdata); fread(&ny,4,1,fpdata); fread(&nz,4,1,fpdata); // fread(&nbytes,4,1,fpdata); //if (nbytes != nx*ny*nz) { // printf("Error: this is a compressed data file\n"); // return 10; //} width = nx; height = ny; depth = nz; xysize = width*height; printf("Desired image dimensions: width, height, depth: %d %d %d\n",width,height,depth); ImageType::Pointer im = ImageType::New(); ImageType::SizeType imsize; imsize[0] = width; imsize[1] = height; imsize[2] = depth; ImageType::IndexType imstart; imstart[0] = 0; imstart[1] = 0; imstart[2] = 0; ImageType::RegionType imregion; imregion.SetSize(imsize); imregion.SetIndex(imstart); im->SetRegions(imregion); im->Allocate(); p = (unsigned char *)(im->GetBufferPointer()); nbytes = nx*ny*nz; fread(p,nbytes,1,fpdata); typedef itk::ImageFileWriter<ImageType> FileWriterType; FileWriterType::Pointer writer = FileWriterType::New(); writer->SetFileName(tiffFile); writer->SetInput(im); if (use_compression) { writer->UseCompressionOn(); } try { writer->Update(); } catch (itk::ExceptionObject &e) { std::cout << e << std::endl; return 3; } if (use_compression) { printf("Created compressed image file: %s\n",tiffFile); } else { printf("Created uncompressed image file: %s\n",tiffFile); } } else { // create data file typedef itk::ImageFileReader<ImageType> FileReaderType; FileReaderType::Pointer reader = FileReaderType::New(); reader->SetFileName(tiffFile); try { reader->Update(); } catch (itk::ExceptionObject &e) { std::cout << e << std::endl; printf("Read error on input file\n"); return 2; // Read error on input tiff file } im = reader->GetOutput(); width = im->GetLargestPossibleRegion().GetSize()[0]; height = im->GetLargestPossibleRegion().GetSize()[1]; depth = im->GetLargestPossibleRegion().GetSize()[2]; printf("Image dimensions: width, height, depth: %d %d %d\n",width,height,depth); p = (unsigned char *)(im->GetBufferPointer()); fpdata = fopen(dataFile,"wb"); if (fpdata == NULL) { return 2; } nx = width; ny = height; nz = depth; if (compressdata) { // create compressed buffer pc from p using (0,1) // First round up nx, ny, nz to a multiple of 8 k = nx%8; if (k == 0) nx8 = nx; else nx8 = nx + 8-k; k = ny%8; if (k == 0) ny8 = ny; else ny8 = ny + 8-k; k = nx%8; if (k == 0) nz8 = nz; else nz8 = nz + 8-k; nbytes = nx8*ny8*nz8/8; pc = (unsigned char *)malloc(nbytes*sizeof(unsigned char)); int kbit = 0; int kbyte = 0; k = 0; unsigned char pcbyte = 0; for (int iz=0; iz<nz8; iz++) { bool zok = (iz<nz); for (int iy=0; iy<ny8; iy++) { bool yok = (iy<ny); for (int ix=0; ix<nx8; ix++) { bool xok = (ix<nx); if (xok && yok && zok) { // set bit kbit of pcbyte to (0,1) based on p[k] if (p[k] == 1 || p[k] == 255) { pcbyte |= 1 << kbit; } if (kbyte < 4) printf("ix,iy,iz k p[k] pcbyte: %d %d %d %d %d %d\n", ix,iy,iz,k,p[k],pcbyte); k++; } kbit++; if (kbit == 8) { pc[kbyte] = pcbyte; if (kbyte == 0) { printf("byte:%d %d\n",kbyte,pc[kbyte]); } kbyte++; pcbyte = 0; kbit = 0; } } } } } fwrite(&nx,4,1,fpdata); fwrite(&ny,4,1,fpdata); fwrite(&nz,4,1,fpdata); fwrite(&nx8,4,1,fpdata); fwrite(&ny8,4,1,fpdata); fwrite(&nz8,4,1,fpdata); fwrite(&nbytes,4,1,fpdata); printf("nx,ny,nz nx8,ny8,nz8: %d %d %d %d %d %d\n",nx,ny,nz,nx8,ny8,nz8); width = nx; height = ny; depth = nz; xysize = width*height; // write(nfdata) nx,ny,nz,(((imagedata(ix,iy,iz),ix=1,nx),iy=1,ny),iz=1,nz) // try this... if (compressdata) { fwrite(pc,1,nbytes,fpdata); } else { fwrite(p,1,nx*ny*nz,fpdata); } fclose(fpdata); } return 0; }
void NrrdTensorImageReader ::GenerateData() { if ( m_FileName == "") { throw itk::ImageFileReaderException(__FILE__, __LINE__, "Sorry, the filename is empty!"); } else { try { const std::string& locale = "C"; const std::string& currLocale = setlocale( LC_ALL, NULL ); if ( locale.compare(currLocale)!=0 ) { try { setlocale(LC_ALL, locale.c_str()); } catch(...) { MITK_INFO << "Could not set locale " << locale; } } typedef itk::VectorImage<float,3> ImageType; itk::NrrdImageIO::Pointer io = itk::NrrdImageIO::New(); typedef itk::ImageFileReader<ImageType> FileReaderType; FileReaderType::Pointer reader = FileReaderType::New(); reader->SetImageIO(io); reader->SetFileName(this->m_FileName); reader->Update(); ImageType::Pointer img = reader->GetOutput(); typedef itk::Image<itk::DiffusionTensor3D<float>,3> VecImgType; VecImgType::Pointer vecImg = VecImgType::New(); vecImg->SetSpacing( img->GetSpacing() ); // Set the image spacing vecImg->SetOrigin( img->GetOrigin() ); // Set the image origin vecImg->SetDirection( img->GetDirection() ); // Set the image direction vecImg->SetRegions( img->GetLargestPossibleRegion()); vecImg->Allocate(); itk::ImageRegionIterator<VecImgType> ot (vecImg, vecImg->GetLargestPossibleRegion() ); ot = ot.Begin(); itk::ImageRegionIterator<ImageType> it (img, img->GetLargestPossibleRegion() ); it = it.Begin(); typedef ImageType::PixelType VarPixType; typedef VecImgType::PixelType FixPixType; int numComponents = img->GetNumberOfComponentsPerPixel(); itk::MetaDataDictionary imgMetaDictionary = img->GetMetaDataDictionary(); std::vector<std::string> imgMetaKeys = imgMetaDictionary.GetKeys(); std::vector<std::string>::const_iterator itKey = imgMetaKeys.begin(); std::string metaString; bool readFrame = false; double xx, xy, xz, yx, yy, yz, zx, zy, zz; MeasurementFrameType measFrame; measFrame.SetIdentity(); MeasurementFrameType measFrameTransp; measFrameTransp.SetIdentity(); for (; itKey != imgMetaKeys.end(); itKey ++) { itk::ExposeMetaData<std::string> (imgMetaDictionary, *itKey, metaString); if (itKey->find("measurement frame") != std::string::npos) { sscanf(metaString.c_str(), " ( %lf , %lf , %lf ) ( %lf , %lf , %lf ) ( %lf , %lf , %lf ) \n", &xx, &xy, &xz, &yx, &yy, &yz, &zx, &zy, &zz); if (xx>10e-10 || xy>10e-10 || xz>10e-10 || yx>10e-10 || yy>10e-10 || yz>10e-10 || zx>10e-10 || zy>10e-10 || zz>10e-10 ) { readFrame = true; measFrame(0,0) = xx; measFrame(0,1) = xy; measFrame(0,2) = xz; measFrame(1,0) = yx; measFrame(1,1) = yy; measFrame(1,2) = yz; measFrame(2,0) = zx; measFrame(2,1) = zy; measFrame(2,2) = zz; measFrameTransp = measFrame.GetTranspose(); } } } if (numComponents==6) { while (!it.IsAtEnd()) { // T'=RTR' VarPixType vec = it.Get(); FixPixType fixVec(vec.GetDataPointer()); if(readFrame) { itk::DiffusionTensor3D<float> tensor; tensor.SetElement(0, vec.GetElement(0)); tensor.SetElement(1, vec.GetElement(1)); tensor.SetElement(2, vec.GetElement(2)); tensor.SetElement(3, vec.GetElement(3)); tensor.SetElement(4, vec.GetElement(4)); tensor.SetElement(5, vec.GetElement(5)); tensor = tensor.PreMultiply(measFrame); tensor = tensor.PostMultiply(measFrameTransp); fixVec = tensor; } ot.Set(fixVec); ++ot; ++it; } } else if(numComponents==9) { while (!it.IsAtEnd()) { VarPixType vec = it.Get(); itk::DiffusionTensor3D<float> tensor; tensor.SetElement(0, vec.GetElement(0)); tensor.SetElement(1, vec.GetElement(1)); tensor.SetElement(2, vec.GetElement(2)); tensor.SetElement(3, vec.GetElement(4)); tensor.SetElement(4, vec.GetElement(5)); tensor.SetElement(5, vec.GetElement(8)); if(readFrame) { tensor = tensor.PreMultiply(measFrame); tensor = tensor.PostMultiply(measFrameTransp); } FixPixType fixVec(tensor); ot.Set(fixVec); ++ot; ++it; } } else { throw itk::ImageFileReaderException(__FILE__, __LINE__, "Image has wrong number of pixel components!"); } this->GetOutput()->InitializeByItk(vecImg.GetPointer()); this->GetOutput()->SetVolume(vecImg->GetBufferPointer()); try { setlocale(LC_ALL, currLocale.c_str()); } catch(...) { MITK_INFO << "Could not reset locale " << currLocale; } } catch(std::exception& e) { throw itk::ImageFileReaderException(__FILE__, __LINE__, e.what()); } catch(...) { throw itk::ImageFileReaderException(__FILE__, __LINE__, "Sorry, an error occurred while reading the requested DTI file!"); } } }
//---------------------------------------------------------------------------- //---------------------------------------------------------------------------- int main(int argc, char**argv) { int width, height, depth, xysize, compress, err; float peelsize; float ball_radius; char *inputFile, *erodedFile, *binFile; bool compressdata = true; bool make_binFile; if (argc != 5 && argc != 6) { printf("Usage: erode input_tiff eroded_tiff data_file ball_radius compress_data\n"); printf("or\n"); printf("Usage: erode input_tiff eroded_tiff ball_radius compress_data\n"); return 0; } inputFile = argv[1]; erodedFile = argv[2]; if (argc == 6) { make_binFile = true; binFile = argv[3]; sscanf(argv[4],"%f",&ball_radius); sscanf(argv[5],"%d",&compress); } else { make_binFile = false; sscanf(argv[3],"%f",&ball_radius); sscanf(argv[4],"%d",&compress); } compressdata = (compress == 1); printf("Input image file: %s\n",inputFile); printf("Eroded image file: %s\n",erodedFile); if (make_binFile) printf("Hull binary data file: %s\n",binFile); printf("Ball radius: %f\n",ball_radius); printf("compressdata: %d\n",compressdata); typedef itk::ImageFileReader<ImageType> FileReaderType; FileReaderType::Pointer reader = FileReaderType::New(); reader->SetFileName(inputFile); try { reader->Update(); } catch (itk::ExceptionObject &e) { std::cout << e << std::endl; return 1; } inputImage = reader->GetOutput(); width = inputImage->GetLargestPossibleRegion().GetSize()[0]; height = inputImage->GetLargestPossibleRegion().GetSize()[1]; depth = inputImage->GetLargestPossibleRegion().GetSize()[2]; xysize = width*height; printf("Image dimensions: width, height, depth: %d %d %d\n",width,height,depth); /* typedef itk::BinaryThresholdImageFilter<ImageType, ImageType> ThresholdFilterType; ThresholdFilterType::Pointer Thresh = ThresholdFilterType::New(); if (threshold > 0) { printf("Thresholding\n"); Thresh->SetInput(inputImage); Thresh->SetUpperThreshold(threshold); Thresh->SetOutsideValue(1); Thresh->SetInsideValue(0); Thresh->ReleaseDataFlagOn(); //err = ImWriter(Thresh->GetOutput(),"thresh.tif"); //if (err != 0) { // printf("ImWriter error on threshold file\n"); // return 1; //} } printf("Closing\n"); typedef itk::BinaryCloseParaImageFilter<ImageType, ImageType> BinCloseFilterType; BinCloseFilterType::Pointer BinClose = BinCloseFilterType::New(); if (threshold > 0) { BinClose->SetInput(Thresh->GetOutput()); } else { BinClose->SetInput(inputImage); } BinClose->SetUseImageSpacing(true); BinClose->SetRadius(ballsize); BinClose->ReleaseDataFlagOn(); // err = ImWriter(BinClose->GetOutput(),"close1.tif"); //if (err != 0) { // printf("ImWriter error on close1 file\n"); // return 1; //} printf("Scaling\n"); typedef itk::ShiftScaleImageFilter<ImageType, ImageType> ScaleFilterType; ScaleFilterType::Pointer Scale = ScaleFilterType::New(); Scale->SetInput(BinClose->GetOutput()); Scale->SetScale(255.0); */ // const unsigned int radiusValue = peelsize/2; typedef itk::FlatStructuringElement< 3 > StructuringElementType; StructuringElementType::RadiusType radius; radius.Fill( ball_radius ); StructuringElementType structuringElement = StructuringElementType::Ball( radius ); typedef itk::BinaryErodeImageFilter< ImageType, ImageType, StructuringElementType > BinaryErodeImageFilterType; BinaryErodeImageFilterType::Pointer erodeFilter = BinaryErodeImageFilterType::New(); erodeFilter->SetInput( reader->GetOutput() ); erodeFilter->SetKernel( structuringElement ); printf("Writing eroded tiff\n"); err = ImWriter(erodeFilter->GetOutput(),erodedFile); if (err != 0) { printf("ImWriter error on eroded file\n"); return 1; } if (make_binFile) { printf("Writing close binary data file\n"); p = (unsigned char *)(erodeFilter->GetOutput()->GetBufferPointer()); err = BinImWriter(binFile,p,width,height,depth,compressdata); if (err != 0) { printf("BinImWriter error on binary file\n"); return 2; } } return 0; }