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; }
typename itk::Image<TPixel, VImageDimension>::Pointer MaskUtilities<TPixel, VImageDimension>::ExtractMaskImageRegion() { if (m_Mask==nullptr || m_Image==nullptr) { MITK_ERROR << "Set an image and a mask first"; } bool maskSanity = CheckMaskSanity(); if (!maskSanity) { MITK_ERROR << "Mask and image are not compatible"; } typedef itk::Image< TPixel, VImageDimension > ImageType; typedef itk::Image< unsigned short, VImageDimension > MaskType; typedef itk::ExtractImageFilter< ImageType, ImageType > ExtractImageFilterType; typename ImageType::SizeType imageSize = m_Image->GetBufferedRegion().GetSize(); typename ImageType::SizeType maskSize = m_Mask->GetBufferedRegion().GetSize(); typename itk::Image<TPixel, VImageDimension>::Pointer extractedImg = itk::Image<TPixel, VImageDimension>::New(); bool maskSmallerImage = false; for ( unsigned int i = 0; i < ImageType::ImageDimension; ++i ) { if ( maskSize[i] < imageSize[i] ) { maskSmallerImage = true; } } if ( maskSmallerImage ) { typename ExtractImageFilterType::Pointer extractImageFilter = ExtractImageFilterType::New(); typename MaskType::PointType maskOrigin = m_Mask->GetOrigin(); typename ImageType::PointType imageOrigin = m_Image->GetOrigin(); typename MaskType::SpacingType maskSpacing = m_Mask->GetSpacing(); typename ImageType::RegionType extractionRegion; typename ImageType::IndexType extractionRegionIndex; for (unsigned int i=0; i < maskOrigin.GetPointDimension(); i++) { extractionRegionIndex[i] = (maskOrigin[i] - imageOrigin[i]) / maskSpacing[i]; } extractionRegion.SetIndex(extractionRegionIndex); extractionRegion.SetSize(m_Mask->GetLargestPossibleRegion().GetSize()); extractImageFilter->SetInput( m_Image ); extractImageFilter->SetExtractionRegion( extractionRegion ); extractImageFilter->SetCoordinateTolerance( 0.001 ); extractImageFilter->SetDirectionTolerance( 0.001 ); extractImageFilter->Update(); extractedImg = extractImageFilter->GetOutput(); extractedImg->SetOrigin(m_Mask->GetOrigin()); extractedImg->SetLargestPossibleRegion(m_Mask->GetLargestPossibleRegion()); extractedImg->SetBufferedRegion(m_Mask->GetBufferedRegion()); } else { extractedImg = m_Image; } return extractedImg; }
template <class inputType, unsigned int Dimension> medAbstractJob::medJobExitStatus medItkBiasCorrectionProcess::N4BiasCorrectionCore() { medJobExitStatus eRes = medAbstractJob::MED_JOB_EXIT_SUCCESS; typedef itk::Image<inputType, Dimension > ImageType; typedef itk::Image <float, Dimension> OutputImageType; typedef itk::Image<unsigned char, Dimension> MaskImageType; typedef itk::N4BiasFieldCorrectionImageFilter<OutputImageType, MaskImageType, OutputImageType> BiasFilter; typedef itk::ConstantPadImageFilter<OutputImageType, OutputImageType> PadderType; typedef itk::ConstantPadImageFilter<MaskImageType, MaskImageType> MaskPadderType; typedef itk::ShrinkImageFilter<OutputImageType, OutputImageType> ShrinkerType; typedef itk::ShrinkImageFilter<MaskImageType, MaskImageType> MaskShrinkerType; typedef itk::BSplineControlPointImageFilter<typename BiasFilter::BiasFieldControlPointLatticeType, typename BiasFilter::ScalarImageType> BSplinerType; typedef itk::ExpImageFilter<OutputImageType, OutputImageType> ExpFilterType; typedef itk::DivideImageFilter<OutputImageType, OutputImageType, OutputImageType> DividerType; typedef itk::ExtractImageFilter<OutputImageType, OutputImageType> CropperType; unsigned int uiThreadNb = static_cast<unsigned int>(m_poUIThreadNb->value()); unsigned int uiShrinkFactors = static_cast<unsigned int>(m_poUIShrinkFactors->value()); unsigned int uiSplineOrder = static_cast<unsigned int>(m_poUISplineOrder->value()); float fWienerFilterNoise = static_cast<float>(m_poFWienerFilterNoise->value()); float fbfFWHM = static_cast<float>(m_poFbfFWHM->value()); float fConvergenceThreshold = static_cast<float>(m_poFConvergenceThreshold->value()); float fSplineDistance = static_cast<float>(m_poFSplineDistance->value()); float fProgression = 0; QStringList oListValue = m_poSMaxIterations->value().split("x"); std::vector<unsigned int> oMaxNumbersIterationsVector(oListValue.size()); std::vector<float> oInitialMeshResolutionVect(Dimension); for (int i=0; i<oMaxNumbersIterationsVector.size(); ++i) { oMaxNumbersIterationsVector[i] = (unsigned int)oListValue[i].toInt(); } oInitialMeshResolutionVect[0] = static_cast<float>(m_poFInitialMeshResolutionVect1->value()); oInitialMeshResolutionVect[1] = static_cast<float>(m_poFInitialMeshResolutionVect2->value()); oInitialMeshResolutionVect[2] = static_cast<float>(m_poFInitialMeshResolutionVect3->value()); typename ImageType::Pointer image = dynamic_cast<ImageType *>((itk::Object*)(this->input()->data())); typedef itk::CastImageFilter <ImageType, OutputImageType> CastFilterType; typename CastFilterType::Pointer castFilter = CastFilterType::New(); castFilter->SetInput(image); /********************************************************************************/ /***************************** PREPARING STARTING *******************************/ /********************************************************************************/ /*** 0 ******************* Create filter and accessories ******************/ ABORT_CHECKING(m_bAborting); typename BiasFilter::Pointer filter = BiasFilter::New(); typename BiasFilter::ArrayType oNumberOfControlPointsArray; m_filter = filter; /*** 1 ******************* Read input image *******************************/ ABORT_CHECKING(m_bAborting); fProgression = 1; updateProgression(fProgression); /*** 2 ******************* Creating Otsu mask *****************************/ ABORT_CHECKING(m_bAborting); itk::TimeProbe timer; timer.Start(); typename MaskImageType::Pointer maskImage = ITK_NULLPTR; typedef itk::OtsuThresholdImageFilter<OutputImageType, MaskImageType> ThresholderType; typename ThresholderType::Pointer otsu = ThresholderType::New(); m_filter = otsu; otsu->SetInput(castFilter->GetOutput()); otsu->SetNumberOfHistogramBins(200); otsu->SetInsideValue(0); otsu->SetOutsideValue(1); otsu->SetNumberOfThreads(uiThreadNb); otsu->Update(); updateProgression(fProgression); maskImage = otsu->GetOutput(); /*** 3A *************** Set Maximum number of Iterations for the filter ***/ ABORT_CHECKING(m_bAborting); typename BiasFilter::VariableSizeArrayType itkTabMaximumIterations; itkTabMaximumIterations.SetSize(oMaxNumbersIterationsVector.size()); for (int i = 0; i < oMaxNumbersIterationsVector.size(); ++i) { itkTabMaximumIterations[i] = oMaxNumbersIterationsVector[i]; } filter->SetMaximumNumberOfIterations(itkTabMaximumIterations); /*** 3B *************** Set Fitting Levels for the filter *****************/ typename BiasFilter::ArrayType oFittingLevelsTab; oFittingLevelsTab.Fill(oMaxNumbersIterationsVector.size()); filter->SetNumberOfFittingLevels(oFittingLevelsTab); updateProgression(fProgression); /*** 4 ******************* Save image's index, size, origine **************/ ABORT_CHECKING(m_bAborting); typename ImageType::IndexType oImageIndex = image->GetLargestPossibleRegion().GetIndex(); typename ImageType::SizeType oImageSize = image->GetLargestPossibleRegion().GetSize(); typename ImageType::PointType newOrigin = image->GetOrigin(); typename OutputImageType::Pointer outImage = castFilter->GetOutput(); if (fSplineDistance > 0) { /*** 5 ******************* Compute number of control points **************/ ABORT_CHECKING(m_bAborting); itk::SizeValueType lowerBound[3]; itk::SizeValueType upperBound[3]; for (unsigned int i = 0; i < 3; i++) { float domain = static_cast<float>(image->GetLargestPossibleRegion().GetSize()[i] - 1) * image->GetSpacing()[i]; unsigned int numberOfSpans = static_cast<unsigned int>(std::ceil(domain / fSplineDistance)); unsigned long extraPadding = static_cast<unsigned long>((numberOfSpans * fSplineDistance - domain) / image->GetSpacing()[i] + 0.5); lowerBound[i] = static_cast<unsigned long>(0.5 * extraPadding); upperBound[i] = extraPadding - lowerBound[i]; newOrigin[i] -= (static_cast<float>(lowerBound[i]) * image->GetSpacing()[i]); oNumberOfControlPointsArray[i] = numberOfSpans + filter->GetSplineOrder(); } updateProgression(fProgression); /*** 6 ******************* Padder ****************************************/ ABORT_CHECKING(m_bAborting); typename PadderType::Pointer imagePadder = PadderType::New(); m_filter = imagePadder; imagePadder->SetInput(castFilter->GetOutput()); imagePadder->SetPadLowerBound(lowerBound); imagePadder->SetPadUpperBound(upperBound); imagePadder->SetConstant(0); imagePadder->SetNumberOfThreads(uiThreadNb); imagePadder->Update(); updateProgression(fProgression); outImage = imagePadder->GetOutput(); /*** 7 ******************** Handle the mask image *************************/ ABORT_CHECKING(m_bAborting); typename MaskPadderType::Pointer maskPadder = MaskPadderType::New(); m_filter = maskPadder; maskPadder->SetInput(maskImage); maskPadder->SetPadLowerBound(lowerBound); maskPadder->SetPadUpperBound(upperBound); maskPadder->SetConstant(0); maskPadder->SetNumberOfThreads(uiThreadNb); maskPadder->Update(); updateProgression(fProgression); maskImage = maskPadder->GetOutput(); /*** 8 ******************** SetNumber Of Control Points *******************/ ABORT_CHECKING(m_bAborting); filter->SetNumberOfControlPoints(oNumberOfControlPointsArray); } else if (oInitialMeshResolutionVect.size() == 3) { /*** 9 ******************** SetNumber Of Control Points alternative *******/ ABORT_CHECKING(m_bAborting); for (unsigned i = 0; i < 3; i++) { oNumberOfControlPointsArray[i] = static_cast<unsigned int>(oInitialMeshResolutionVect[i]) + filter->GetSplineOrder(); } filter->SetNumberOfControlPoints(oNumberOfControlPointsArray); updateProgression(fProgression, 3); } else { fProgression = 0; updateProgression(fProgression); std::cout << "No BSpline distance and Mesh Resolution is ignored because not 3 dimensions" << std::endl; } /*** 10 ******************* Shrinker image ********************************/ ABORT_CHECKING(m_bAborting); typename ShrinkerType::Pointer imageShrinker = ShrinkerType::New(); m_filter = imageShrinker; imageShrinker->SetInput(outImage); /*** 11 ******************* Shrinker mask *********************************/ ABORT_CHECKING(m_bAborting); typename MaskShrinkerType::Pointer maskShrinker = MaskShrinkerType::New(); m_filter = maskShrinker; maskShrinker->SetInput(maskImage); /*** 12 ******************* Shrink mask and image *************************/ ABORT_CHECKING(m_bAborting); imageShrinker->SetShrinkFactors(uiShrinkFactors); maskShrinker->SetShrinkFactors(uiShrinkFactors); imageShrinker->SetNumberOfThreads(uiThreadNb); maskShrinker->SetNumberOfThreads(uiThreadNb); imageShrinker->Update(); updateProgression(fProgression); maskShrinker->Update(); updateProgression(fProgression); /*** 13 ******************* Filter setings ********************************/ ABORT_CHECKING(m_bAborting); filter->SetSplineOrder(uiSplineOrder); filter->SetWienerFilterNoise(fWienerFilterNoise); filter->SetBiasFieldFullWidthAtHalfMaximum(fbfFWHM); filter->SetConvergenceThreshold(fConvergenceThreshold); filter->SetInput(imageShrinker->GetOutput()); filter->SetMaskImage(maskShrinker->GetOutput()); /*** 14 ******************* Apply filter **********************************/ ABORT_CHECKING(m_bAborting); try { filter->SetNumberOfThreads(uiThreadNb); filter->Update(); updateProgression(fProgression, 5); } catch (itk::ExceptionObject & err) { std::cerr << "ExceptionObject caught !" << std::endl; std::cerr << err << std::endl; eRes = medAbstractJob::MED_JOB_EXIT_FAILURE; return eRes; } /** * Reconstruct the bias field at full image resolution. Divide * the original input image by the bias field to get the final * corrected image. */ ABORT_CHECKING(m_bAborting); typename BSplinerType::Pointer bspliner = BSplinerType::New(); m_filter = bspliner; bspliner->SetInput(filter->GetLogBiasFieldControlPointLattice()); bspliner->SetSplineOrder(filter->GetSplineOrder()); bspliner->SetSize(image->GetLargestPossibleRegion().GetSize()); bspliner->SetOrigin(newOrigin); bspliner->SetDirection(image->GetDirection()); bspliner->SetSpacing(image->GetSpacing()); bspliner->SetNumberOfThreads(uiThreadNb); bspliner->Update(); updateProgression(fProgression); /*********************** Logarithm phase ***************************/ ABORT_CHECKING(m_bAborting); typename OutputImageType::Pointer logField = OutputImageType::New(); logField->SetOrigin(image->GetOrigin()); logField->SetSpacing(image->GetSpacing()); logField->SetRegions(image->GetLargestPossibleRegion()); logField->SetDirection(image->GetDirection()); logField->Allocate(); itk::ImageRegionIterator<typename BiasFilter::ScalarImageType> IB(bspliner->GetOutput(), bspliner->GetOutput()->GetLargestPossibleRegion()); itk::ImageRegionIterator<OutputImageType> IF(logField, logField->GetLargestPossibleRegion()); for (IB.GoToBegin(), IF.GoToBegin(); !IB.IsAtEnd(); ++IB, ++IF) { IF.Set(IB.Get()[0]); } /*********************** Exponential phase *************************/ ABORT_CHECKING(m_bAborting); typename ExpFilterType::Pointer expFilter = ExpFilterType::New(); m_filter = expFilter; expFilter->SetInput(logField); expFilter->SetNumberOfThreads(uiThreadNb); expFilter->Update(); updateProgression(fProgression); /************************ Dividing phase ***************************/ ABORT_CHECKING(m_bAborting); typename DividerType::Pointer divider = DividerType::New(); m_filter = divider; divider->SetInput1(castFilter->GetOutput()); divider->SetInput2(expFilter->GetOutput()); divider->SetNumberOfThreads(uiThreadNb); divider->Update(); updateProgression(fProgression); /******************** Prepare cropping phase ***********************/ ABORT_CHECKING(m_bAborting); typename ImageType::RegionType inputRegion; inputRegion.SetIndex(oImageIndex); inputRegion.SetSize(oImageSize); /************************ Cropping phase ***************************/ ABORT_CHECKING(m_bAborting); typename CropperType::Pointer cropper = CropperType::New(); m_filter = cropper; cropper->SetInput(divider->GetOutput()); cropper->SetExtractionRegion(inputRegion); cropper->SetDirectionCollapseToSubmatrix(); cropper->SetNumberOfThreads(uiThreadNb); cropper->Update(); updateProgression(fProgression); /********************** Write output image *************************/ ABORT_CHECKING(m_bAborting); medAbstractImageData *out = qobject_cast<medAbstractImageData *>(medAbstractDataFactory::instance()->create("itkDataImageFloat3")); out->setData(cropper->GetOutput()); this->setOutput(out); m_filter = 0; return eRes; }