diff --git a/Modules/DiffusionImaging/DiffusionCore/Algorithms/itkNonLocalMeansDenoisingFilter.txx b/Modules/DiffusionImaging/DiffusionCore/Algorithms/itkNonLocalMeansDenoisingFilter.txx index ff65f4a5e9..1a2fc23551 100644 --- a/Modules/DiffusionImaging/DiffusionCore/Algorithms/itkNonLocalMeansDenoisingFilter.txx +++ b/Modules/DiffusionImaging/DiffusionCore/Algorithms/itkNonLocalMeansDenoisingFilter.txx @@ -1,375 +1,375 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef __itkNonLocalMeansDenoisingFilter_txx #define __itkNonLocalMeansDenoisingFilter_txx #include #include #include #define _USE_MATH_DEFINES #include #include "itkImageRegionIterator.h" #include "itkNeighborhoodIterator.h" #include #include namespace itk { template< class TPixelType > NonLocalMeansDenoisingFilter< TPixelType > ::NonLocalMeansDenoisingFilter() - : m_SearchRadius(5), + : m_SearchRadius(4), m_ComparisonRadius(1), m_UseJointInformation(false), m_UseRicianAdaption(false), m_Variance(1), m_Mask(NULL) { this->SetNumberOfRequiredInputs( 1 ); } template< class TPixelType > void NonLocalMeansDenoisingFilter< TPixelType > ::BeforeThreadedGenerateData() { MITK_INFO << "Variance: " << m_Variance; MITK_INFO << "SearchRadius: " << m_SearchRadius; MITK_INFO << "ComparisonRadius: " << m_ComparisonRadius; MITK_INFO << "Use Joint Information: " << std::boolalpha << m_UseJointInformation; MITK_INFO << "Use Rician Adaption: " << std::boolalpha << m_UseRicianAdaption; typename InputImageType::Pointer inputImagePointer = static_cast< InputImageType * >( this->ProcessObject::GetInput(0) ); if (m_Mask.IsNull()) { m_Mask = MaskImageType::New(); m_Mask->SetRegions(inputImagePointer->GetLargestPossibleRegion()); m_Mask->Allocate(); m_Mask->FillBuffer(1); } else { ImageRegionIterator< MaskImageType > mit(m_Mask, m_Mask->GetLargestPossibleRegion()); mit.GoToBegin(); typename MaskImageType::IndexType minIndex; typename MaskImageType::IndexType maxIndex; minIndex.Fill(10000); maxIndex.Fill(0); while (!mit.IsAtEnd()) { if (mit.Get()) { minIndex[0] = minIndex[0] < mit.GetIndex()[0] ? minIndex[0] : mit.GetIndex()[0]; minIndex[1] = minIndex[1] < mit.GetIndex()[1] ? minIndex[1] : mit.GetIndex()[1]; minIndex[2] = minIndex[2] < mit.GetIndex()[2] ? minIndex[2] : mit.GetIndex()[2]; maxIndex[0] = maxIndex[0] > mit.GetIndex()[0] ? maxIndex[0] : mit.GetIndex()[0]; maxIndex[1] = maxIndex[1] > mit.GetIndex()[1] ? maxIndex[1] : mit.GetIndex()[1]; maxIndex[2] = maxIndex[2] > mit.GetIndex()[2] ? maxIndex[2] : mit.GetIndex()[2]; } ++mit; } typename OutputImageType::SizeType size; size[0] = maxIndex[0] - minIndex[0]; size[1] = maxIndex[1] - minIndex[1]; size[2] = maxIndex[2] - minIndex[2]; typename OutputImageType::RegionType region (minIndex, size); typename OutputImageType::Pointer outputImage = static_cast< OutputImageType * >(this->ProcessObject::GetOutput(0)); outputImage->SetRequestedRegion(region); } m_CurrentVoxelCount = 0; } template< class TPixelType > void NonLocalMeansDenoisingFilter< TPixelType > ::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, ThreadIdType ) { // initialize iterators typename OutputImageType::Pointer outputImage = static_cast< OutputImageType * >(this->ProcessObject::GetOutput(0)); ImageRegionIterator< OutputImageType > oit(outputImage, outputRegionForThread); oit.GoToBegin(); // typename MaskImageType::Pointer maskImage = // static_cast< MaskImageType* >( this->ProcessObject::GetInput(1) ); ImageRegionIterator< MaskImageType > mit(m_Mask, outputRegionForThread); mit.GoToBegin(); typedef ImageRegionIteratorWithIndex InputIteratorType; typename InputImageType::Pointer inputImagePointer = NULL; inputImagePointer = static_cast< InputImageType * >( this->ProcessObject::GetInput(0) ); InputIteratorType git(inputImagePointer, outputRegionForThread ); git.GoToBegin(); // iterate over complete image region while( !git.IsAtEnd() ) { typename OutputImageType::PixelType outpix; outpix.SetSize (inputImagePointer->GetVectorLength()); if (mit.Get() != 0) { if(!m_UseJointInformation) { for (int i = 0; i < (int)inputImagePointer->GetVectorLength(); ++i) { double summw = 0; double sumj = 0; double w = 0; std::vector wj; std::vector p; typename InputIteratorType::IndexType index; index = git.GetIndex(); for (int x = index.GetElement(0) - m_SearchRadius; x <= index.GetElement(0) + m_SearchRadius; ++x) { for (int y = index.GetElement(1) - m_SearchRadius; y <= index.GetElement(1) + m_SearchRadius; ++y) { for (int z = index.GetElement(2) - m_SearchRadius; z <= index.GetElement(2) + m_SearchRadius; ++z) { typename InputIteratorType::IndexType indexV; indexV.SetElement(0, x); indexV.SetElement(1, y); indexV.SetElement(2, z); if (inputImagePointer->GetLargestPossibleRegion().IsInside(indexV)) { TPixelType pixelJ = inputImagePointer->GetPixel(indexV)[i]; double sumk = 0; double size = 0; for (int xi = index.GetElement(0) - m_ComparisonRadius, xj = x - m_ComparisonRadius; xi <= index.GetElement(0) + m_ComparisonRadius; ++xi, ++xj) { for (int yi = index.GetElement(1) - m_ComparisonRadius, yj = y - m_ComparisonRadius; yi <= index.GetElement(1) + m_ComparisonRadius; ++yi, ++yj) { for (int zi = index.GetElement(2) - m_ComparisonRadius, zj = z - m_ComparisonRadius; zi <= index.GetElement(2) + m_ComparisonRadius; ++zi, ++zj) { typename InputIteratorType::IndexType indexI, indexJ; indexI.SetElement(0, xi); indexI.SetElement(1, yi); indexI.SetElement(2, zi); indexJ.SetElement(0, xj); indexJ.SetElement(1, yj); indexJ.SetElement(2, zj); // Compare neighborhoods ni & nj if (inputImagePointer->GetLargestPossibleRegion().IsInside(indexI) && inputImagePointer->GetLargestPossibleRegion().IsInside(indexJ)) { int diff = inputImagePointer->GetPixel(indexI)[i] - inputImagePointer->GetPixel(indexJ)[i]; sumk += (double)(diff*diff); ++size; } } } } // weight all neighborhoods w = std::exp( - sumk / size / m_Variance); wj.push_back(w); if (m_UseRicianAdaption) { p.push_back((double)(pixelJ*pixelJ)); } else { p.push_back((double)(pixelJ)); } summw += w; } } } } for (unsigned int n = 0; n < wj.size(); ++n) { sumj += (wj[n]/summw) * p[n]; } if (m_UseRicianAdaption) { sumj -=2 * m_Variance; } if (sumj < 0) { sumj = 0; } TPixelType outval; if (m_UseRicianAdaption) { outval = std::floor(std::sqrt(sumj) + 0.5); } else { outval = std::floor(sumj + 0.5); } outpix.SetElement(i, outval); } } else { double Z = 0; itk::VariableLengthVector sumj; sumj.SetSize(inputImagePointer->GetVectorLength()); sumj.Fill(0.0); double w = 0; std::vector wj; std::vector > p; typename InputIteratorType::IndexType index; index = git.GetIndex(); for (int x = index.GetElement(0) - m_SearchRadius; x <= index.GetElement(0) + m_SearchRadius; ++x) { for (int y = index.GetElement(1) - m_SearchRadius; y <= index.GetElement(1) + m_SearchRadius; ++y) { for (int z = index.GetElement(2) - m_SearchRadius; z <= index.GetElement(2) + m_SearchRadius; ++z) { typename InputIteratorType::IndexType indexV; indexV.SetElement(0, x); indexV.SetElement(1, y); indexV.SetElement(2, z); if (inputImagePointer->GetLargestPossibleRegion().IsInside(indexV)) { typename InputImageType::PixelType pixelJ = inputImagePointer->GetPixel(indexV); double sumk = 0; double size = 0; for (int xi = index.GetElement(0) - m_ComparisonRadius, xj = x - m_ComparisonRadius; xi <= index.GetElement(0) + m_ComparisonRadius; ++xi, ++xj) { for (int yi = index.GetElement(1) - m_ComparisonRadius, yj = y - m_ComparisonRadius; yi <= index.GetElement(1) + m_ComparisonRadius; ++yi, ++yj) { for (int zi = index.GetElement(2) - m_ComparisonRadius, zj = z - m_ComparisonRadius; zi <= index.GetElement(2) + m_ComparisonRadius; ++zi, ++zj) { typename InputIteratorType::IndexType indexI, indexJ; indexI.SetElement(0, xi); indexI.SetElement(1, yi); indexI.SetElement(2, zi); indexJ.SetElement(0, xj); indexJ.SetElement(1, yj); indexJ.SetElement(2, zj); // Compare neighborhoods ni & nj if (inputImagePointer->GetLargestPossibleRegion().IsInside(indexI) && inputImagePointer->GetLargestPossibleRegion().IsInside(indexJ)) { typename InputImageType::PixelType diff = inputImagePointer->GetPixel(indexI) - inputImagePointer->GetPixel(indexJ); sumk += (double)(diff.GetSquaredNorm()); ++size; } } } } // weight all neighborhoods size *= inputImagePointer->GetVectorLength() + 1; w = std::exp( - (sumk / size) / m_Variance); wj.push_back(w); if (m_UseRicianAdaption) { itk::VariableLengthVector m; m.SetSize(inputImagePointer->GetVectorLength()); for (unsigned int i = 0; i < inputImagePointer->GetVectorLength(); ++i) { double sp = (double)(pixelJ.GetElement(i) * pixelJ.GetElement(i)); m.SetElement(i,sp); } p.push_back(m); } else ++size; { p.push_back(pixelJ); } Z += w; } } } } for (unsigned int n = 0; n < wj.size(); ++n) { sumj = sumj + ((wj[n]/Z) * p[n]); } if (m_UseRicianAdaption) { sumj = sumj - (2 * m_Variance); } for (unsigned int i = 0; i < inputImagePointer->GetVectorLength(); ++i) { double a = sumj.GetElement(i); if (a < 0) { a = 0; } TPixelType outval; if (m_UseRicianAdaption) { outval = std::floor(std::sqrt(a) + 0.5); } else { outval = std::floor(a + 0.5); } outpix.SetElement(i, outval); } } } else { outpix.Fill(0); } oit.Set(outpix); ++oit; ++m_CurrentVoxelCount; ++git; ++mit; } MITK_INFO << "One Thread finished calculation"; } template< class TPixelType > void NonLocalMeansDenoisingFilter< TPixelType >::PrintSelf(std::ostream& os, Indent indent) const { Superclass::PrintSelf(os,indent); } template< class TPixelType > void NonLocalMeansDenoisingFilter< TPixelType >::SetInputImage(const InputImageType* image) { this->SetNthInput(0, const_cast< InputImageType* >(image)); } template< class TPixelType > void NonLocalMeansDenoisingFilter< TPixelType >::SetInputMask(MaskImageType* mask) { //this->SetNthInput(1, const_cast< MaskImageType* >(mask)); m_Mask = mask; } } #endif // __itkNonLocalMeansDenoisingFilter_txx diff --git a/Modules/DiffusionImaging/MiniApps/DwiDenoising.cpp b/Modules/DiffusionImaging/MiniApps/DwiDenoising.cpp index 0bdac8ead8..95acbf1842 100644 --- a/Modules/DiffusionImaging/MiniApps/DwiDenoising.cpp +++ b/Modules/DiffusionImaging/MiniApps/DwiDenoising.cpp @@ -1,172 +1,172 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "MiniAppManager.h" #include #include #include "ctkCommandLineParser.h" #include #include #include #include #include typedef mitk::DiffusionImage DiffusionImageType; typedef itk::Image ImageType; mitk::BaseData::Pointer LoadFile(std::string filename) { if( filename.empty() ) return NULL; const std::string s1="", s2=""; std::vector infile = mitk::BaseDataIO::LoadBaseDataFromFile( filename, s1, s2, false ); if( infile.empty() ) { MITK_INFO << "File " << filename << " could not be read!"; return NULL; } mitk::BaseData::Pointer baseData = infile.at(0); return baseData; } /** * Denoises DWI using the Nonlocal - Means algorithm */ int DwiDenoising(int argc, char* argv[]) { ctkCommandLineParser parser; parser.setArgumentPrefix("--", "-"); parser.addArgument("input", "i", ctkCommandLineParser::String, "input image (DWI)", us::Any(), false); parser.addArgument("mask", "m", ctkCommandLineParser::String, "brainmask for input image", us::Any(), false); parser.addArgument("search", "s", ctkCommandLineParser::Int, "search radius", us::Any(), false); parser.addArgument("compare", "c", ctkCommandLineParser::Int, "compare radius", us::Any(), false); parser.addArgument("channels", "ch", ctkCommandLineParser::Int, "radius of used channels", us::Any(), true); map parsedArgs = parser.parseArguments(argc, argv); if (parsedArgs.size()==0) return EXIT_FAILURE; string inFileName = us::any_cast(parsedArgs["input"]); string maskName = us::any_cast(parsedArgs["mask"]); string outFileName = inFileName; boost::algorithm::erase_all(outFileName, ".dwi"); int search = us::any_cast(parsedArgs["search"]); int compare = us::any_cast(parsedArgs["compare"]); int channels = 0; if (parsedArgs.count("channels")) channels = us::any_cast(parsedArgs["channels"]); try { if( boost::algorithm::ends_with(inFileName, ".dwi")) { DiffusionImageType::Pointer dwi = dynamic_cast(LoadFile(inFileName).GetPointer()); mitk::Image::Pointer mask = dynamic_cast(LoadFile(maskName).GetPointer()); ImageType::Pointer itkMask = ImageType::New(); mitk::CastToItkImage(mask, itkMask); itk::NonLocalMeansDenoisingFilter::Pointer filter = itk::NonLocalMeansDenoisingFilter::New(); filter->SetNumberOfThreads(12); filter->SetInputImage(dwi->GetVectorImage()); filter->SetInputMask(itkMask); if (channels == 0) { MITK_INFO << "Denoising with: s = " << search << "; c = " << compare; filter->SetUseJointInformation(false); filter->SetSearchRadius(search); filter->SetComparisonRadius(compare); filter->Update(); DiffusionImageType::Pointer output = DiffusionImageType::New(); output->SetVectorImage(filter->GetOutput()); output->SetReferenceBValue(dwi->GetReferenceBValue()); output->SetDirections(dwi->GetDirections()); output->InitializeFromVectorImage(); std::stringstream name; name << outFileName << "_NLMr_" << search << "-" << compare << ".dwi"; MITK_INFO << "Writing: " << name.str(); mitk::NrrdDiffusionImageWriter::Pointer writer = mitk::NrrdDiffusionImageWriter::New(); writer->SetInput(output); writer->SetFileName(name.str()); writer->Update(); } else { MITK_INFO << "Denoising with: s = " << search << "; c = " << compare << "; ch = " << channels; filter->SetUseJointInformation(true); filter->SetSearchRadius(search); filter->SetComparisonRadius(compare); - filter->SetChannelRadius(channels); +// filter->SetChannelRadius(channels); filter->Update(); DiffusionImageType::Pointer output = DiffusionImageType::New(); output->SetVectorImage(filter->GetOutput()); output->SetReferenceBValue(dwi->GetReferenceBValue()); output->SetDirections(dwi->GetDirections()); output->InitializeFromVectorImage(); std::stringstream name; name << outFileName << "_NLMv_" << search << "-" << compare << "-" << channels << ".dwi"; MITK_INFO << "Writing " << name.str(); mitk::NrrdDiffusionImageWriter::Pointer writer = mitk::NrrdDiffusionImageWriter::New(); writer->SetInput(output); writer->SetFileName(name.str()); writer->Update(); } MITK_INFO << "Finish!"; } else { MITK_INFO << "Only supported for .dwi!"; } } catch (itk::ExceptionObject e) { MITK_INFO << e; return EXIT_FAILURE; } catch (std::exception e) { MITK_INFO << e.what(); return EXIT_FAILURE; } catch (...) { MITK_INFO << "ERROR!?!"; return EXIT_FAILURE; } return EXIT_SUCCESS; } RegisterDiffusionMiniApp(DwiDenoising); diff --git a/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkDenoisingView.cpp b/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkDenoisingView.cpp index 4128697d60..62a9117491 100644 --- a/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkDenoisingView.cpp +++ b/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkDenoisingView.cpp @@ -1,500 +1,444 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ // Blueberry #include #include // Qmitk #include "QmitkDenoisingView.h" #include #include QmitkDenoisingWorker::QmitkDenoisingWorker(QmitkDenoisingView *view) : m_View(view) { } void QmitkDenoisingWorker::run() { if (m_View->m_ImageNode.IsNotNull() && m_View->m_BrainMaskNode.IsNotNull()) { switch (m_View->m_SelectedFilter) { case 0: - case 3: { break; } case 1: case 2: { try { m_View->m_NoExceptionThrown = true; m_View->m_NonLocalMeansFilter->Update(); } catch (itk::ExceptionObject& e) { m_View->m_NoExceptionThrown = false; MITK_ERROR << e.what(); } break; } } m_View->m_DenoisingThread.quit(); } } const std::string QmitkDenoisingView::VIEW_ID = "org.mitk.views.denoisingview"; QmitkDenoisingView::QmitkDenoisingView() : QmitkFunctionality() , m_Controls( 0 ) , m_ImageNode(NULL) , m_BrainMaskNode(NULL) , m_DenoisingWorker(this) , m_ThreadIsRunning(false) , m_NonLocalMeansFilter(NULL) , m_InputImage(NULL) , m_LastProgressCount(0) , m_MaxProgressCount(0) { m_DenoisingWorker.moveToThread(&m_DenoisingThread); connect(&m_DenoisingThread, SIGNAL(started()), this, SLOT(BeforeThread())); connect(&m_DenoisingThread, SIGNAL(started()), &m_DenoisingWorker, SLOT(run())); connect(&m_DenoisingThread, SIGNAL(finished()), this, SLOT(AfterThread())); connect(&m_DenoisingThread, SIGNAL(terminated()), this, SLOT(AfterThread())); m_DenoisingTimer = new QTimer(this); } QmitkDenoisingView::~QmitkDenoisingView() { delete m_DenoisingTimer; } void QmitkDenoisingView::CreateQtPartControl( QWidget *parent ) { // build up qt view, unless already done if ( !m_Controls ) { // create GUI widgets from the Qt Designer's .ui file m_Controls = new Ui::QmitkDenoisingViewControls; m_Controls->setupUi( parent ); CreateConnections(); ResetParameterPanel(); } } void QmitkDenoisingView::CreateConnections() { if ( m_Controls ) { connect( (QObject*)(m_Controls->m_ApplyButton), SIGNAL(clicked()), this, SLOT(StartDenoising())); connect( (QObject*)(m_Controls->m_SelectFilterComboBox), SIGNAL(activated(int)), this, SLOT(SelectFilter(int))); connect( m_DenoisingTimer, SIGNAL(timeout()), this, SLOT(UpdateProgress())); } } void QmitkDenoisingView::Activated() { QmitkFunctionality::Activated(); m_Controls->m_SelectFilterComboBox->clear(); m_Controls->m_SelectFilterComboBox->insertItem(NOFILTERSELECTED, QString( QApplication::translate("QmitkDenoisingView", "Please select a filter", 0, QApplication::UnicodeUTF8) )); - m_Controls->m_SelectFilterComboBox->insertItem(NLMR, QString( QApplication::translate("QmitkDenoisingView", "Non local means filter", 0, QApplication::UnicodeUTF8) )); - m_Controls->m_SelectFilterComboBox->insertItem(NLMV, QString( QApplication::translate("QmitkDenoisingView", "Non local means filter with joint information", 0, QApplication::UnicodeUTF8) )); + m_Controls->m_SelectFilterComboBox->insertItem(NLM, QString( QApplication::translate("QmitkDenoisingView", "Non local means filter", 0, QApplication::UnicodeUTF8) )); +// m_Controls->m_SelectFilterComboBox->insertItem(NLMV, QString( QApplication::translate("QmitkDenoisingView", "Non local means filter with joint information", 0, QApplication::UnicodeUTF8) )); m_Controls->m_SelectFilterComboBox->insertItem(GAUSS, QString( QApplication::translate("QmitkDenoisingView", "Discrete gaussian filter", 0, QApplication::UnicodeUTF8) )); } void QmitkDenoisingView::OnSelectionChanged( std::vector nodes ) { if (m_ThreadIsRunning) return; if (m_SelectedFilter != NOFILTERSELECTED) { m_Controls->m_InputImageLabel->setText("mandatory"); - m_Controls->m_InputBrainMaskLabel->setText("mandatory"); } else { m_Controls->m_InputImageLabel->setText("mandatory"); } + m_Controls->m_InputBrainMaskLabel->setText("optional"); m_Controls->m_ApplyButton->setEnabled(false); m_ImageNode = NULL; m_BrainMaskNode = NULL; // iterate selection for( std::vector::iterator it = nodes.begin(); it != nodes.end(); ++it ) { mitk::DataNode::Pointer node = *it; if( node.IsNotNull() && dynamic_cast(node->GetData())) { m_Controls->m_InputImageLabel->setText(node->GetName().c_str()); m_ImageNode = node; } bool isBinary = false; node->GetBoolProperty("binary", isBinary); // look for a brainmask in selection if( node.IsNotNull() && static_cast(node->GetData()) && isBinary) { m_Controls->m_InputBrainMaskLabel->setText(node->GetName().c_str()); m_BrainMaskNode = node; } } // Preparation of GUI to start denoising if a filter is selected - if (m_ImageNode.IsNotNull() && m_BrainMaskNode.IsNotNull()) - { - if(m_SelectedFilter != NOFILTERSELECTED) - { - m_Controls->m_ApplyButton->setEnabled(true); - } - } - else if (m_ImageNode.IsNotNull() && m_SelectedFilter == GAUSS) + if (m_ImageNode.IsNotNull()) { m_Controls->m_ApplyButton->setEnabled(true); } } void QmitkDenoisingView::StartDenoising() { - if (m_ImageNode.IsNotNull() && m_BrainMaskNode.IsNotNull() && m_SelectedFilter != GAUSS) + if (m_ImageNode.IsNotNull()) { m_LastProgressCount = 0; switch (m_SelectedFilter) { case NOFILTERSELECTED: - case GAUSS: { break; } - case NLMR: + case NLM: { // initialize NLMr m_InputImage = dynamic_cast (m_ImageNode->GetData()); - m_ImageMask = dynamic_cast(m_BrainMaskNode->GetData()); - itk::Image::Pointer itkMask = itk::Image::New(); - mitk::CastToItkImage(m_ImageMask, itkMask); m_NonLocalMeansFilter = NonLocalMeansDenoisingFilterType::New(); m_NonLocalMeansFilter->SetNumberOfThreads(12); m_NonLocalMeansFilter->SetInputImage(m_InputImage->GetVectorImage()); - m_NonLocalMeansFilter->SetInputMask(itkMask); - m_NonLocalMeansFilter->SetUseJointInformation(false); + m_NonLocalMeansFilter->SetUseRicianAdaption(m_Controls->m_RicianCheckbox->isChecked()); + m_NonLocalMeansFilter->SetUseJointInformation(m_Controls->m_JointInformationCheckbox->isChecked()); m_NonLocalMeansFilter->SetSearchRadius(m_Controls->m_SpinBoxParameter1->value()); m_NonLocalMeansFilter->SetComparisonRadius(m_Controls->m_SpinBoxParameter2->value()); + if (m_BrainMaskNode.IsNotNull()) + { + // use brainmask if set + m_ImageMask = dynamic_cast(m_BrainMaskNode->GetData()); + itk::Image::Pointer itkMask = itk::Image::New(); + mitk::CastToItkImage(m_ImageMask, itkMask); + m_NonLocalMeansFilter->SetInputMask(itkMask); + } + // initialize the progressbar m_MaxProgressCount = m_InputImage->GetDimension(0) * m_InputImage->GetDimension(1) * m_InputImage->GetDimension(2); mitk::ProgressBar::GetInstance()->AddStepsToDo(m_MaxProgressCount); // start denoising in detached thread m_DenoisingThread.start(QThread::HighestPriority); break; } - case NLMV: + case GAUSS: { - // initialize NLMv + // initialize GAUSS m_InputImage = dynamic_cast (m_ImageNode->GetData()); - m_ImageMask = dynamic_cast(m_BrainMaskNode->GetData()); - itk::Image::Pointer itkMask = itk::Image::New(); - mitk::CastToItkImage(m_ImageMask, itkMask); - m_NonLocalMeansFilter = NonLocalMeansDenoisingFilterType::New(); - m_NonLocalMeansFilter->SetNumberOfThreads(1); - m_NonLocalMeansFilter->SetInputImage(m_InputImage->GetVectorImage()); - m_NonLocalMeansFilter->SetInputMask(itkMask); - m_NonLocalMeansFilter->SetUseJointInformation(true); - m_NonLocalMeansFilter->SetSearchRadius(m_Controls->m_SpinBoxParameter1->value()); - m_NonLocalMeansFilter->SetComparisonRadius(m_Controls->m_SpinBoxParameter2->value()); -// m_NonLocalMeansFilter->SetChannelRadius(m_Controls->m_SpinBoxParameter3->value()); + ExtractFilterType::Pointer extractor = ExtractFilterType::New(); + extractor->SetInput(m_InputImage->GetVectorImage()); + ComposeFilterType::Pointer composer = ComposeFilterType::New(); - // initialize the progressbar - m_MaxProgressCount = m_InputImage->GetDimension(0) * m_InputImage->GetDimension(1) * m_InputImage->GetDimension(2); - mitk::ProgressBar::GetInstance()->AddStepsToDo(m_MaxProgressCount); + for (unsigned int i = 0; i < m_InputImage->GetVectorImage()->GetVectorLength(); ++i) + { + extractor->SetIndex(i); + extractor->Update(); - // start denoising in detached thread - m_DenoisingThread.start(QThread::HighestPriority); + m_GaussianFilter = GaussianFilterType::New(); + m_GaussianFilter->SetInput(extractor->GetOutput()); + m_GaussianFilter->SetVariance(m_Controls->m_SpinBoxParameter1->value()); + m_GaussianFilter->Update(); - break; - } - } - } - else if(m_SelectedFilter == GAUSS && m_ImageNode.IsNotNull()) - { - // initialize GAUSS - m_InputImage = dynamic_cast (m_ImageNode->GetData()); + composer->SetInput(i, m_GaussianFilter->GetOutput()); + } + composer->Update(); - ExtractFilterType::Pointer extractor = ExtractFilterType::New(); - extractor->SetInput(m_InputImage->GetVectorImage()); - ComposeFilterType::Pointer composer = ComposeFilterType::New(); - for (unsigned int i = 0; i < m_InputImage->GetVectorImage()->GetVectorLength(); ++i) - { - extractor->SetIndex(i); - extractor->Update(); + DiffusionImageType::Pointer image = DiffusionImageType::New(); + image->SetVectorImage(composer->GetOutput()); + image->SetReferenceBValue(m_InputImage->GetReferenceBValue()); + image->SetDirections(m_InputImage->GetDirections()); + image->InitializeFromVectorImage(); + mitk::DataNode::Pointer imageNode = mitk::DataNode::New(); + imageNode->SetData( image ); + QString name = m_ImageNode->GetName().c_str(); - m_GaussianFilter = GaussianFilterType::New(); - m_GaussianFilter->SetInput(extractor->GetOutput()); - m_GaussianFilter->SetVariance(m_Controls->m_SpinBoxParameter1->value()); - m_GaussianFilter->Update(); + imageNode->SetName((name+"_gauss_"+QString::number(m_Controls->m_SpinBoxParameter1->value())).toStdString().c_str()); + GetDefaultDataStorage()->Add(imageNode); - composer->SetInput(i, m_GaussianFilter->GetOutput()); + break; + } } - composer->Update(); - - - DiffusionImageType::Pointer image = DiffusionImageType::New(); - image->SetVectorImage(composer->GetOutput()); - image->SetReferenceBValue(m_InputImage->GetReferenceBValue()); - image->SetDirections(m_InputImage->GetDirections()); - image->InitializeFromVectorImage(); - mitk::DataNode::Pointer imageNode = mitk::DataNode::New(); - imageNode->SetData( image ); - QString name = m_ImageNode->GetName().c_str(); - - imageNode->SetName((name+"_gauss_"+QString::number(m_Controls->m_SpinBoxParameter1->value())).toStdString().c_str()); - GetDefaultDataStorage()->Add(imageNode); } } void QmitkDenoisingView::ResetParameterPanel() { m_Controls->m_DwiLabel->setEnabled(false); m_Controls->m_InputImageLabel->setEnabled(false); - m_Controls->m_BrainMaskLabel->hide(); - m_Controls->m_InputBrainMaskLabel->hide(); + m_Controls->m_BrainMaskLabel->setEnabled(false); + m_Controls->m_InputBrainMaskLabel->setEnabled(false); m_Controls->m_ParameterBox->hide(); m_Controls->m_LabelParameter_1->hide(); m_Controls->m_LabelParameter_2->hide(); m_Controls->m_LabelParameter_3->hide(); m_Controls->m_SpinBoxParameter1->hide(); m_Controls->m_SpinBoxParameter2->hide(); - m_Controls->m_SpinBoxParameter3->hide(); + m_Controls->m_DoubleSpinBoxParameter3->hide(); + m_Controls->m_RicianLabel->hide(); + m_Controls->m_RicianCheckbox->hide(); + m_Controls->m_JointInformationLabel->hide(); + m_Controls->m_JointInformationCheckbox->hide(); } void QmitkDenoisingView::SelectFilter(int filter) { if (m_ThreadIsRunning) return; //Prepare GUI this->ResetParameterPanel(); switch (filter) { case 0: { m_SelectedFilter = NOFILTERSELECTED; break; } - case 1: - { - m_SelectedFilter = NLMR; - m_Controls->m_DwiLabel->setEnabled(true); - m_Controls->m_InputImageLabel->setEnabled(true); - m_Controls->m_BrainMaskLabel->show(); - m_Controls->m_InputBrainMaskLabel->show(); - m_Controls->m_ParameterBox->show(); - m_Controls->m_LabelParameter_1->show(); - m_Controls->m_LabelParameter_1->setText("Search Radius:"); - m_Controls->m_LabelParameter_2->show(); - m_Controls->m_LabelParameter_2->setText("Comparision Radius:"); - m_Controls->m_SpinBoxParameter1->show(); - m_Controls->m_SpinBoxParameter1->setValue(1); - m_Controls->m_SpinBoxParameter2->show(); - m_Controls->m_SpinBoxParameter2->setValue(1); - break; - } - case 2: + case 1: { - m_SelectedFilter = NLMV; + m_SelectedFilter = NLM; m_Controls->m_DwiLabel->setEnabled(true); m_Controls->m_InputImageLabel->setEnabled(true); - m_Controls->m_BrainMaskLabel->show(); - m_Controls->m_InputBrainMaskLabel->show(); + m_Controls->m_BrainMaskLabel->setEnabled(true); + m_Controls->m_InputBrainMaskLabel->setEnabled(true); m_Controls->m_ParameterBox->show(); m_Controls->m_LabelParameter_1->show(); m_Controls->m_LabelParameter_1->setText("Search Radius:"); m_Controls->m_LabelParameter_2->show(); m_Controls->m_LabelParameter_2->setText("Comparision Radius:"); m_Controls->m_LabelParameter_3->show(); - m_Controls->m_LabelParameter_3->setText("Number of neighboring gradients:"); + m_Controls->m_LabelParameter_3->setText("Noise variance:"); m_Controls->m_SpinBoxParameter1->show(); - m_Controls->m_SpinBoxParameter1->setValue(1); + m_Controls->m_SpinBoxParameter1->setValue(4); m_Controls->m_SpinBoxParameter2->show(); m_Controls->m_SpinBoxParameter2->setValue(1); - m_Controls->m_SpinBoxParameter3->show(); - m_Controls->m_SpinBoxParameter3->setValue(1); + m_Controls->m_DoubleSpinBoxParameter3->show(); + m_Controls->m_DoubleSpinBoxParameter3->setValue(1.0); + m_Controls->m_RicianLabel->show(); + m_Controls->m_RicianCheckbox->show(); + m_Controls->m_RicianCheckbox->setChecked(true); + m_Controls->m_JointInformationLabel->show(); + m_Controls->m_JointInformationCheckbox->show(); + m_Controls->m_JointInformationCheckbox->setChecked(false); break; } - case 3: + + case 2: { m_SelectedFilter = GAUSS; m_Controls->m_DwiLabel->setEnabled(true); m_Controls->m_InputImageLabel->setEnabled(true); + m_Controls->m_BrainMaskLabel->setEnabled(true); + m_Controls->m_InputBrainMaskLabel->setEnabled(true); m_Controls->m_ParameterBox->show(); m_Controls->m_LabelParameter_1->show(); m_Controls->m_LabelParameter_1->setText("Variance:"); m_Controls->m_SpinBoxParameter1->show(); m_Controls->m_SpinBoxParameter1->setValue(2); - m_Controls->m_LabelParameter_2->hide(); - m_Controls->m_SpinBoxParameter2->hide(); + + break; } } if (m_ImageNode.IsNull()) { if (m_SelectedFilter != NOFILTERSELECTED) m_Controls->m_InputImageLabel->setText("mandatory"); else m_Controls->m_InputImageLabel->setText("mandatory"); } if (m_ImageNode.IsNotNull()) { m_Controls->m_ApplyButton->setEnabled(false); switch(filter) { case NOFILTERSELECTED: { break; } - case NLMR: - case NLMV: - { - if (m_BrainMaskNode.IsNotNull()) - m_Controls->m_ApplyButton->setEnabled(true); - break; - } + case NLM: case GAUSS: { m_Controls->m_ApplyButton->setEnabled(true); break; } } } } void QmitkDenoisingView::BeforeThread() { m_ThreadIsRunning = true; + // initialize timer to update the progressbar at each timestep m_DenoisingTimer->start(500); m_Controls->m_LabelParameter_1->setEnabled(false); m_Controls->m_LabelParameter_2->setEnabled(false); m_Controls->m_LabelParameter_3->setEnabled(false); m_Controls->m_SpinBoxParameter1->setEnabled(false); m_Controls->m_SpinBoxParameter2->setEnabled(false); - m_Controls->m_SpinBoxParameter3->setEnabled(false); + m_Controls->m_DoubleSpinBoxParameter3->setEnabled(false); m_Controls->m_SelectFilterComboBox->setEnabled(false); m_Controls->m_ApplyButton->setEnabled(false); } void QmitkDenoisingView::AfterThread() { m_ThreadIsRunning = false; // stop timer to stop updates of progressbar m_DenoisingTimer->stop(); // make sure progressbar is finished mitk::ProgressBar::GetInstance()->Progress(m_MaxProgressCount); if (m_NoExceptionThrown) { switch (m_SelectedFilter) { case NOFILTERSELECTED: case GAUSS: { break; } - case NLMR: - { - DiffusionImageType::Pointer image = DiffusionImageType::New(); - image->SetVectorImage(m_NonLocalMeansFilter->GetOutput()); - image->SetReferenceBValue(m_InputImage->GetReferenceBValue()); - image->SetDirections(m_InputImage->GetDirections()); - image->InitializeFromVectorImage(); - mitk::DataNode::Pointer imageNode = mitk::DataNode::New(); - imageNode->SetData( image ); - QString name = m_ImageNode->GetName().c_str(); - - imageNode->SetName((name+"_NLMr_"+QString::number(m_Controls->m_SpinBoxParameter1->value())+"-"+QString::number(m_Controls->m_SpinBoxParameter2->value())).toStdString().c_str()); - GetDefaultDataStorage()->Add(imageNode); - break; - } - - case NLMV: + case NLM: { DiffusionImageType::Pointer image = DiffusionImageType::New(); image->SetVectorImage(m_NonLocalMeansFilter->GetOutput()); image->SetReferenceBValue(m_InputImage->GetReferenceBValue()); image->SetDirections(m_InputImage->GetDirections()); image->InitializeFromVectorImage(); mitk::DataNode::Pointer imageNode = mitk::DataNode::New(); imageNode->SetData( image ); QString name = m_ImageNode->GetName().c_str(); - imageNode->SetName((name+"_NLMv_"+QString::number(m_Controls->m_SpinBoxParameter1->value())+"-"+QString::number(m_Controls->m_SpinBoxParameter2->value())+"-"+QString::number(m_Controls->m_SpinBoxParameter3->value())).toStdString().c_str()); + //TODO: Rician adaption & joint information in name + imageNode->SetName((name+"_NLM_"+QString::number(m_Controls->m_SpinBoxParameter1->value())+"-"+QString::number(m_Controls->m_SpinBoxParameter2->value())).toStdString().c_str()); GetDefaultDataStorage()->Add(imageNode); - - m_Controls->m_LabelParameter_3->setEnabled(true); - m_Controls->m_SpinBoxParameter3->setEnabled(true); break; } } } m_Controls->m_LabelParameter_1->setEnabled(true); m_Controls->m_LabelParameter_2->setEnabled(true); m_Controls->m_SpinBoxParameter1->setEnabled(true); m_Controls->m_SpinBoxParameter2->setEnabled(true); m_Controls->m_SelectFilterComboBox->setEnabled(true); m_Controls->m_ApplyButton->setEnabled(true); } void QmitkDenoisingView::UpdateProgress() { switch (m_SelectedFilter) { case NOFILTERSELECTED: case GAUSS: { break; } - case NLMR: - case NLMV: + case NLM: { unsigned int currentProgressCount = m_NonLocalMeansFilter->GetCurrentVoxelCount(); mitk::ProgressBar::GetInstance()->Progress(currentProgressCount-m_LastProgressCount); m_LastProgressCount = currentProgressCount; break; } } } diff --git a/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkDenoisingView.h b/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkDenoisingView.h index d804268379..a04b52ace5 100644 --- a/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkDenoisingView.h +++ b/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkDenoisingView.h @@ -1,131 +1,130 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef _QMITKQmitkDenoisingView_H_INCLUDED #define _QMITKQmitkDenoisingView_H_INCLUDED #include #include #include "ui_QmitkDenoisingViewControls.h" #include #include #include #include #include #include #include #include #include class QmitkDenoisingView; class QmitkDenoisingWorker : public QObject { Q_OBJECT public: QmitkDenoisingWorker(QmitkDenoisingView* view); public slots: void run(); private: QmitkDenoisingView* m_View; }; /*! \brief View displaying details to denoise diffusionweighted images. \sa QmitkFunctionality \ingroup Functionalities */ class QmitkDenoisingView : public QmitkFunctionality { // this is needed for all Qt objects that should have a Qt meta-object // (everything that derives from QObject and wants to have signal/slots) Q_OBJECT public: static const std::string VIEW_ID; QmitkDenoisingView(); virtual ~QmitkDenoisingView(); /** Typedefs */ typedef short DiffusionPixelType; typedef mitk::DiffusionImage< DiffusionPixelType > DiffusionImageType; typedef mitk::Image MaskImageType; typedef itk::NonLocalMeansDenoisingFilter< DiffusionPixelType > NonLocalMeansDenoisingFilterType; typedef itk::DiscreteGaussianImageFilter < itk::Image< DiffusionPixelType, 3>, itk::Image< DiffusionPixelType, 3> > GaussianFilterType; typedef itk::VectorImageToImageFilter < DiffusionPixelType > ExtractFilterType; typedef itk::ComposeImageFilter < itk::Image > ComposeFilterType; virtual void CreateQtPartControl(QWidget *parent); /// \brief Creation of the connections of main and control widget virtual void CreateConnections(); /// \brief Creation of the connections of the FilterComboBox virtual void Activated(); protected slots: void StartDenoising(); ///< prepares filter condition and starts thread for denoising void SelectFilter(int filter); ///< updates which filter is selected void BeforeThread(); ///< starts timer & disables all buttons while denoising void AfterThread(); ///< stops timer & creates a new datanode of the denoised image void UpdateProgress(); ///< updates the progressbar each timestep private: /// \brief called by QmitkFunctionality when DataManager's selection has changed virtual void OnSelectionChanged( std::vector nodes ); void ResetParameterPanel(); Ui::QmitkDenoisingViewControls* m_Controls; mitk::DataNode::Pointer m_ImageNode; mitk::DataNode::Pointer m_BrainMaskNode; QmitkDenoisingWorker m_DenoisingWorker; QThread m_DenoisingThread; bool m_ThreadIsRunning; bool m_NoExceptionThrown; NonLocalMeansDenoisingFilterType::Pointer m_NonLocalMeansFilter; GaussianFilterType::Pointer m_GaussianFilter; DiffusionImageType::Pointer m_InputImage; MaskImageType::Pointer m_ImageMask; QTimer* m_DenoisingTimer; unsigned int m_LastProgressCount; unsigned int m_MaxProgressCount; enum FilterType { NOFILTERSELECTED, - NLMR, - NLMV, + NLM, GAUSS }m_SelectedFilter; friend class QmitkDenoisingWorker; }; #endif // _QmitkDenoisingView_H_INCLUDED diff --git a/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkDenoisingViewControls.ui b/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkDenoisingViewControls.ui index a6d8bf918b..7dda469626 100644 --- a/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkDenoisingViewControls.ui +++ b/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkDenoisingViewControls.ui @@ -1,191 +1,237 @@ QmitkDenoisingViewControls 0 0 351 734 0 0 QmitkTemplate 6 - + + 9 + + + 9 + + + 9 + + 9 Denoisingfilter - + + 9 + + + 9 + + + 9 + + 9 Input Data DWI: <html><head/><body><p><span style=" color:#ff0000;">mandatory</span></p></body></html> true Brainmask: - <html><head/><body><p><span style=" color:#ff0000;">mandatory</span></p></body></html> + <html><head/><body><p><span style=" color:#ff0000;">optional</span></p></body></html> Parameters 1 3 1 7 Parameter 3 Parameter 1 - + Qt::Horizontal 40 20 Parameter 2 - + Apply - + Qt::Vertical 20 40 + + + + Use rician adaption: + + + + + + + Use joint information: + + + + + + + + + + + + + + + + + - + Qt::Vertical QSizePolicy::Expanding 20 220