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; }
int testBackCasting(mitk::Image* imgMem, typename ImageType::Pointer & itkImage, bool disconnectAfterImport) { int result; if(itkImage.IsNull()) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } int *p = (int*)itkImage->GetBufferPointer(); if(p==NULL) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; std::cout << "Testing mitk::CastToMitkImage: " << std::flush; mitk::Image::Pointer mitkImage = mitk::Image::New(); mitk::CastToMitkImage( itkImage, mitkImage ); std::cout<<"[PASSED]"<<std::endl; result = compareGeometries(imgMem->GetGeometry(), mitkImage->GetGeometry()); if(result != EXIT_SUCCESS) return result; std::cout << "Testing whether data after mitk::CastToMitkImage is available: " << std::flush; if(mitkImage->IsChannelSet()==false) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; std::cout << "Testing mitk::ImportItkImage: " << std::flush; mitkImage = mitk::ImportItkImage(itkImage); std::cout<<"[PASSED]"<<std::endl; if(disconnectAfterImport) { std::cout << "Testing DisconnectPipeline() on mitk::Image into which was imported : " << std::flush; mitkImage->DisconnectPipeline(); std::cout<<"[PASSED]"<<std::endl; } result = compareGeometries(imgMem->GetGeometry(), mitkImage->GetGeometry()); if(result != EXIT_SUCCESS) return result; std::cout << "Testing whether data after mitk::ImportItkImage is available: " << std::flush; if(mitkImage->IsChannelSet()==false) { std::cout<<"[FAILED]"<<std::endl; return EXIT_FAILURE; } std::cout<<"[PASSED]"<<std::endl; return EXIT_SUCCESS; }
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 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()); }