static mitk::Image::Pointer GenerateMaskImage(unsigned int dimX, unsigned int dimY, unsigned int dimZ, float spacingX = 1, float spacingY = 1, float spacingZ = 1) { typedef itk::Image< TPixelType, 3 > ImageType; typename ImageType::RegionType imageRegion; imageRegion.SetSize(0, dimX); imageRegion.SetSize(1, dimY); imageRegion.SetSize(2, dimZ); typename ImageType::SpacingType spacing; spacing[0] = spacingX; spacing[1] = spacingY; spacing[2] = spacingZ; mitk::Point3D origin; origin.Fill(0.0); itk::Matrix<double, 3, 3> directionMatrix; directionMatrix.SetIdentity(); typename ImageType::Pointer image = ImageType::New(); image->SetSpacing( spacing ); image->SetOrigin( origin ); image->SetDirection( directionMatrix ); image->SetLargestPossibleRegion( imageRegion ); image->SetBufferedRegion( imageRegion ); image->SetRequestedRegion( imageRegion ); image->Allocate(); image->FillBuffer(1); mitk::Image::Pointer mitkImage = mitk::Image::New(); mitkImage->InitializeByItk( image.GetPointer() ); mitkImage->SetVolume( image->GetBufferPointer() ); return mitkImage; }
void StrokeMask:: Write(const std::string& filename, const TPixel& strokeValue) { typedef itk::Image<TPixel, 2> ImageType; typename ImageType::Pointer image = ImageType::New(); image->SetRegions(this->GetLargestPossibleRegion()); image->Allocate(); itk::ImageRegionConstIteratorWithIndex<StrokeMask> maskIterator(this, this->GetLargestPossibleRegion()); while(!maskIterator.IsAtEnd()) { if(maskIterator.Get() == StrokeMaskPixelTypeEnum::STROKE) { image->SetPixel(maskIterator.GetIndex(), strokeValue); } else { image->SetPixel(maskIterator.GetIndex(), itk::NumericTraits<TPixel>::Zero); } ++maskIterator; } typedef itk::ImageFileWriter<ImageType> WriterType; typename WriterType::Pointer writer = WriterType::New(); writer->SetFileName(filename); writer->SetInput(image); writer->Update(); }
typename PatchExtractor<PValue>::ImageType::Pointer PatchExtractor<PValue>::ExtractPatch() { ContIndexType startIndex; for(unsigned int i = 0; i < 2; i++) { startIndex[i] = m_Center[i] - (m_Size[i] / 2.0); } startIndex[2] = 0; PointType newOrigin; m_Image->TransformContinuousIndexToPhysicalPoint(startIndex, newOrigin); typename ImageType::Pointer patch = ImageType::New(); patch->SetDirection(m_Image->GetDirection()); patch->SetOrigin(newOrigin); patch->SetSpacing(m_Image->GetSpacing()); typename ImageType::RegionType region; m_Size[2] = 1; region.SetSize(m_Size); patch->SetRegions(region); patch->Allocate(); typedef itk::NearestNeighborInterpolateImageFunction<ImageType, double> InterpolatorType; typename InterpolatorType::Pointer interpolator = InterpolatorType::New(); interpolator->SetInputImage(m_Image); itk::ImageRegionIterator<ImageType> imageIt(patch, region); while(!imageIt.IsAtEnd()) { typename ImageType::IndexType index = imageIt.GetIndex(); PointType point; patch->TransformIndexToPhysicalPoint(index, point); if(interpolator->IsInsideBuffer(point)) imageIt.Set(interpolator->Evaluate(point)); else imageIt.Set(0); ++imageIt; } return patch; }
static mitk::Image::Pointer GenerateGradientWithDimXImage(unsigned int dimX, unsigned int dimY, unsigned int dimZ, float spacingX = 1, float spacingY = 1, float spacingZ = 1) { typedef itk::Image< TPixelType, 3 > ImageType; typename ImageType::RegionType imageRegion; imageRegion.SetSize(0, dimX); imageRegion.SetSize(1, dimY); imageRegion.SetSize(2, dimZ); typename ImageType::SpacingType spacing; spacing[0] = spacingX; spacing[1] = spacingY; spacing[2] = spacingZ; mitk::Point3D origin; origin.Fill(0.0); itk::Matrix<double, 3, 3> directionMatrix; directionMatrix.SetIdentity(); typename ImageType::Pointer image = ImageType::New(); image->SetSpacing( spacing ); image->SetOrigin( origin ); image->SetDirection( directionMatrix ); image->SetLargestPossibleRegion( imageRegion ); image->SetBufferedRegion( imageRegion ); image->SetRequestedRegion( imageRegion ); image->Allocate(); image->FillBuffer(0.0); typedef itk::ImageRegionIterator<ImageType> IteratorOutputType; IteratorOutputType it(image, imageRegion); it.GoToBegin(); TPixelType val = 0; while(!it.IsAtEnd()) { it.Set(val % dimX); val++; ++it; } mitk::Image::Pointer mitkImage = mitk::Image::New(); mitkImage->InitializeByItk( image.GetPointer() ); mitkImage->SetVolume( image->GetBufferPointer() ); return mitkImage; }
Image::Pointer mitk::OpenCVToMitkImageFilter::ConvertIplToMitkImage( const IplImage * input ) { typedef itk::Image< TPixel, VImageDimension > ImageType; typename ImageType::Pointer output = ImageType::New(); typename ImageType::RegionType region; typename ImageType::RegionType::SizeType size; typename ImageType::RegionType::IndexType index; typename ImageType::SpacingType spacing; size.Fill( 1 ); size[0] = input->width; size[1] = input->height; index.Fill(0); spacing.Fill(1); region.SetSize(size); region.SetIndex(index); output->SetRegions(region); output->SetSpacing(spacing); output->Allocate(); // CAVE: The itk openCV bridge seem to NOT correctly copy the image data, hence the call to // itk::OpenCVImageBridge::IplImageToITKImage<ImageType>() is simply used to initialize the itk image // and in the next step the image data are copied by hand! if(input->nChannels == 3) // these are RGB images and need to be set to BGR before conversion! { output = itk::OpenCVImageBridge::IplImageToITKImage<ImageType>(input); } else { memcpy((void*) output->GetBufferPointer(), (void*) input->imageDataOrigin, input->width*input->height*sizeof(TPixel)); } Image::Pointer mitkImage = Image::New(); mitkImage = GrabItkImageMemory(output); return mitkImage; }
void ForegroundBackgroundSegmentMask:: Write(const std::string& filename, const ForegroundPixelValueWrapper<TPixel>& foregroundValue, const BackgroundPixelValueWrapper<TPixel>& backgroundValue) { typedef itk::Image<TPixel, 2> ImageType; typename ImageType::Pointer image = ImageType::New(); image->SetRegions(this->GetLargestPossibleRegion()); image->Allocate(); itk::ImageRegionConstIteratorWithIndex<ForegroundBackgroundSegmentMask> maskIterator(this, this->GetLargestPossibleRegion()); while(!maskIterator.IsAtEnd()) { if(maskIterator.Get() == ForegroundBackgroundSegmentMaskPixelTypeEnum::FOREGROUND) { image->SetPixel(maskIterator.GetIndex(), foregroundValue.Value); } else if(maskIterator.Get() == ForegroundBackgroundSegmentMaskPixelTypeEnum::BACKGROUND) { image->SetPixel(maskIterator.GetIndex(), backgroundValue.Value); } ++maskIterator; } typedef itk::ImageFileWriter<ImageType> WriterType; typename WriterType::Pointer writer = WriterType::New(); writer->SetFileName(filename); writer->SetInput(image); writer->Update(); }
void invcondemonsforces(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { typedef float PixelType; typedef itk::Image< PixelType, Dimension > ImageType; typedef float VectorComponentType; typedef itk::Vector<VectorComponentType, Dimension> VectorPixelType; typedef itk::Image<VectorPixelType, Dimension> DeformationFieldType; typedef itk::ESMInvConDemonsRegistrationFunction <ImageType,ImageType,DeformationFieldType> DemonsRegistrationFunctionType; //boost::timer timer; // Allocate images and deformation field typename ImageType::Pointer fixedimage = ImageType::New(); typename ImageType::Pointer movingimage = ImageType::New(); typename ImageType::Pointer fw_weightimage = ImageType::New(); typename DeformationFieldType::Pointer field = DeformationFieldType::New(); typename ImageType::Pointer jacobianimage = ImageType::New(); typename DeformationFieldType::Pointer inv_field = DeformationFieldType::New(); typename DeformationFieldType::Pointer update = DeformationFieldType::New(); typename DeformationFieldType::SpacingType spacing; spacing.Fill( 1.0 ); typename DeformationFieldType::PointType origin; origin.Fill( 0.0 ); typename DeformationFieldType::RegionType region; typename DeformationFieldType::SizeType size; typename DeformationFieldType::IndexType start; unsigned int numPix(1u); const MatlabPixelType * fixinptr = static_cast<const MatlabPixelType *>(mxGetData(prhs[0])); const MatlabPixelType * movinptr = static_cast<const MatlabPixelType *>(mxGetData(prhs[1])); const MatlabPixelType * fieldinptrs[Dimension]; const MatlabPixelType * inv_fieldinptrs[Dimension]; mwSize matlabdims[Dimension]; for (unsigned int d=0; d<Dimension; d++) { matlabdims[d]= mxGetDimensions(prhs[0])[d]; size[d] = matlabdims[d]; start[d] = 0; numPix *= size[d]; fieldinptrs[d] = static_cast<const MatlabPixelType *>(mxGetData(prhs[2+d])); inv_fieldinptrs[d] = static_cast<const MatlabPixelType *>(mxGetData(prhs[2+Dimension+d])); } const MatlabPixelType * jacobianptr = static_cast<const MatlabPixelType *> (mxGetData(prhs[2*Dimension + 2])); const MatlabPixelType * fw_weightptr = static_cast<const MatlabPixelType *> (mxGetData(prhs[2*Dimension + 3])); const double RegWeight = static_cast<double>( mxGetPr(prhs[2*Dimension+4])[0] ); const unsigned int UseJacFlag = 1; //std::cout << "RegWeight : " << RegWeight << std::endl; region.SetSize( size ); region.SetIndex( start ); fixedimage->SetOrigin( origin ); fixedimage->SetSpacing( spacing ); fixedimage->SetRegions( region ); fixedimage->Allocate(); movingimage->SetOrigin( origin ); movingimage->SetSpacing( spacing ); movingimage->SetRegions( region ); movingimage->Allocate(); fw_weightimage->SetOrigin( origin ); fw_weightimage->SetSpacing( spacing ); fw_weightimage->SetRegions( region ); fw_weightimage->Allocate(); field->SetOrigin( origin ); field->SetSpacing( spacing ); field->SetRegions( region ); field->Allocate(); inv_field->SetOrigin( origin ); inv_field->SetSpacing( spacing ); inv_field->SetRegions( region ); inv_field->Allocate(); update->SetOrigin( origin ); update->SetSpacing( spacing ); update->SetRegions( region ); update->Allocate(); if (UseJacFlag > 0) { jacobianimage->SetOrigin( origin ); jacobianimage->SetSpacing( spacing ); jacobianimage->SetRegions( region ); jacobianimage->Allocate(); } //mexPrintf("done Allocate(); %f sec\n", timer.elapsed()); //timer.restart(); PixelType * fixptr = fixedimage->GetBufferPointer(); const PixelType * const fixbuff_end = fixptr + numPix; PixelType * movptr = movingimage->GetBufferPointer(); PixelType * fwweightptr = NULL; PixelType * jacptr = NULL; if (UseJacFlag > 0) { jacptr = jacobianimage->GetBufferPointer(); } fwweightptr = fw_weightimage->GetBufferPointer(); VectorPixelType * fieldptr = field->GetBufferPointer(); VectorPixelType * inv_fieldptr = inv_field->GetBufferPointer(); while ( fixptr != fixbuff_end ) { *fixptr++ = *fixinptr++; *movptr++ = *movinptr++; *fwweightptr++ = *fw_weightptr++; for (unsigned int d=0; d<Dimension; d++) { (*fieldptr)[d] = *(fieldinptrs[d])++; } if (UseJacFlag > 0) { *jacptr++ = *jacobianptr++; } ++fieldptr; for (unsigned int d=0; d<Dimension; d++) { (*inv_fieldptr)[d] = *(inv_fieldinptrs[d])++; } ++inv_fieldptr; } // Create demons function typename DemonsRegistrationFunctionType::Pointer drfp = DemonsRegistrationFunctionType::New(); //mexPrintf("step size: %f\n",mxGetPr(prhs[2*Dimension+2])[0]); //drfp->SetMaximumUpdateStepLength( mxGetPr(prhs[2*Dimension+2])[0] ); typename DemonsRegistrationFunctionType::GradientType gtype = DemonsRegistrationFunctionType::Symmetric; drfp->SetUseGradientType( gtype ); drfp->SetDeformationField( field ); drfp->SetInvDeformationField( inv_field); drfp->SetFixedImage( fixedimage ); drfp->SetMovingImage( movingimage ); drfp->SetRegWeight(RegWeight); drfp->SetUseFwWeight(true); drfp->SetFwWeightImage(fw_weightimage); if (UseJacFlag > 0) { drfp->SetUseJacobian(true); drfp->SetJacobianDetImage(jacobianimage); } else { drfp->SetUseJacobian(false); } drfp->InitializeIteration(); //mexPrintf("done demons function init %f sec\n", timer.elapsed()); //timer.restart(); const itk::Size<Dimension> radius = drfp->GetRadius(); // Break the input into a series of regions. The first region is free // of boundary conditions, the rest with boundary conditions. We operate // on the output region because input has been copied to output. typedef itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator <DeformationFieldType> FaceCalculatorType; typedef typename FaceCalculatorType::FaceListType FaceListType; typedef typename DemonsRegistrationFunctionType::NeighborhoodType NeighborhoodIteratorType; typedef itk::ImageRegionIterator<DeformationFieldType> UpdateIteratorType; FaceCalculatorType faceCalculator; FaceListType faceList = faceCalculator(field, region, radius); typename FaceListType::iterator fIt = faceList.begin(); // Ask the function object for a pointer to a data structure it // will use to manage any global values it needs. We'll pass this // back to the function object at each calculation and then // again so that the function object can use it to determine a // time step for this iteration. void * globalData = drfp->GetGlobalDataPointer(); // Process the non-boundary region. NeighborhoodIteratorType nD(radius, field, *fIt); UpdateIteratorType nU(update, *fIt); nD.GoToBegin(); while( !nD.IsAtEnd() ) { nU.Value() = drfp->ComputeUpdate(nD, globalData); ++nD; ++nU; } // Process each of the boundary faces. NeighborhoodIteratorType bD; UpdateIteratorType bU; for (++fIt; fIt != faceList.end(); ++fIt) { bD = NeighborhoodIteratorType(radius, field, *fIt); bU = UpdateIteratorType(update, *fIt); bD.GoToBegin(); bU.GoToBegin(); while ( !bD.IsAtEnd() ) { bU.Value() = drfp->ComputeUpdate(bD, globalData); ++bD; ++bU; } } // Ask the finite difference function to compute the time step for // this iteration. We give it the global data pointer to use, then // ask it to free the global data memory. //timeStep = df->ComputeGlobalTimeStep(globalData); drfp->ReleaseGlobalDataPointer(globalData); //mexPrintf("done actual computations %f sec\n", timer.elapsed()); //timer.restart(); // Allocate outputs const mxClassID classID = mxGetClassID(prhs[0]); MatlabPixelType * outptrs[Dimension]; for (unsigned int d=0; d<Dimension; d++) { plhs[d] = mxCreateNumericArray( Dimension, matlabdims, classID, mxREAL); outptrs[d] = static_cast<MatlabPixelType *>(mxGetData(plhs[d])); } //mexPrintf("done allocate outputs %f sec\n", timer.elapsed()); //timer.restart(); // put result into outputs const VectorPixelType * upptr = update->GetBufferPointer(); const VectorPixelType * const upbuff_end = upptr + numPix; while ( upptr != upbuff_end ) { for (unsigned int d=0; d<Dimension; d++) { *(outptrs[d])++ = (*upptr)[d]; } ++upptr; } //mexPrintf("done outputs copy %f sec\n", timer.elapsed()); }