ITK/Examples/Developer/OilPaintingImageFilter: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
(correct file name)
(added a few typename keywords, reduces the number of errors with GCC)
Line 39: Line 39:
#ifndef __itkOilPaintingImageFilter_h
#ifndef __itkOilPaintingImageFilter_h
#define __itkOilPaintingImageFilter_h
#define __itkOilPaintingImageFilter_h
 
#include "itkImageToImageFilter.h"
#include "itkImageToImageFilter.h"
#include "itkNeighborhoodIterator.h"
#include "itkNeighborhoodIterator.h"
 
namespace itk
namespace itk
{
{
Line 62: Line 62:
   typedef SmartPointer< Self >        Pointer;
   typedef SmartPointer< Self >        Pointer;
   typedef typename NeighborhoodIterator<TImage>::RadiusType RadiusType;
   typedef typename NeighborhoodIterator<TImage>::RadiusType RadiusType;
 
   /** Method for creation through the object factory. */
   /** Method for creation through the object factory. */
   itkNewMacro(Self);
   itkNewMacro(Self);
 
   /** Run-time type information (and related methods). */
   /** Run-time type information (and related methods). */
   itkTypeMacro(OilPaintingImageFilter, ImageToImageFilter);
   itkTypeMacro(OilPaintingImageFilter, ImageToImageFilter);
 
   itkSetMacro(NumberOfBins, unsigned int);
   itkSetMacro(NumberOfBins, unsigned int);
   itkGetConstMacro(NumberOfBins, const unsigned int);
   itkGetConstMacro(NumberOfBins, const unsigned int);
 
   itkSetMacro(Radius, RadiusType);
   itkSetMacro(Radius, RadiusType);
   itkGetConstMacro(Radius, const RadiusType);
   itkGetConstMacro(Radius, const RadiusType);
   void SetRadius(unsigned int radius);
   void SetRadius(unsigned int radius);
 
protected:
protected:
   OilPaintingImageFilter();
   OilPaintingImageFilter();
   ~OilPaintingImageFilter(){}
   ~OilPaintingImageFilter(){}
 
   virtual void BeforeThreadedGenerateData();
   virtual void BeforeThreadedGenerateData();
 
   /** Does the real work. */
   /** Does the real work. */
   virtual void ThreadedGenerateData(const OutputImageRegionType & outputRegionForThread, int threadId);
   virtual void ThreadedGenerateData(const typename OutputImageRegionType& outputRegionForThread, int threadId);
 
private:
private:
   OilPaintingImageFilter(const Self &); //purposely not implemented
   OilPaintingImageFilter(const Self &); //purposely not implemented
   void operator=(const Self &);  //purposely not implemented
   void operator=(const Self &);  //purposely not implemented
 
   typename TImage::ValueType m_Maximum, m_Minimum;
   typename TImage::ValueType m_Maximum, m_Minimum;
   typename NeighborhoodIterator<TImage>::RadiusType m_Radius;
   typename NeighborhoodIterator<TImage>::RadiusType m_Radius;
Line 94: Line 94:
};
};
} //namespace ITK
} //namespace ITK
 
 
#ifndef ITK_MANUAL_INSTANTIATION
#ifndef ITK_MANUAL_INSTANTIATION
#include "itkOilPaintingImageFilter.txx"
#include "itkOilPaintingImageFilter.txx"
#endif
#endif
 
#endif // __itkOilPaintingImageFilter_h
#endif // __itkOilPaintingImageFilter_h
</source>
</source>
Line 107: Line 107:
#ifndef __itkOilPaintingImageFilter_txx
#ifndef __itkOilPaintingImageFilter_txx
#define __itkOilPaintingImageFilter_txx
#define __itkOilPaintingImageFilter_txx
 
#include "itkOilPaintingImageFilter.h"
#include "itkOilPaintingImageFilter.h"
#include "itkObjectFactory.h"
#include "itkObjectFactory.h"
Line 114: Line 114:
#include "itkNeighborhoodIterator.h"
#include "itkNeighborhoodIterator.h"
#include "itkMinimumMaximumImageCalculator.h"
#include "itkMinimumMaximumImageCalculator.h"
 
namespace itk
namespace itk
{
{
Line 123: Line 123:
     SetRadius(5);
     SetRadius(5);
}
}
 
