08 November 2011

The Problem with C++

The typedefs say it all; I’ll stick with Objective-C please. Presented without comment.

#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#include "itkSize.h"
#include "itkIndex.h"
#include "itkImage.h"
#include "itkImageRegionIterator.h"
#include "itkPoint.h"
#include "itkSphereSpatialFunction.h"
#include "itkFloodFilledSpatialFunctionConditionalIterator.h"
#include "itkPNGImageIO.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkCannyEdgeDetectionImageFilter.h"
#include <fstream>
#include <iostream>



int main () {


const unsigned int Dimension = 2; 
typedef unsigned char PixelType;
typedef itk::Image< PixelType, Dimension > ImageType;
typedef itk::ImageRegionIterator< ImageType> IteratorType;

ImageType::Pointer image = ImageType::New();

ImageType::SizeType size;
size[0] = 256;
size[1] = 256;

ImageType::IndexType start;
start[0] = 0;
start[1] = 0;

ImageType::RegionType region;
region.SetSize(size);
region.SetIndex(start);

image->SetRegions(region);
image->Allocate();

double spacing[ImageType::ImageDimension];
spacing[0] = .5;
spacing[1] = .5;
image->SetSpacing(spacing);

double origin[ImageType::ImageDimension];
origin[0]=0;
origin[1]=0;
image->SetOrigin(origin);

ImageType::PixelType pixelValue='X';
for(int i=0;i<256;i++) {
    for(int j=0;j<256;j++) {
        ImageType::IndexType pixelIndex;
        pixelIndex[0]=i;
        pixelIndex[1]=j;
        image->SetPixel(pixelIndex,pixelValue);
    }
}

IteratorType it(image,image->GetRequestedRegion() );

for( it = it.Begin();!it.IsAtEnd();++it) {
    it.Set('O');
}

  for(int strat = 0; strat < 4; strat++)
    {

    typedef itk::SphereSpatialFunction<2> FunctionType;
    typedef FunctionType::InputType FunctionPositionType;

    FunctionType::Pointer spatialFunc = FunctionType::New();
    spatialFunc->SetRadius( 25.0 );

    FunctionPositionType center;
    center[0] = 128;
    center[1] = 128;
    spatialFunc->SetCenter(center);

    ImageType::IndexType seedPos;
    const ImageType::IndexValueType pos[] = {2,2};
    seedPos.SetIndex(pos);

    typedef itk::FloodFilledSpatialFunctionConditionalIterator
      <ImageType, FunctionType> ItType;

    ItType sfi = ItType(image, spatialFunc, seedPos);

    switch(strat){
    case 0:
      {
        sfi.SetOriginInclusionStrategy();
      }
    break;
    case 1:
      {
        sfi.SetCenterInclusionStrategy();
      }
    break;
    case 2:
      {
        sfi.SetCompleteInclusionStrategy();
      }
    break;
    case 3:
      {
        sfi.SetIntersectInclusionStrategy();
      }
    } 

    for( sfi.GoToBegin(); !( sfi.IsAtEnd() ); ++sfi)
      {
      sfi.Set('c');
      }

    }   

    typedef itk::ImageFileWriter<ImageType> WriterType2D;
    WriterType2D::Pointer writer2D = WriterType2D::New();
    writer2D->SetFileName( "PartE.png" );
    writer2D->SetInput( image );
    writer2D->Write();

    typedef itk::ImageFileReader<ImageType> ReaderType;
    ReaderType::Pointer reader = ReaderType::New();

    reader->SetFileName("Tiger.png");

    ImageType::Pointer image1 = reader->GetOutput();

    typedef itk::Image<float, Dimension>    RealImageType;
     CastToRealFilterType;
    typedef itk::RescaleIntensityImageFilter<RealImageType, CharImageType > RescaleFilter;
    typedef itk::CannyEdgeDetectionImageFilter<RealImageType, RealImageType> CannyFilter;

    float variance = 2.0;
    float upperThreshold = 0.0;
    float lowerThreshold = 0.0;

    itk::CastImageFilter< ImageType, RealImageType>::Pointer toReal = CastToRealFilterType::New();
    RescaleFilter::Pointer rescale = RescaleFilter::New();
    CannyFilter::Pointer cannyFilter = CannyFilter::New();

    writer->SetFileName( "filteredImage.png" );

  rescale->SetOutputMinimum(   0 );
  rescale->SetOutputMaximum( 255 );

  toReal->SetInput( image1);

  cannyFilter->SetInput( toReal->GetOutput() );
  cannyFilter->SetVariance( variance );
  cannyFilter->SetUpperThreshold( upperThreshold );
  cannyFilter->SetLowerThreshold( lowerThreshold );

  rescale->SetInput( cannyFilter->GetOutput() );
  writer->SetInput( rescale->GetOutput() );


    writer->Write();

}

No comments:

Post a Comment