void mitk::ContourUtils::ItkCopyFilledContourToSlice( itk::Image<TPixel,VImageDimension>* originalSlice, const Image* filledContourSlice, int overwritevalue ) { typedef itk::Image<TPixel,VImageDimension> SliceType; typename SliceType::Pointer filledContourSliceITK; CastToItkImage( filledContourSlice, filledContourSliceITK ); // now the original slice and the ipSegmentation-painted slice are in the same format, and we can just copy all pixels that are non-zero typedef itk::ImageRegionIterator< SliceType > OutputIteratorType; typedef itk::ImageRegionConstIterator< SliceType > InputIteratorType; InputIteratorType inputIterator( filledContourSliceITK, filledContourSliceITK->GetLargestPossibleRegion() ); OutputIteratorType outputIterator( originalSlice, originalSlice->GetLargestPossibleRegion() ); outputIterator.GoToBegin(); inputIterator.GoToBegin(); while ( !outputIterator.IsAtEnd() ) { if ( inputIterator.Get() != 0 ) { outputIterator.Set( overwritevalue ); } ++outputIterator; ++inputIterator; } }
void TestFixture::CodeExecuter::Execute(const char* jamExecutable, behavior::Compatibility compatibility, const std::map<std::string, std::string>& code, const std::map<std::string, int>& codeAge, std::ostream& output, std::ostream& errorOutput) { fTemporaryDirectoryCreator.Delete(); fOutputPipe = NULL; // create a temporary test directory and change into it fTemporaryDirectoryCreator.Create(true); // write the code to the Jamfiles data::Time now = data::Time::Now(); for (std::map<std::string, std::string>::const_iterator it = code.begin(); it != code.end(); ++it) { std::string jamfilePath = MakePath( fTemporaryDirectoryCreator.Directory(), it->first.c_str()); CreateFile(jamfilePath.c_str(), it->second.c_str()); // set file time int age = codeAge.find(it->first) != codeAge.end() ? codeAge.at(it->first) : 0; // TODO: Platform specific! utimbuf times; times.actime = times.modtime = (time_t)now.Seconds() - age; if (utime(it->first.c_str(), ×) != 0) { HAM_TEST_THROW("Failed to set time on \"%s\": %s", it->first.c_str(), strerror(errno)) } } if (jamExecutable != NULL) { // run the executable fOutputPipe = popen(jamExecutable, "r"); if (fOutputPipe == NULL) { HAM_TEST_THROW("Failed to execute \"%s\": %s", jamExecutable, strerror(errno)) } // read input until done std::ostream_iterator<char> outputIterator(output); char buffer[4096]; for (;;) { size_t bytesRead = fread(buffer, 1, sizeof(buffer), fOutputPipe); if (bytesRead > 0) { outputIterator = std::copy(buffer, buffer + bytesRead, outputIterator); } if (bytesRead < sizeof(buffer)) break; } } else {
void mitk::OverwriteSliceImageFilter::ItkImageProcessing(const itk::Image<TPixel1, VImageDimension1> *inputImage, itk::Image<TPixel2, VImageDimension2> *outputImage) { typedef itk::Image<TPixel1, VImageDimension1> SliceImageType; typedef itk::Image<short signed int, VImageDimension1> DiffImageType; typedef itk::Image<TPixel2, VImageDimension2> VolumeImageType; typedef itk::ImageSliceIteratorWithIndex<VolumeImageType> OutputSliceIteratorType; typedef itk::ImageRegionConstIterator<SliceImageType> InputSliceIteratorType; typedef itk::ImageRegionIterator<DiffImageType> DiffSliceIteratorType; typename VolumeImageType::RegionType sliceInVolumeRegion; sliceInVolumeRegion = outputImage->GetLargestPossibleRegion(); sliceInVolumeRegion.SetSize(m_SliceDimension, 1); // just one slice sliceInVolumeRegion.SetIndex(m_SliceDimension, m_SliceIndex); // exactly this slice, please OutputSliceIteratorType outputIterator(outputImage, sliceInVolumeRegion); outputIterator.SetFirstDirection(m_Dimension0); outputIterator.SetSecondDirection(m_Dimension1); InputSliceIteratorType inputIterator(inputImage, inputImage->GetLargestPossibleRegion()); typename DiffImageType::Pointer diffImage; CastToItkImage(m_SliceDifferenceImage, diffImage); DiffSliceIteratorType diffIterator(diffImage, diffImage->GetLargestPossibleRegion()); // iterate over output slice (and over input slice simultaneously) outputIterator.GoToBegin(); inputIterator.GoToBegin(); diffIterator.GoToBegin(); while (!outputIterator.IsAtEnd()) { while (!outputIterator.IsAtEndOfSlice()) { while (!outputIterator.IsAtEndOfLine()) { diffIterator.Set(static_cast<short signed int>(inputIterator.Get() - outputIterator.Get())); // oh oh, not good for bigger values outputIterator.Set((TPixel2)inputIterator.Get()); ++outputIterator; ++inputIterator; ++diffIterator; } outputIterator.NextLine(); } outputIterator.NextSlice(); } }
void DeepCopyScalarImage(const TImage* const input, TImage* const output) { output->SetRegions(input->GetLargestPossibleRegion()); output->Allocate(); itk::ImageRegionConstIterator<TImage> inputIterator(input, input->GetLargestPossibleRegion()); itk::ImageRegionIterator<TImage> outputIterator(output, output->GetLargestPossibleRegion()); while(!inputIterator.IsAtEnd()) { outputIterator.Set(inputIterator.Get()); ++inputIterator; ++outputIterator; } }
void mitk::PixelManipulationTool::ITKPixelManipulation( itk::Image<TPixel, VImageDimension>* originalImage, Image* maskImage, Image* newImage, int newValue) { typedef itk::Image< TPixel, VImageDimension> itkImageType; typedef itk::Image< unsigned char, 3> itkMaskType; typename itkImageType::Pointer itkImage; typename itkMaskType::Pointer itkMask; CastToItkImage( newImage, itkImage); CastToItkImage( maskImage, itkMask); typedef itk::ImageRegionConstIterator< itkImageType > InputIteratorType; typedef itk::ImageRegionIterator< itkImageType > OutputIteratorType; typedef itk::ImageRegionConstIterator< itkMaskType > MaskIteratorType; MaskIteratorType maskIterator ( itkMask, itkMask->GetLargestPossibleRegion() ); InputIteratorType inputIterator( originalImage, originalImage->GetLargestPossibleRegion() ); OutputIteratorType outputIterator( itkImage, itkImage->GetLargestPossibleRegion() ); inputIterator.GoToBegin(); outputIterator.GoToBegin(); maskIterator.GoToBegin(); while (!outputIterator.IsAtEnd()) { if (maskIterator.Get()) { if (m_FixedValue) outputIterator.Set(newValue); else outputIterator.Set( inputIterator.Get()+ newValue); } else outputIterator.Set( inputIterator.Get()); ++inputIterator; ++outputIterator; ++maskIterator; } }
void mitk::DiffImageApplier::ItkImageProcessing3DDiff( itk::Image<TPixel1,VImageDimension1>* diffImage, itk::Image<TPixel2,VImageDimension2>* outputImage ) { typedef itk::Image<TPixel1, VImageDimension1> DiffImageType; typedef itk::Image<TPixel2, VImageDimension2> VolumeImageType; typedef itk::ImageRegionIterator< VolumeImageType > OutputSliceIteratorType; typedef itk::ImageRegionConstIterator< DiffImageType > DiffSliceIteratorType; OutputSliceIteratorType outputIterator( outputImage, outputImage->GetLargestPossibleRegion() ); DiffSliceIteratorType diffIterator( diffImage, diffImage->GetLargestPossibleRegion() ); // iterate over output slice (and over input slice simultaneously) outputIterator.GoToBegin(); diffIterator.GoToBegin(); while ( !outputIterator.IsAtEnd() ) { TPixel2 newValue = outputIterator.Get() + (TPixel2) ((double)diffIterator.Get() * m_Factor); outputIterator.Set( newValue ); ++outputIterator; ++diffIterator; } }
void mitk::DiffImageApplier::ItkImageProcessing2DDiff( itk::Image<TPixel1,VImageDimension1>* diffImage, itk::Image<TPixel2,VImageDimension2>* outputImage ) { typedef itk::Image<TPixel1, VImageDimension1> DiffImageType; typedef itk::Image<TPixel2, VImageDimension2> VolumeImageType; typedef itk::ImageSliceIteratorWithIndex< VolumeImageType > OutputSliceIteratorType; typedef itk::ImageRegionConstIterator< DiffImageType > DiffSliceIteratorType; typename VolumeImageType::RegionType sliceInVolumeRegion; sliceInVolumeRegion = outputImage->GetLargestPossibleRegion(); sliceInVolumeRegion.SetSize( m_SliceDimension, 1 ); // just one slice sliceInVolumeRegion.SetIndex( m_SliceDimension, m_SliceIndex ); // exactly this slice, please OutputSliceIteratorType outputIterator( outputImage, sliceInVolumeRegion ); outputIterator.SetFirstDirection(m_Dimension0); outputIterator.SetSecondDirection(m_Dimension1); DiffSliceIteratorType diffIterator( diffImage, diffImage->GetLargestPossibleRegion() ); // iterate over output slice (and over input slice simultaneously) outputIterator.GoToBegin(); diffIterator.GoToBegin(); while ( !outputIterator.IsAtEnd() ) { while ( !outputIterator.IsAtEndOfSlice() ) { while ( !outputIterator.IsAtEndOfLine() ) { TPixel2 newValue = outputIterator.Get() + (TPixel2) ((double)diffIterator.Get() * m_Factor); outputIterator.Set( newValue ); ++outputIterator; ++diffIterator; } outputIterator.NextLine(); } outputIterator.NextSlice(); } }
int main(int argc, char *argv[]) { if(argc < 3) { std::cerr << "Required: inputFilename outputFilename" << std::endl; return EXIT_FAILURE; } std::string inputFilename = argv[1]; std::string outputFilename = argv[2]; typedef itk::ImageFileReader<ImageType> ReaderType; ReaderType::Pointer reader = ReaderType::New(); reader->SetFileName(inputFilename.c_str()); reader->Update(); ImageType::Pointer output = ImageType::New(); output->SetNumberOfComponentsPerPixel(reader->GetOutput()->GetNumberOfComponentsPerPixel()); output->SetRegions(reader->GetOutput()->GetLargestPossibleRegion()); output->Allocate(); itk::ImageRegionIterator<ImageType> outputIterator(output, output->GetLargestPossibleRegion()); while(!outputIterator.IsAtEnd()) { outputIterator.Set(reader->GetOutput()->GetPixel(outputIterator.GetIndex())); ++outputIterator; } typedef itk::ImageFileWriter<ImageType> WriterType; WriterType::Pointer writer = WriterType::New(); writer->SetFileName(outputFilename); writer->SetInput(output); writer->Update(); return EXIT_SUCCESS; }
void mitk::BinaryThresholdULTool::ITKThresholding( itk::Image<TPixel, VImageDimension>* originalImage, Image* segmentation, unsigned int timeStep ) { ImageTimeSelector::Pointer timeSelector = ImageTimeSelector::New(); timeSelector->SetInput( segmentation ); timeSelector->SetTimeNr( timeStep ); timeSelector->UpdateLargestPossibleRegion(); Image::Pointer segmentation3D = timeSelector->GetOutput(); typedef itk::Image< Tool::DefaultSegmentationDataType, 3> SegmentationType; // this is sure for new segmentations SegmentationType::Pointer itkSegmentation; CastToItkImage( segmentation3D, itkSegmentation ); // iterate over original and segmentation typedef itk::ImageRegionConstIterator< itk::Image<TPixel, VImageDimension> > InputIteratorType; typedef itk::ImageRegionIterator< SegmentationType > SegmentationIteratorType; InputIteratorType inputIterator( originalImage, originalImage->GetLargestPossibleRegion() ); SegmentationIteratorType outputIterator( itkSegmentation, itkSegmentation->GetLargestPossibleRegion() ); inputIterator.GoToBegin(); outputIterator.GoToBegin(); while (!outputIterator.IsAtEnd()) { if ( (signed)inputIterator.Get() >= m_CurrentLowerThresholdValue && (signed)inputIterator.Get() <= m_CurrentUpperThresholdValue ) { outputIterator.Set( 1 ); } else { outputIterator.Set( 0 ); } ++inputIterator; ++outputIterator; } }
bool AppITKImage::LoadAndValidate(const std::string& imagePath) { typedef itk::ImageFileReader<AppImageITKType> ReaderType; ReaderType::Pointer reader = ReaderType::New(); reader->SetFileName(imagePath); reader->Update(); if(!m_image) { m_image = AppImageITKType::New(); } // Deep copy m_image->SetRegions(reader->GetOutput()->GetLargestPossibleRegion()); m_image->SetNumberOfComponentsPerPixel(LfnIc::Image::Pixel::NUM_CHANNELS); m_image->Allocate(); itk::ImageRegionConstIterator<AppImageITKType> inputIterator(reader->GetOutput(), reader->GetOutput()->GetLargestPossibleRegion()); itk::ImageRegionIterator<AppImageITKType> outputIterator(m_image, m_image->GetLargestPossibleRegion()); while(!inputIterator.IsAtEnd()) { outputIterator.Set(inputIterator.Get()); ++inputIterator; ++outputIterator; } #if USE_CHANNEL_WEIGHTING // Setup channel weights - Uniform weighting - set the weight of each channel so it has the perceived range of 255 // If a channel already has the range 255, the weight is set to 1. If a channel has a range smaller // than 255, its weight will be > 1. If a channel has a weight larger than 255, its weight will be set to < 1. // A weight should never be negative. There is no magic to scaling to 255, it is just that usually there will be some // RGB type channels so 255 should make several of the weights close to 1. std::cout << "Weights: "; for (int c = 0; c < LfnIc::Image::Pixel::NUM_CHANNELS; c++) { typedef itk::Image<LfnIc::Image::Pixel::ChannelType, 2> ScalarImageType; typedef itk::VectorIndexSelectionCastImageFilter<AppImageITKType, ScalarImageType> IndexSelectionType; IndexSelectionType::Pointer indexSelectionFilter = IndexSelectionType::New(); indexSelectionFilter->SetIndex(c); indexSelectionFilter->SetInput(m_image); indexSelectionFilter->Update(); typedef itk::MinimumMaximumImageCalculator <ScalarImageType> ImageCalculatorFilterType; ImageCalculatorFilterType::Pointer imageCalculatorFilter = ImageCalculatorFilterType::New(); imageCalculatorFilter->SetImage(indexSelectionFilter->GetOutput()); imageCalculatorFilter->Compute(); // If there is no variation in a channel (take an image with only red (255,0,0) and blue (0,0,255) pixels for example), // then computing the weight doesn't make sense (and causes a divide by zero). if( (imageCalculatorFilter->GetMaximum() - imageCalculatorFilter->GetMinimum()) <= 0) { m_channelWeights[c] = 1.0f; } else { m_channelWeights[c] = 255.0f / (imageCalculatorFilter->GetMaximum() - imageCalculatorFilter->GetMinimum()); } std::cout << m_channelWeights[c] << " "; } std::cout << std::endl; // Now that the weights have been calculated, apply them directly to the image data. LfnIc::Image::Pixel* pixelPtr = AppITKImage::GetData(); for (int i = 0, n = GetWidth() * GetHeight(); i < n; ++i, ++pixelPtr) { LfnIc::Image::Pixel& pixel = *pixelPtr; for (int c = 0; c < LfnIc::Image::Pixel::NUM_CHANNELS; ++c) { pixel.channel[c] *= m_channelWeights[c]; } } #endif // USE_CHANNEL_WEIGHTING return true; }
void FileIOManager::writeToFile(std::string filePath, std::vector<std::string>& fileData) { std::ofstream newFile(filePath); std::ostream_iterator<std::string> outputIterator(newFile, "\n"); std::copy(fileData.begin(), fileData.end(), outputIterator); }
void InternalWritePreviewOnWorkingImage(itk::Image<TPixel, VImageDimension> *targetSlice, const mitk::Image *sourceSlice, mitk::Image *originalImage, int overwritevalue) { typedef itk::Image<TPixel, VImageDimension> SliceType; typename SliceType::Pointer sourceSliceITK; CastToItkImage(sourceSlice, sourceSliceITK); // now the original slice and the ipSegmentation-painted slice are in the same format, and we can just copy all pixels // that are non-zero typedef itk::ImageRegionIterator<SliceType> OutputIteratorType; typedef itk::ImageRegionConstIterator<SliceType> InputIteratorType; InputIteratorType inputIterator(sourceSliceITK, sourceSliceITK->GetLargestPossibleRegion()); OutputIteratorType outputIterator(targetSlice, targetSlice->GetLargestPossibleRegion()); outputIterator.GoToBegin(); inputIterator.GoToBegin(); mitk::LabelSetImage *workingImage = dynamic_cast<mitk::LabelSetImage *>(originalImage); assert(workingImage); int activePixelValue = workingImage->GetActiveLabel()->GetValue(); if (activePixelValue == 0) // if exterior is the active label { while (!outputIterator.IsAtEnd()) { if (inputIterator.Get() != 0) { outputIterator.Set(overwritevalue); } ++outputIterator; ++inputIterator; } } else if (overwritevalue != 0) // if we are not erasing { while (!outputIterator.IsAtEnd()) { int targetValue = static_cast<int>(outputIterator.Get()); if (inputIterator.Get() != 0) { if (!workingImage->GetLabel(targetValue)->GetLocked()) { outputIterator.Set(overwritevalue); } } if (targetValue == overwritevalue) { outputIterator.Set(inputIterator.Get()); } ++outputIterator; ++inputIterator; } } else // if we are erasing { while (!outputIterator.IsAtEnd()) { const int targetValue = outputIterator.Get(); if (inputIterator.Get() != 0) { if (targetValue == activePixelValue) outputIterator.Set(overwritevalue); } ++outputIterator; ++inputIterator; } } }
int main() { typedef double scalar_type; typedef numeric::vector <scalar_type> vector_type; typedef numeric::matrix <scalar_type> matrix_type; typedef scalar_type (*function_type )( vector_type const & ); typedef vector_type (*function_grad_type )( vector_type const & ); typedef matrix_type (*function_inv_hessian_type)( vector_type const & ); typedef scalar_type (*scalar_norm_type )( scalar_type ); typedef scalar_type (*vector_norm_type )( numeric::ublas::vector_expression<vector_type> const & ); typedef numeric::barrier_method::PointDebugInfo<scalar_type> points_debug_info_type; function_type const f = &function::function<vector_type>; function_grad_type const df = &function::functionGrad<vector_type>; scalar_type const preferredPrecision = function::preferredPrecision; scalar_norm_type const sNorm = &xmath::abs<scalar_type>; vector_norm_type const vNorm = &numeric::ublas::norm_2<vector_type>; vector_type startPoint(2); startPoint(0) = function::startX; startPoint(1) = function::startY; scalar_type const startMu = function::startMu; scalar_type const beta = function::beta; std::vector<function_type> constraints; std::vector<function_grad_type> constraintsGrads; constraints. push_back(&function::limitFunc1<scalar_type>); constraints. push_back(&function::limitFunc2<scalar_type>); constraintsGrads.push_back(&function::limitFuncGrad1<scalar_type>); constraintsGrads.push_back(&function::limitFuncGrad2<scalar_type>); // TODO: Separate this constants. scalar_type const gradientDescentPrecision = 1e-8; scalar_type const gradientDescentStep = 1.0; { // // Calculating Lipschitz constant. // vector_type x(2); x(0) = function::loLipschitzX; x(1) = function::loLipschitzY; vector_type y(2); y(0) = function::hiLipschitzX; y(1) = function::hiLipschitzY; vector_type step(2); step(0) = function::lipschitzStepX; step(1) = function::lipschitzStepY; scalar_type const R = numeric::lipschitz_constant<function_type, scalar_type, scalar_norm_type, vector_type, vector_norm_type>( f, sNorm, x, y, vNorm, step); { // Saving Lipschitz constant. char const *dataFileName = "../output/lipschitz_const.tex"; boost::scoped_ptr<std::ofstream> ofs(new std::ofstream(dataFileName)); if (ofs->fail()) { std::cerr << "Failed to open data file '" << dataFileName << "'.\n"; return 1; } *ofs << boost::format("%1$15.8f") % R; } } { // // Barrier method. // std::vector<points_debug_info_type> points; vector_type const xMin = numeric::barrier_method::find_min <function_type, function_grad_type, scalar_type>( f, df, constraints.begin(), constraints.end(), constraintsGrads.begin(), constraintsGrads.end(), startPoint, startMu, beta, preferredPrecision, gradientDescentPrecision, gradientDescentStep, std::back_inserter(points)); { // Saving passed spots. char const *dataFileName = "../output/ne_points.dat"; // TODO: Correct file name. boost::scoped_ptr<std::ofstream> ofs(new std::ofstream(dataFileName)); if (ofs->fail()) { std::cerr << "Failed to open data file '" << dataFileName << "'.\n"; return 1; } for (size_t i = 0; i < points.size(); ++i) { points_debug_info_type const &pdi = points[i]; numeric::output_vector_coordinates<std::ofstream, vector_type>(*ofs, pdi.x, " ", "\n", "%1$15.8f"); } } { // Saving detail report. char const *dataFileName = "../output/bm_points.tex"; boost::scoped_ptr<std::ofstream> ofs(new std::ofstream(dataFileName)); if (ofs->fail()) { std::cerr << "Failed to open data file '" << dataFileName << "'.\n"; return 1; } for (size_t i = 0; i < points.size(); ++i) { points_debug_info_type const &pdi = points[i]; *ofs << i + 1 << " & "; // TODO: There is space between brackets and numbers! *ofs << "( "; numeric::output_vector_coordinates(*ofs, pdi.x, ", ", "", "%1$15.8f"); *ofs << " ) & "; *ofs << boost::format("%1$15.8f") % (pdi.fx) << " & "; *ofs << boost::format("%1$15.8f") % (pdi.mu) << " & "; *ofs << boost::format("%1$15.8f") % (pdi.Bx) << " & "; *ofs << boost::format("%1$15.8f") % (pdi.mu * pdi.Bx) << " & "; *ofs << boost::format("%1$15.8f") % (pdi.fx + pdi.mu * pdi.Bx) << " \\\\ " << std::endl; } } if (points.begin() != points.end()) { // Saving passed spots function values (for contours). char const *dataFileName = "../output/ne_contours.gp"; // TODO: Correct file name. boost::scoped_ptr<std::ofstream> ofs(new std::ofstream(dataFileName)); if (ofs->fail()) { std::cerr << "Failed to open data file '" << dataFileName << "'.\n"; return 1; } *ofs << "set cntrparam levels discrete "; for (size_t i = 1; i < points.size(); ++i) *ofs << points[i].fx << ","; *ofs << points[0].fx; *ofs << std::endl; } } { // Generating report data. char const *dataFileName = "../output/ne_points.tex"; boost::scoped_ptr<std::ofstream> ofs(new std::ofstream(dataFileName)); if (ofs->fail()) { std::cerr << "Failed to open data file '" << dataFileName << "'.\n"; return 1; } boost::optional<scalar_type> prevFMinVal; for (size_t p = 0; p < numeric::array_size(function::precisions); ++p) { scalar_type const precision = function::precisions[p]; size_t nPoints(0); numeric::CountingOutputIterator outputIterator(nPoints); vector_type const xMin = numeric::barrier_method::find_min <function_type, function_grad_type, scalar_type>( f, df, constraints.begin(), constraints.end(), constraintsGrads.begin(), constraintsGrads.end(), startPoint, startMu, beta, precision, gradientDescentPrecision, gradientDescentStep, outputIterator); // Outputting: precision, number of iterations, xMin, f(xMin), f(xMin) - f(xPrevMin), grad f, g1(xMin), g2(xMin). *ofs << boost::format("%1$1.0e") % precision << " & " << nPoints << " & ("; numeric::output_vector_coordinates(*ofs, xMin, ", ", ""); *ofs << ") & "; scalar_type const curFMinVal = f(xMin); *ofs << boost::format("%1$15.8f") % curFMinVal << " & "; if (prevFMinVal) *ofs << boost::format("%1e") % (curFMinVal - *prevFMinVal); prevFMinVal = curFMinVal; vector_type const curGrad = df(xMin); *ofs << " & ("; numeric::output_vector_coordinates(*ofs, curGrad, ", ", "", "%1e"); *ofs << ")"; *ofs << " & " << constraints[0](xMin); *ofs << " & " << constraints[1](xMin); *ofs << " \\\\\n"; } } }