template<class TImage>
template<class TImage>
void OilPaintingImageFilter<TImage>::SetRadius(unsigned int radius)
void OilPaintingImageFilter<TImage>::SetRadius(unsigned int radius)
Line 130: Line 130:
       m_Radius[i] = radius;
       m_Radius[i] = radius;
}
}
 
template<class TImage>
template<class TImage>
void OilPaintingImageFilter<TImage>::BeforeThreadedGenerateData()
void OilPaintingImageFilter<TImage>::BeforeThreadedGenerateData()
{
{
     typedef itk::MinimumMaximumImageCalculator< TImage >  CalculatorType;
     typedef itk::MinimumMaximumImageCalculator< TImage >  CalculatorType;
     CalculatorType::Pointer calculatorI = CalculatorType::New();
     typename CalculatorType::Pointer calculatorI = CalculatorType::New();
     calculatorI->SetImage( this->GetInput() );
     calculatorI->SetImage( this->GetInput() );
     calculatorI->Compute();
     calculatorI->Compute();
Line 141: Line 141:
     m_Minimum = calculatorI->GetMinimum();
     m_Minimum = calculatorI->GetMinimum();
}
}
 
template<class TImage>
template<class TImage>
void OilPaintingImageFilter<TImage>
void OilPaintingImageFilter<TImage>
::ThreadedGenerateData(const OutputImageRegionType & outputRegionForThread, int threadId)
::ThreadedGenerateData(const typename OutputImageRegionType& outputRegionForThread, int threadId)
{
{
   typename TImage::ConstPointer input = this->GetInput();
   typename TImage::ConstPointer input = this->GetInput();
   typename TImage::Pointer output = this->GetOutput();
   typename TImage::Pointer output = this->GetOutput();
 
   itk::ImageRegionIterator<TImage> out(output, outputRegionForThread);
   itk::ImageRegionIterator<TImage> out(output, outputRegionForThread);
   itk::ConstNeighborhoodIterator<TImage> it(m_Radius, input, outputRegionForThread);
   itk::ConstNeighborhoodIterator<TImage> it(m_Radius, input, outputRegionForThread);
 
   unsigned long long *bins=new unsigned long long[m_NumberOfBins];
   unsigned long long *bins=new unsigned long long[m_NumberOfBins];
 
   while(!out.IsAtEnd())
   while(!out.IsAtEnd())
   {
   {
     for (unsigned int i=0; i<m_NumberOfBins; i++)
     for (unsigned int i=0; i<m_NumberOfBins; i++)
       bins[i]=0; //reset histogram
       bins[i]=0; //reset histogram
 
     //create histogram of values within the radius
     //create histogram of values within the radius
     for (unsigned int i = 0; i < it.Size(); ++i)
     for (unsigned int i = 0; i < it.Size(); ++i)
Line 167: Line 167:
         //without -0.001, when val=max then we would go over the upper bound (index m_NumberOfBins)
         //without -0.001, when val=max then we would go over the upper bound (index m_NumberOfBins)
     }
     }
 
     //find the peak
     //find the peak
     unsigned maxIndex=0;
     unsigned maxIndex=0;
Line 177: Line 177:
             maxIndex=i;
             maxIndex=i;
         }
         }
 
     //set middle value of a bin's range as intensity
     //set middle value of a bin's range as intensity
     out.Set((maxIndex+0.5)*(m_Maximum-m_Minimum)/m_NumberOfBins);
     out.Set((maxIndex+0.5)*(m_Maximum-m_Minimum)/m_NumberOfBins);
 
     ++it;
     ++it;
     ++out;
     ++out;
   }
   }
 
   delete bins; //dealloc array
   delete bins; //dealloc array
}
}
 
}// end namespace
}// end namespace
 
#endif //__itkOilPaintingImageFilter_txx
#endif //__itkOilPaintingImageFilter_txx
</source>
</source>


{{ITKCMakeLists|ImageFilterExample}}
{{ITKCMakeLists|ImageFilterExample}}

Revision as of 15:54, 1 August 2011

This example demonstrates a simple multi-threaded filter, creating the effect of oil painting. You can also use this class as-is (copy .h and .txx files into your project and use them).

