[Insight-users] creating my first finding max value in neighborhood

john smith mkitkinsightuser at gmail.com
Mon Feb 28 10:22:08 EST 2011


Hi to all,

 I am trying to develop my first filter in itk using visualstudio2010 and
cmake. This filter devides my input image into neighborhoods of nine pixels
and finds their maximum value. Then, this value is transfered to one pixel
of my output image. I have written the following code, but I have some
failures that I cannot understand. Could someone help me to overide my
problems? Furthermore, I would like to know if it is possible to write my
own filter only with .cxx and .txt files, like*
http://www.itk.org/Wiki/ITK/Examples/Iterators/ConstNeighborhoodIterator*.

Thanks


---------------------------max_value_neighborhood.cxx-----------------------------
--------------------------------------------------------------------------------------------------
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"

#include "max_value_filter.h"

int main(int, char*[])
{
  // Setup types
  typedef itk::Image<unsigned char, 2>   ImageType;
  typedef itk::max_value_filter<ImageType>  FilterType;

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

  // Create and the filter
  FilterType::Pointer filter = FilterType::New();
  filter->SetInput(reader->GetOutput());
  filter->Update();

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

  return EXIT_SUCCESS;
}


------------------------------------------max_value_filter.h-----------------------
--------------------------------------------------------------------------------------
#ifndef __itkmax_value_filter_h
#define __itkmax_value_filter_h

#include "itkImageToImageFilter.h"

namespace itk
{
template< class TImage>
class max_value_filter:public ImageToImageFilter< TImage, TImage >
{
public:
  /** Standard class typedefs. */
  typedef max_value_filter             Self;
  typedef ImageToImageFilter< TImage, TImage > Superclass;
  typedef SmartPointer< Self >        Pointer;

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

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

protected:
  max_value_filter(){}
  ~max_value_filter(){}

  /** Does the real work. */
  virtual void GenerateData();

private:
  max_value_filter(const Self &); //purposely not implemented
  void operator=(const Self &);  //purposely not implemented

};
} //namespace ITK


#ifndef ITK_MANUAL_INSTANTIATION
#include "max_value_filter.txx"
#endif


#endif // __max_value_filter_h


----------------------------max_value_filter.txx--------------------------------
-------------------------------------------------------------------------------------
#ifndef __itkmax_value_filter_txx
#define __itkmax_value_filter_txx

#include "max_value_filter.h"
#include "itkObjectFactory.h"
#include "itkImageRegionIterator.h"
#include "itkConstNeighborhoodIterator.h"

namespace itk
{

template< class TImage>
void max_value_filter< TImage>
::GenerateData()
{
  typename TImage::ConstPointer input = this->GetInput();

  typename TImage::Pointer output = this->GetOutput();
  output->SetRegions(input->GetLargestPossibleRegion());
  output->Allocate();

  itk::ImageRegionIterator<TImage> outputIterator(output,
output->GetLargestPossibleRegion());
  itk::ImageRegionConstIterator<TImage> inputIterator(input,
input->GetLargestPossibleRegion());




  while(!inputIterator.IsAtEnd())
    {

TImage::IndexType max = inputIterator.GetIndex(0);

    for(unsigned int i = 1; i < 9; i++)
      {
      TImage::IndexType index = inputIterator.GetIndex(i);


        if (max<index)
                      {
                        max =
inputIterator.GetIndex(i);
                      }


      bool IsInBounds;
      inputIterator.GetPixel(i, IsInBounds);

      }

    outputIterator.Set(inputIterator.Get(max));

    ++inputIterator;
    ++outputIterator;
    }


}

}// end namespace


#endif



----------------------CMakeLists.txt------------------------------
------------------------------------------------------------------------
cmake_minimum_required(VERSION 2.6)

PROJECT(max_value_filter)

FIND_PACKAGE(ITK REQUIRED)
INCLUDE(${ITK_USE_FILE})

ADD_EXECUTABLE(max_value_filter max_value_neighborhood.cxx)
TARGET_LINK_LIBRARIES(max_value_filter ITKBasicFilters ITKIO ITKCommon)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20110228/c07d9510/attachment.htm>


More information about the Insight-users mailing list