[Insight-users] creating my first finding max value in neighborhood
robert tamburo
robert.tamburo at gmail.com
Mon Feb 28 10:39:52 EST 2011
It would be helpful to see the error that you are receiving. The below
portion of code looks odd to me because you're comparing indices:
if (max<index)
{
max =
inputIterator.GetIndex(i);
}
I previously wrote a maximum image filter by modifying the mean image
filter. It has the bells and whistles of an image filter like checking
boundary conditions, etc. I attached the code for you to examine for your
development.
On Mon, Feb 28, 2011 at 10:22 AM, john smith <mkitkinsightuser at gmail.com>wrote:
> 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)
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.html
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20110228/73ed7ef6/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: itkNewMaximumImageFilter.h
Type: text/x-chdr
Size: 5052 bytes
Desc: not available
URL: <http://www.itk.org/pipermail/insight-users/attachments/20110228/73ed7ef6/attachment.h>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: itkNewMaximumImageFilter.txx
Type: application/octet-stream
Size: 5411 bytes
Desc: not available
URL: <http://www.itk.org/pipermail/insight-users/attachments/20110228/73ed7ef6/attachment.obj>
More information about the Insight-users
mailing list