ImageFilterExample.cxx

<source lang="cpp">

  1. include "itkImage.h"
  2. include "itkImageFileReader.h"
  3. include "itkImageFileWriter.h"
  1. include "itkOilPaintingImageFilter.h"

int main(int, char*[]) {

 typedef itk::Image<unsigned char, 2>   ImageType;
 typedef itk::OilPaintingImageFilter<ImageType>  FilterType;

 typedef itk::ImageFileReader<ImageType> ReaderType;
 ReaderType::Pointer reader = ReaderType::New();
 reader->SetFileName("LenaGrayscale.jpg");
 reader->Update();

 FilterType::Pointer filter = FilterType::New();
 filter->SetInput(reader->GetOutput());
 filter->SetNumberOfBins(50);
 filter->SetRadius(2);
 filter->Update();

 typedef  itk::ImageFileWriter< ImageType  > WriterType;
 WriterType::Pointer writer = WriterType::New();
 writer->SetFileName("LenaOil.jpg");
 writer->SetInput(filter->GetOutput());
 writer->Update();

 return EXIT_SUCCESS;

} </source>

itkOilPaintingImageFilter.h

<source lang="cpp">

  1. ifndef __itkOilPaintingImageFilter_h
  2. define __itkOilPaintingImageFilter_h
  1. include "itkImageToImageFilter.h"
  2. include "itkNeighborhoodIterator.h"

namespace itk { /** \class OilPaintingImageFilter

* \brief Implements oil painting artistic image filter.
*
* Default number of bins is 20.
* Default radius is 5, meaning a window of 11x11x11 for 3D images.
*
* \ingroup ImageFilters
*/

template< class TImage> class OilPaintingImageFilter:public ImageToImageFilter< TImage, TImage > { public:

 /** Standard class typedefs. */
 typedef OilPaintingImageFilter             Self;
 typedef ImageToImageFilter< TImage, TImage > Superclass;
 typedef SmartPointer< Self >        Pointer;
 typedef typename NeighborhoodIterator<TImage>::RadiusType RadiusType;

 /** Method for creation through the object factory. */
 itkNewMacro(Self);

 /** Run-time type information (and related methods). */
 itkTypeMacro(OilPaintingImageFilter, ImageToImageFilter);

 itkSetMacro(NumberOfBins, unsigned int);
 itkGetConstMacro(NumberOfBins, const unsigned int);

 itkSetMacro(Radius, RadiusType);
 itkGetConstMacro(Radius, const RadiusType);
 void SetRadius(unsigned int radius);

protected:

 OilPaintingImageFilter();
 ~OilPaintingImageFilter(){}

 virtual void BeforeThreadedGenerateData();

 /** Does the real work. */
 virtual void ThreadedGenerateData(const typename OutputImageRegionType& outputRegionForThread, int threadId);

private:

 OilPaintingImageFilter(const Self &); //purposely not implemented
 void operator=(const Self &);  //purposely not implemented

 typename TImage::ValueType m_Maximum, m_Minimum;
 typename NeighborhoodIterator<TImage>::RadiusType m_Radius;
 unsigned int m_NumberOfBins;

}; } //namespace ITK


  1. ifndef ITK_MANUAL_INSTANTIATION
  2. include "itkOilPaintingImageFilter.txx"
  3. endif
  1. endif // __itkOilPaintingImageFilter_h

</source>

itkOilPaintingImageFilter.txx

<source lang="cpp">

  1. ifndef __itkOilPaintingImageFilter_txx
  2. define __itkOilPaintingImageFilter_txx
  1. include "itkOilPaintingImageFilter.h"
  2. include "itkObjectFactory.h"
  3. include "itkImageRegionIterator.h"
  4. include "itkImageRegionConstIterator.h"
  5. include "itkNeighborhoodIterator.h"
  6. include "itkMinimumMaximumImageCalculator.h"

namespace itk { template<class TImage> OilPaintingImageFilter<TImage>::OilPaintingImageFilter() {

