diff --git a/Modules/DiffusionImaging/DiffusionCore/Algorithms/itkNonLocalMeansDenoisingFilter.cpp b/Modules/DiffusionImaging/DiffusionCore/Algorithms/itkNonLocalMeansDenoisingFilter.cpp index b6218c8035..d27fb52192 100644 --- a/Modules/DiffusionImaging/DiffusionCore/Algorithms/itkNonLocalMeansDenoisingFilter.cpp +++ b/Modules/DiffusionImaging/DiffusionCore/Algorithms/itkNonLocalMeansDenoisingFilter.cpp @@ -1,139 +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 __itkNonLocalMeansDenoisingFilter_txx #define __itkNonLocalMeansDenoisingFilter_txx #include #include #include #define _USE_MATH_DEFINES #include #include "itkImageRegionConstIterator.h" #include "itkImageRegionConstIteratorWithIndex.h" #include "itkImageRegionIterator.h" #include "itkNeighborhoodIterator.h" + namespace itk { template< class TInPixelType, class TOutPixelType > NonLocalMeansDenoisingFilter< TInPixelType, TOutPixelType> ::NonLocalMeansDenoisingFilter() { this->SetNumberOfRequiredInputs( 1 ); } template< class TInPixelType, class TOutPixelType > void NonLocalMeansDenoisingFilter< TInPixelType, TOutPixelType> ::BeforeThreadedGenerateData() { typename OutputImageType::Pointer outputImage = static_cast< OutputImageType * >(this->ProcessObject::GetOutput(0)); - outputImage->FillBuffer(0.0); +// outputImage->FillBuffer(0.0); } template< class TInPixelType, class TOutPixelType > void NonLocalMeansDenoisingFilter< TInPixelType, TOutPixelType> ::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, ThreadIdType ) { typename OutputImageType::Pointer outputImage = static_cast< OutputImageType * >(this->ProcessObject::GetOutput(0)); ImageRegionIterator< OutputImageType > oit(outputImage, outputRegionForThread); oit.GoToBegin(); typedef ConstNeighborhoodIterator InputIteratorType; typename InputImageType::Pointer inputImagePointer = NULL; inputImagePointer = static_cast< InputImageType * >( this->ProcessObject::GetInput(0) ); InputIteratorType git(m_V_Radius, inputImagePointer, outputRegionForThread ); + InputIteratorType njit(m_N_Radius, inputImagePointer, outputRegionForThread ); + InputIteratorType niit(m_N_Radius, inputImagePointer, outputRegionForThread ); git.GoToBegin(); while( !git.IsAtEnd() ) { - typename TInPixelType xi = git.GetCenterPixel(); - for (int i=0; i < git.Size(); ++i) + TInPixelType xi = git.GetCenterPixel()[0]; + double sumj = 0; + double wj = 0; + for (int j = 0; j < git.Size(); ++j) { - typename TInPixelType xj = git.GetPixel(i); - if (xi == xj && git.GetCenterPixel.GetIndex() != i) - { - typename ConstNeighborhoodIterator nit1(m_N_Radius, inputImagePointer, xi); - typename ConstNeighborhoodIterator nit2(m_N_Radius, inputImagePointer, xj); - for (int k = 0; ) - } - } - /*double S0 = 0; - int c = 0; - for (int i=0; iGetVectorLength(); i++) - { - GradientDirectionType g = m_GradientDirections->GetElement(i); - if (g.magnitude()<0.001) - { - S0 += pix[i]; - c++; - } - } - if (c>0) - S0 /= c; + TInPixelType xj = git.GetPixel(j)[0]; - if (S0>0) + if (xi == xj && git.Size()/2 != j) { - c = 0; - for (int i=0; iGetVectorLength(); i++) - { - GradientDirectionType g = m_GradientDirections->GetElement(i); - if (g.magnitude()>0.001) - { - double twonorm = g.two_norm(); - double b = m_B_value*twonorm*twonorm; - if (b>0) - { - double S = pix[i]; - outval -= std::log(S/S0)/b; - c++; - } - } - } - - if (c>0) - outval /= c; + niit.SetLocation(git.GetIndex()); + njit.SetLocation(git.GetIndex(j)); + double sumk = 0; + for (int k = 0; k < niit.Size(); ++k) + { + sumk += std::pow( (niit.GetPixel(k)[0] - njit.GetPixel(k)[0]), 2); + } + wj = std::exp( - ( std::sqrt( (1 / niit.Size()) * sumk) / m_H)); + sumj += wj * std::pow(xj, 2) - 2 * std::pow(m_H, 2); } - - if (outval==outval && outval<10000) - oit.Set( outval );*/ + } + TOutPixelType outval = /*dynamic_cast*/ (std::sqrt(sumj)); + oit.Set(outval); //TODO work with Gradients!!! ++oit; ++git; } std::cout << "One Thread finished calculation" << std::endl; } template< class TInPixelType, class TOutPixelType > -void -NonLocalMeansDenoisingFilter< TInPixelType, TOutPixelType> -::PrintSelf(std::ostream& os, Indent indent) const +void NonLocalMeansDenoisingFilter< TInPixelType, TOutPixelType>::PrintSelf(std::ostream& os, Indent indent) const { Superclass::PrintSelf(os,indent); } +template < class TInPixelType, class TOutPixelType > +void NonLocalMeansDenoisingFilter< TInPixelType, TOutPixelType>::SetNRadius(unsigned int n) +{ + for (int i = 0; i < InputImageType::ImageDimension; ++i) + { + m_N_Radius [i] = n; + } +} + +template < class TInPixelType, class TOutPixelType > +void NonLocalMeansDenoisingFilter< TInPixelType, TOutPixelType>::SetVRadius(unsigned int v) +{ + for (int i = 0; i < InputImageType::ImageDimension; ++i) + { + m_V_Radius [i] = v; + } +} + } #endif // __itkNonLocalMeansDenoisingFilter_txx diff --git a/Modules/DiffusionImaging/DiffusionCore/Algorithms/itkNonLocalMeansDenoisingFilter.h b/Modules/DiffusionImaging/DiffusionCore/Algorithms/itkNonLocalMeansDenoisingFilter.h index 2026548f7f..a330a33b82 100644 --- a/Modules/DiffusionImaging/DiffusionCore/Algorithms/itkNonLocalMeansDenoisingFilter.h +++ b/Modules/DiffusionImaging/DiffusionCore/Algorithms/itkNonLocalMeansDenoisingFilter.h @@ -1,82 +1,87 @@ /*=================================================================== 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. ===================================================================*/ /*=================================================================== This file is based heavily on a corresponding ITK filter. ===================================================================*/ #ifndef __itkNonLocalMeansDenoisingFilter_h_ #define __itkNonLocalMeansDenoisingFilter_h_ #include "itkImageToImageFilter.h" #include "itkVectorImage.h" #include #include namespace itk{ /** \class NonLocalMeansDenoisingFilter */ template< class TInPixelType, class TOutPixelType > class NonLocalMeansDenoisingFilter : public ImageToImageFilter< VectorImage < TInPixelType, 3 >, VectorImage < TOutPixelType, 3 > > { public: typedef NonLocalMeansDenoisingFilter Self; typedef SmartPointer Pointer; typedef SmartPointer ConstPointer; typedef ImageToImageFilter< VectorImage < TInPixelType, 3 >, VectorImage < TOutPixelType, 3 > > Superclass; typedef mitk::DiffusionImage< short >::GradientDirectionType GradientDirectionType; typedef mitk::DiffusionImage< short >::GradientDirectionContainerType::Pointer GradientContainerType; /** Method for creation through the object factory. */ itkNewMacro(Self) /** Runtime information support. */ itkTypeMacro(NonLocalMeansDenoisingFilter, ImageToImageFilter) typedef typename Superclass::InputImageType InputImageType; typedef typename Superclass::OutputImageType OutputImageType; typedef typename Superclass::OutputImageRegionType OutputImageRegionType; itkSetMacro( B_value, double ) itkSetMacro( GradientDirections, GradientContainerType ) + itkSetMacro( H, unsigned int) + void SetNRadius(unsigned int n); + void SetVRadius(unsigned int v); + protected: NonLocalMeansDenoisingFilter(); ~NonLocalMeansDenoisingFilter() {} void PrintSelf(std::ostream& os, Indent indent) const; void BeforeThreadedGenerateData(); void ThreadedGenerateData( const OutputImageRegionType &outputRegionForThread, ThreadIdType); double m_B_value; GradientContainerType m_GradientDirections; typename NeighborhoodIterator < VectorImage < TInPixelType, 3 > >::RadiusType m_V_Radius; typename NeighborhoodIterator < VectorImage < TInPixelType, 3 > >::RadiusType m_N_Radius; + unsigned int m_H; }; } #ifndef ITK_MANUAL_INSTANTIATION #include "itkNonLocalMeansDenoisingFilter.cpp" #endif #endif //__itkNonLocalMeansDenoisingFilter_h_ 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 0f269a2928..4e532637c1 100644 --- a/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkDenoisingView.cpp +++ b/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkDenoisingView.cpp @@ -1,115 +1,119 @@ /*=================================================================== 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 #include #include #include #include #include #include #include #include #include #include #include const std::string QmitkDenoisingView::VIEW_ID = "org.mitk.views.denoisingview"; QmitkDenoisingView::QmitkDenoisingView() : QmitkFunctionality() , m_Controls( 0 ) , m_ImageNode(NULL) { } QmitkDenoisingView::~QmitkDenoisingView() { } 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 ); this->CreateConnections(); } } void QmitkDenoisingView::CreateConnections() { if ( m_Controls ) { connect( (QObject*)(m_Controls->m_ApplyButton), SIGNAL(clicked()), this, SLOT(StartDenoising())); } } void QmitkDenoisingView::OnSelectionChanged( std::vector nodes ) { if (m_ImageNode.IsNotNull()) m_ImageNode->RemoveObserver( m_PropertyObserverTag ); m_Controls->m_InputData->setTitle("Please Select Input Data"); m_Controls->m_InputImageLabel->setText("mandatory"); m_ImageNode = 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; } } if (m_ImageNode.IsNotNull()) { itk::ReceptorMemberCommand::Pointer command = itk::ReceptorMemberCommand::New(); m_PropertyObserverTag = m_ImageNode->AddObserver( itk::ModifiedEvent(), command ); m_Controls->m_InputData->setTitle("Input Data"); } } void QmitkDenoisingView::StartDenoising() { if (m_ImageNode.IsNotNull()) { MITK_INFO << "JOPP"; + DiffusionImageType::Pointer inImage = dynamic_cast (m_ImageNode->GetData()); typedef itk::NonLocalMeansDenoisingFilter NonLocalMeansDenoisingFilterType; NonLocalMeansDenoisingFilterType::Pointer denoisingFilter = NonLocalMeansDenoisingFilterType::New(); - denoisingFilter->SetVRadius((unsigned int)m_Controls->m_Parameter1); - denoisingFilter->SetNRadius((unsigned int)m_Controls->m_Parameter2); + denoisingFilter->SetNumberOfThreads(1); + denoisingFilter->SetInput(inImage->GetVectorImage()); + denoisingFilter->SetVRadius((unsigned int)m_Controls->m_Parameter1->value()); + denoisingFilter->SetNRadius((unsigned int)m_Controls->m_Parameter2->value()); + denoisingFilter->SetH((unsigned int)m_Controls->m_Parameter3->value()); denoisingFilter->Update(); } } 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 372fa09d59..59d571380e 100644 --- a/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkDenoisingViewControls.ui +++ b/Plugins/org.mitk.gui.qt.diffusionimaging/src/internal/QmitkDenoisingViewControls.ui @@ -1,144 +1,156 @@ QmitkDenoisingViewControls 0 0 351 734 0 0 QmitkTemplate 6 9 9 9 9 Please Select Input Data DWI: <html><head/><body><p><span style=" color:#ff0000;">mandatory</span></p></body></html> true Parameters Apply Parameter 2 Qt::Horizontal 40 20 Parameter 1 Parameter 3 - + + + 1 + + - + + + 1 + + - + + + 1 + + Qt::Vertical QSizePolicy::Expanding 20 220