ITK/Examples/Developer/OilPaintingImageFilter

From KitwarePublic
< ITK‎ | Examples
Revision as of 14:24, 7 June 2011 by Dzenanz (talk | contribs) (Oil painting image filter)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

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 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;
   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 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(ImageFilter)

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

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

endif()

add_executable(ImageFilter MACOSX_BUNDLE ImageFilter.cxx)

if( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(ImageFilter ITKReview ${ITK_LIBRARIES})

else( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(ImageFilter ${ITK_LIBRARIES})

endif( "${ITK_VERSION_MAJOR}" LESS 4 )

</syntaxhighlight>

Download and Build ImageFilter

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

cd ImageFilter/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:

./ImageFilter

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.