   m_NumberOfBins=20;
   SetRadius(5);

}

template<class TImage> void OilPaintingImageFilter<TImage>::SetRadius(unsigned int radius) {

   for (unsigned int i = 0; i < TImage::ImageDimension; ++i)
     m_Radius[i] = radius;

}

template<class TImage> void OilPaintingImageFilter<TImage>::BeforeThreadedGenerateData() {

   typedef itk::MinimumMaximumImageCalculator< TImage >  CalculatorType;
   typename CalculatorType::Pointer calculatorI = CalculatorType::New();
   calculatorI->SetImage( this->GetInput() );
   calculatorI->Compute();
   m_Maximum = calculatorI->GetMaximum();
   m_Minimum = calculatorI->GetMinimum();

}

template<class TImage> void OilPaintingImageFilter<TImage>

ThreadedGenerateData(const typename OutputImageRegionType& outputRegionForThread, int threadId)

{

 typename TImage::ConstPointer input = this->GetInput();
 typename TImage::Pointer output = this->GetOutput();

 itk::ImageRegionIterator<TImage> out(output, outputRegionForThread);
 itk::ConstNeighborhoodIterator<TImage> it(m_Radius, input, outputRegionForThread);

 unsigned long long *bins=new unsigned long long[m_NumberOfBins];

 while(!out.IsAtEnd())
 {
   for (unsigned int i=0; i<m_NumberOfBins; i++)
     bins[i]=0; //reset histogram

   //create histogram of values within the radius
   for (unsigned int i = 0; i < it.Size(); ++i)
   {
       TImage::ValueType val=it.GetPixel(i);
       bins[int((m_NumberOfBins-0.001)*(val-m_Minimum)/(m_Maximum-m_Minimum))]++;
       //casting to int rounds towards zero
       //without -0.001, when val=max then we would go over the upper bound (index m_NumberOfBins)
   }

   //find the peak
   unsigned maxIndex=0;
   unsigned long long maxBin=bins[0];
   for (unsigned int i=1; i<m_NumberOfBins; i++)
       if (bins[i]>maxBin)
       {
           maxBin=bins[i];
           maxIndex=i;
       }

   //set middle value of a bin's range as intensity
   out.Set((maxIndex+0.5)*(m_Maximum-m_Minimum)/m_NumberOfBins);

   ++it;
   ++out;
 }

 delete bins; //dealloc array

}

}// end namespace

  1. endif //__itkOilPaintingImageFilter_txx

</source>

CMakeLists.txt

<syntaxhighlight lang="cmake"> cmake_minimum_required(VERSION 3.9.5)

project(ImageFilterExample)

find_package(ITK REQUIRED) include(${ITK_USE_FILE}) if (ITKVtkGlue_LOADED)

 find_package(VTK REQUIRED)
 include(${VTK_USE_FILE})

endif()

add_executable(ImageFilterExample MACOSX_BUNDLE ImageFilterExample.cxx)

if( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(ImageFilterExample ITKReview ${ITK_LIBRARIES})

else( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(ImageFilterExample ${ITK_LIBRARIES})

endif( "${ITK_VERSION_MAJOR}" LESS 4 )

</syntaxhighlight>

Download and Build ImageFilterExample

Click here to download ImageFilterExample and its CMakeLists.txt file. Once the tarball ImageFilterExample.tar has been downloaded and extracted,

cd ImageFilterExample/build
  • If ITK is installed:
cmake ..
  • If ITK is not installed but compiled on your system, you will need to specify the path to your ITK build:
cmake -DITK_DIR:PATH=/home/me/itk_build ..

Build the project:

make

and run it:

./ImageFilterExample

WINDOWS USERS PLEASE NOTE: Be sure to add the ITK bin directory to your path. This will resolve the ITK dll's at run time.

Building All of the Examples

Many of the examples in the ITK Wiki Examples Collection require VTK. You can build all of the the examples by following these instructions. If you are a new VTK user, you may want to try the Superbuild which will build a proper ITK and VTK.

ItkVtkGlue

ITK >= 4

For examples that use QuickView (which depends on VTK), you must have built ITK with Module_ITKVtkGlue=ON.

ITK < 4

Some of the ITK Examples require VTK to display the images. If you download the entire ITK Wiki Examples Collection, the ItkVtkGlue directory will be included and configured. If you wish to just build a few examples, then you will need to download ItkVtkGlue and build it. When you run cmake it will ask you to specify the location of the ItkVtkGlue binary directory.