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!"); } } }
void TestComputeMaskedImage1DHistogram() { // // Single channel // { // typedef itk::Image<unsigned char, 2> ImageType; // ImageType::Pointer image = ImageType::New(); // ImageType::IndexType corner = {{0,0}}; // ImageType::SizeType size = {{100,100}}; // ImageType::RegionType region(corner, size); // image->SetRegions(region); // image->Allocate(); // itk::ImageRegionIterator<ImageType> imageIterator(image,region); // while(!imageIterator.IsAtEnd()) // { // if(imageIterator.GetIndex()[0] < 70) // { // imageIterator.Set(255); // } // else // { // imageIterator.Set(0); // } // ++imageIterator; // } // ImageType::PixelType rangeMin = 0; // ImageType::PixelType rangeMax = 255; // unsigned int numberOfBins = 10; // typedef int BinValueType; // typedef Histogram<BinValueType>::HistogramType HistogramType; // HistogramType histogram = Histogram<BinValueType>::ComputeImageHistogram1D(image.GetPointer(), // image->GetLargestPossibleRegion(), // numberOfBins, rangeMin, rangeMax); // Histogram<BinValueType>::OutputHistogram(histogram); // std::cout << std::endl; // } // Multi channel VectorImage { typedef itk::VectorImage<unsigned char, 2> ImageType; ImageType::Pointer image = ImageType::New(); ImageType::IndexType corner = {{0,0}}; ImageType::SizeType size = {{100,100}}; ImageType::RegionType region(corner, size); image->SetRegions(region); image->SetNumberOfComponentsPerPixel(3); image->Allocate(); Mask::Pointer mask = Mask::New(); mask->SetRegions(region); mask->Allocate(); itk::ImageRegionIterator<ImageType> imageIterator(image,region); while(!imageIterator.IsAtEnd()) { ImageType::PixelType pixel(image->GetNumberOfComponentsPerPixel()); if(imageIterator.GetIndex()[0] < 70) { for(unsigned int i = 0; i < pixel.GetSize(); ++i) { pixel[i] = 255; } } else { for(unsigned int i = 0; i < pixel.GetSize(); ++i) { pixel[i] = 0; } } imageIterator.Set(pixel); ++imageIterator; } // TypeTraits<ImageType::PixelType>::ComponentType rangeMin = 0; // TypeTraits<ImageType::PixelType>::ComponentType rangeMax = 255; ImageType::PixelType rangeMins; rangeMins.SetSize(image->GetNumberOfComponentsPerPixel()); rangeMins.Fill(0); ImageType::PixelType rangeMaxs; rangeMaxs.SetSize(image->GetNumberOfComponentsPerPixel()); rangeMaxs.Fill(255); unsigned int numberOfBinsPerComponent = 10; typedef int BinValueType; itk::ImageRegion<2> imageRegion = image->GetLargestPossibleRegion(); itk::ImageRegion<2> maskRegion = image->GetLargestPossibleRegion(); typedef MaskedHistogramGenerator<BinValueType> HistogramGeneratorType; typedef HistogramGeneratorType::HistogramType HistogramType; bool allowOutside = false; HistogramType histogram = HistogramGeneratorType::ComputeMaskedImage1DHistogram(image.GetPointer(), imageRegion, mask.GetPointer(), maskRegion, numberOfBinsPerComponent, rangeMins, rangeMaxs, allowOutside, HoleMaskPixelTypeEnum::VALID); histogram.Print(); std::cout << std::endl; } // // Multi channel Image<CovariantVector> // { // typedef itk::Image<itk::CovariantVector<unsigned char, 3>, 2> ImageType; // ImageType::Pointer image = ImageType::New(); // ImageType::IndexType corner = {{0,0}}; // ImageType::SizeType size = {{100,100}}; // ImageType::RegionType region(corner, size); // image->SetRegions(region); // image->Allocate(); // itk::ImageRegionIterator<ImageType> imageIterator(image,region); // while(!imageIterator.IsAtEnd()) // { // ImageType::PixelType pixel(image->GetNumberOfComponentsPerPixel()); // if(imageIterator.GetIndex()[0] < 70) // { // for(unsigned int i = 0; i < pixel.GetNumberOfComponents(); ++i) // { // pixel[i] = 255; // } // } // else // { // for(unsigned int i = 0; i < pixel.GetNumberOfComponents(); ++i) // { // pixel[i] = 0; // } // } // imageIterator.Set(pixel); // ++imageIterator; // } // TypeTraits<ImageType::PixelType>::ComponentType rangeMin = 0; // TypeTraits<ImageType::PixelType>::ComponentType rangeMax = 255; // unsigned int numberOfBinsPerComponent = 10; // typedef int BinValueType; // Histogram<BinValueType>::HistogramType histogram = Histogram<BinValueType>::ComputeImageHistogram1D(image.GetPointer(), // image->GetLargestPossibleRegion(), // numberOfBinsPerComponent, rangeMin, rangeMax); // Histogram<BinValueType>::OutputHistogram(histogram); // std::cout << std::endl; // } }
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!"); } } }
void LidarSegmentationWidget::on_btnCut_clicked() { // Normalize the image ImageType::Pointer normalizedImage = ImageType::New(); normalizedImage->SetNumberOfComponentsPerPixel(this->Image->GetNumberOfComponentsPerPixel()); normalizedImage->SetRegions(this->Image->GetLargestPossibleRegion()); normalizedImage->Allocate(); std::cout << "Normalizing image..." << std::endl; //ITKHelpers::DeepCopy(this->Image.GetPointer(), normalizedImage.GetPointer()); //ITKHelpers::NormalizeVectorImage(normalizedImage.GetPointer()); ITKHelpers::NormalizeImageChannels(this->Image.GetPointer(), normalizedImage.GetPointer()); std::cout << "Normalized image has " << normalizedImage->GetNumberOfComponentsPerPixel() << " channels." << std::endl; //this->GraphCut.SetImage(this->Image); this->GraphCut.SetImage(normalizedImage.GetPointer()); this->GraphCut.Debug = this->chkDebug->isChecked(); //this->GraphCut.SecondStep = this->chkSecondStep->isChecked(); this->GraphCut.IncludeDepthInHistogram = this->chkDepthHistogram->isChecked(); this->GraphCut.IncludeColorInHistogram = this->chkColorHistogram->isChecked(); this->GraphCut.BackgroundThreshold = this->txtBackgroundThreshold->text().toDouble(); if(this->GraphCut.DifferenceFunction) { delete this->GraphCut.DifferenceFunction; } // Setup the Difference object if(this->chkDepthDifference->isChecked() && !this->chkColorDifference->isChecked()) { std::cout << "Using depth-only N-weights." << std::endl; this->GraphCut.DifferenceFunction = new DepthDifference; } else if(!this->chkDepthDifference->isChecked() && this->chkColorDifference->isChecked()) { std::cout << "Using color-only N-weights." << std::endl; this->GraphCut.DifferenceFunction = new ColorDifference; } else if(this->chkDepthDifference->isChecked() && this->chkColorDifference->isChecked()) { std::cout << "Using depth+color N-weights." << std::endl; std::vector<float> weights(4,1.0f); weights[0] = spinRWeight->value(); weights[1] = spinGWeight->value(); weights[2] = spinBWeight->value(); weights[3] = spinDWeight->value(); std::cout << "Weights: " << weights[0] << " " << weights[1] << " " << weights[2] << " " << weights[3] << std::endl; this->GraphCut.DifferenceFunction = new WeightedDifference(weights); } else { std::stringstream ss; ss << "Something is wrong - you must select depth, color, or both." << std::endl; throw std::runtime_error(ss.str()); } // Get the number of bins from the slider this->GraphCut.SetNumberOfHistogramBins(this->sldHistogramBins->value()); if(this->sldLambda->value() == 0) { QMessageBox msgBox; msgBox.setText("You must select lambda > 0!"); msgBox.exec(); return; } // Setup the graph cut from the GUI and the scribble selection this->GraphCut.SetLambda(ComputeLambda()); this->GraphCut.SetSources(this->Sources); this->GraphCut.SetSinks(this->Sinks); //this->GraphCut.SetSources(this->LeftInteractorStyle->GetForegroundSelection()); //this->GraphCut.SetSinks(this->LeftInteractorStyle->GetBackgroundSelection()); // Setup and start the actual cut computation in a different thread QFuture<void> future = QtConcurrent::run(this->GraphCut, &ImageGraphCut::PerformSegmentation); this->FutureWatcher.setFuture(future); this->ProgressDialog->exec(); if(!this->RightRenderer->HasViewProp(this->ResultImageSlice)) { std::cout << "Added ResultImageSlice view prop." << std::endl; //this->RightRenderer->AddViewProp(this->ResultImageSlice); this->ResultImageSlice->GetProperty()->SetLayerNumber(0); // 0 = bottom of the stack. this->RightSourceSinkImageSlice->GetProperty()->SetLayerNumber(1); // The source/sink image should be displayed on top of the result image. } }
void LidarSegmentationWidget::on_btnSegmentLiDAR_clicked() { // Normalize the image ImageType::Pointer normalizedImage = ImageType::New(); normalizedImage->SetNumberOfComponentsPerPixel(this->Image->GetNumberOfComponentsPerPixel()); normalizedImage->SetRegions(this->Image->GetLargestPossibleRegion()); normalizedImage->Allocate(); std::cout << "Normalizing image..." << std::endl; //ITKHelpers::DeepCopy(this->Image.GetPointer(), normalizedImage.GetPointer()); ITKHelpers::NormalizeImageChannels(this->Image.GetPointer(), normalizedImage.GetPointer()); std::cout << "Normalized image has " << normalizedImage->GetNumberOfComponentsPerPixel() << " channels." << std::endl; //this->GraphCut.SetImage(this->Image); this->GraphCut.SetImage(normalizedImage.GetPointer()); this->GraphCut.IncludeDepthInHistogram = true; this->GraphCut.IncludeColorInHistogram = true; this->GraphCut.BackgroundThreshold = 0.4; if(this->GraphCut.DifferenceFunction) { delete this->GraphCut.DifferenceFunction; } std::cout << "Using depth-only N-weights." << std::endl; this->GraphCut.DifferenceFunction = new DepthDifference; // Get the number of bins from the slider this->GraphCut.SetNumberOfHistogramBins(this->sldHistogramBins->value()); // Setup the graph cut from the GUI and the scribble selection //this->GraphCut.SetLambda(.0025); this->GraphCut.SetLambda(ComputeLambda()); this->GraphCut.SetSources(this->Sources); this->GraphCut.SetSinks(this->Sinks); // Setup and start the actual cut computation in a different thread QFuture<void> future = QtConcurrent::run(this->GraphCut, &ImageGraphCut::PerformSegmentation); this->FutureWatcher.setFuture(future); this->ProgressDialog->exec(); // Step 2 - color + depth on_btnReseedForeground_clicked(); on_btnGenerateNeighborSinks_clicked(); this->GraphCut.SetSources(this->Sources); this->GraphCut.SetSinks(this->Sinks); std::cout << "Using depth+color N-weights." << std::endl; std::vector<float> weights(4,1.0f); // weights[0] = 1.0f; // weights[1] = 1.0f; // weights[2] = 1.0f; // weights[3] = 1.0f; this->GraphCut.DifferenceFunction = new WeightedDifference(weights); QFuture<void> futureStep2 = QtConcurrent::run(this->GraphCut, &ImageGraphCut::PerformSegmentation); this->FutureWatcher.setFuture(futureStep2); this->ProgressDialog->exec(); if(!this->RightRenderer->HasViewProp(this->ResultImageSlice)) { std::cout << "Added ResultImageSlice view prop." << std::endl; this->ResultImageSlice->VisibilityOn(); this->ResultImageSlice->GetProperty()->SetLayerNumber(0); // 0 = Bottom of the stack this->RightSourceSinkImageSlice->GetProperty()->SetLayerNumber(1); // The source/sink image should be displayed on top of the result image. } }