<commit_before>/**
 * There were several functions that had been copied and
 * pasted over and over again in this library.  This
 * header files is contains a common definitoin for
 * those file.
 * \author <NAME>
 */
#ifndef __antsUtilities_h__
#define __antsUtilities_h__

#include "antscout.hxx"
#include "antsAllocImage.h"
#include <string>
#include <vector>
#include <algorithm>

#include <stdlib.h>
#include <errno.h>
#include <cmath>
#include <iostream>

#include "itkVector.h"
#include "itkBinaryThresholdImageFilter.h"
#include "itkBinaryBallStructuringElement.h"
#include "itkBinaryDilateImageFilter.h"
#include "itkBinaryErodeImageFilter.h"
#include "itkGrayscaleDilateImageFilter.h"
#include "itkGrayscaleErodeImageFilter.h"

// We need to ensure that only one of these exists!
namespace ants
{
extern boost::iostreams::stream<ants_Sink> antscout;

// ##########################################################################
// ##########################################################################
// ##########################################################################
// ##########################################################################
// ##########################################################################
// ##########################################################################
// ##########################################################################
// Templates
template <class TImage>
typename TImage::Pointer  Morphological( typename TImage::Pointer input, float rad, unsigned int option,
                                         float dilateval)
{
  typedef TImage ImageType;
  enum { ImageDimension = TImage::ImageDimension };
  typedef typename TImage::PixelType PixelType;

  if( option == 0 )
    {
    antscout << " binary eroding the image " << std::endl;
    }
  else if( option == 1 )
    {
    antscout << " binary dilating the image " << std::endl;
    }
  else if( option == 2 )
    {
    antscout << " binary opening the image " << std::endl;
    }
  else if( option == 3 )
    {
    antscout << " binary closing the image " << std::endl;
    }
  else if( option == 4 )
    {
    antscout << " grayscale eroding the image " << std::endl;
    }
  else if( option == 5 )
    {
    antscout << " grayscale dilating the image " << std::endl;
    }
  else if( option == 6 )
    {
    antscout << " grayscale opening the image " << std::endl;
    }
  else if( option == 7 )
    {
    antscout << " grayscale closing the image " << std::endl;
    }

  typedef itk::BinaryBallStructuringElement<
      PixelType,
      ImageDimension>             StructuringElementType;

  typedef itk::BinaryErodeImageFilter<
      TImage,
      TImage,
      StructuringElementType>  ErodeFilterType;

  typedef itk::BinaryDilateImageFilter<
      TImage,
      TImage,
      StructuringElementType>  DilateFilterType;
  typename ErodeFilterType::Pointer  binaryErode  = ErodeFilterType::New();
  typename DilateFilterType::Pointer binaryDilate = DilateFilterType::New();

  StructuringElementType structuringElement;

  structuringElement.SetRadius( (unsigned long) rad );  // 3x3x3 structuring element

  structuringElement.CreateStructuringElement();

  binaryErode->SetKernel(  structuringElement );
  binaryDilate->SetKernel( structuringElement );
  // binaryOpen->SetKernal( structuringElement );
  // binaryClose->SetKernel( structuringElement );
  //
  // typename OpeningFilterType::Pointer  binaryOpen  = OpeningFilterType::New();
  // typename ClosingFilterType::Pointer binaryClose = ClosingFilterType::New();
  typedef itk::GrayscaleErodeImageFilter<
      TImage,
      TImage,
      StructuringElementType> GrayscaleErodeFilterType;

  typedef itk::GrayscaleDilateImageFilter<
      TImage,
      TImage,
      StructuringElementType> GrayscaleDilateFilterType;

  typename GrayscaleErodeFilterType::Pointer grayscaleErode = GrayscaleErodeFilterType::New();
  typename GrayscaleDilateFilterType::Pointer grayscaleDilate = GrayscaleDilateFilterType::New();
  grayscaleErode->SetKernel( structuringElement );
  grayscaleDilate->SetKernel( structuringElement );

  //  It is necessary to define what could be considered objects on the binary
  //  images. This is specified with the methods \code{SetErodeValue()} and
  //  \code{SetDilateValue()}. The value passed to these methods will be
  //  considered the value over which the dilation and erosion rules will apply
  binaryErode->SetErodeValue( (unsigned int ) dilateval );
  binaryDilate->SetDilateValue(  (unsigned int ) dilateval );

  typename TImage::Pointer temp;
  if( option == 1 )
    {
    antscout << " Dilate " << rad << std::endl;
    binaryDilate->SetInput( input );
    binaryDilate->Update();
    temp = binaryDilate->GetOutput();
    }
  else if( option == 0 )
    {
    antscout << " Erode " << rad << std::endl;
    binaryErode->SetInput( input );  // binaryDilate->GetOutput() );
    binaryErode->Update();
    temp = binaryErode->GetOutput();
    }
  else if( option == 2 )
    {
    // dilate(erode(img))
    antscout << " Binary Open " << rad << std::endl;
    // binaryOpen->SetInput( input );//binaryDilate->GetOutput() );
    // binaryOpen->Update();
    binaryErode->SetInput( input );
    binaryDilate->SetInput( binaryErode->GetOutput() );
    binaryDilate->Update();
    temp = binaryDilate->GetOutput();
    }
  else if( option == 3 )
    {
    antscout << " Binary Close " << rad << std::endl;
    // binaryClose->SetInput( input );//binaryDilate->GetOutput() );
    // binaryClose->Update();
    binaryDilate->SetInput( input );
    binaryErode->SetInput( binaryDilate->GetOutput() );
    binaryErode->Update();
    temp = binaryErode->GetOutput();
    }
  else if( option == 4 )
    {
    antscout << " Grayscale Erode " << rad << std::endl;
    grayscaleErode->SetInput( input ); // binaryDilate->GetOutput() );
    grayscaleErode->Update();
    temp = binaryErode->GetOutput();
    }
  else if( option == 5 )
    {
    antscout << " Grayscale Dilate " << rad << std::endl;
    grayscaleDilate->SetInput( input ); // binaryDilate->GetOutput() );
    grayscaleDilate->Update();
    temp = binaryDilate->GetOutput();<commit_msg>BUG: wrong filter being called in grayscale dilation/erosion in Morphological function<commit_after>/**
 * There were several functions that had been copied and
 * pasted over and over again in this library.  This
 * header files is contains a common definitoin for
 * those file.
 * \author <NAME>
 */
#ifndef __antsUtilities_h__
#define __antsUtilities_h__

#include "antscout.hxx"
#include "antsAllocImage.h"
#include <string>
#include <vector>
#include <algorithm>

#include <stdlib.h>
#include <errno.h>
#include <cmath>
#include <iostream>

#include "itkVector.h"
#include "itkBinaryThresholdImageFilter.h"
#include "itkBinaryBallStructuringElement.h"
#include "itkBinaryDilateImageFilter.h"
#include "itkBinaryErodeImageFilter.h"
#include "itkGrayscaleDilateImageFilter.h"
#include "itkGrayscaleErodeImageFilter.h"

// We need to ensure that only one of these exists!
namespace ants
{
extern boost::iostreams::stream<ants_Sink> antscout;

// ##########################################################################
// ##########################################################################
// ##########################################################################
// ##########################################################################
// ##########################################################################
// ##########################################################################
// ##########################################################################
// Templates
template <class TImage>
typename TImage::Pointer  Morphological( typename TImage::Pointer input, float rad, unsigned int option,
                                         float dilateval)
{
  typedef TImage ImageType;
  enum { ImageDimension = TImage::ImageDimension };
  typedef typename TImage::PixelType PixelType;

  if( option == 0 )
    {
    antscout << " binary eroding the image " << std::endl;
    }
  else if( option == 1 )
    {
    antscout << " binary dilating the image " << std::endl;
    }
  else if( option == 2 )
    {
    antscout << " binary opening the image " << std::endl;
    }
  else if( option == 3 )
    {
    antscout << " binary closing the image " << std::endl;
    }
  else if( option == 4 )
    {
    antscout << " grayscale eroding the image " << std::endl;
    }
  else if( option == 5 )
    {
    antscout << " grayscale dilating the image " << std::endl;
    }
  else if( option == 6 )
    {
    antscout << " grayscale opening the image " << std::endl;
    }
  else if( option == 7 )
    {
    antscout << " grayscale closing the image " << std::endl;
    }

  typedef itk::BinaryBallStructuringElement<
      PixelType,
      ImageDimension>             StructuringElementType;

  typedef itk::BinaryErodeImageFilter<
      TImage,
      TImage,
      StructuringElementType>  ErodeFilterType;

  typedef itk::BinaryDilateImageFilter<
      TImage,
      TImage,
      StructuringElementType>  DilateFilterType;
  typename ErodeFilterType::Pointer  binaryErode  = ErodeFilterType::New();
  typename DilateFilterType::Pointer binaryDilate = DilateFilterType::New();

  StructuringElementType structuringElement;

  structuringElement.SetRadius( (unsigned long) rad );  // 3x3x3 structuring element

  structuringElement.CreateStructuringElement();

  binaryErode->SetKernel(  structuringElement );
  binaryDilate->SetKernel( structuringElement );
  // binaryOpen->SetKernal( structuringElement );
  // binaryClose->SetKernel( structuringElement );
  //
  // typename OpeningFilterType::Pointer  binaryOpen  = OpeningFilterType::New();
  // typename ClosingFilterType::Pointer binaryClose = ClosingFilterType::New();
  typedef itk::GrayscaleErodeImageFilter<
      TImage,
      TImage,
      StructuringElementType> GrayscaleErodeFilterType;

  typedef itk::GrayscaleDilateImageFilter<
      TImage,
      TImage,
      StructuringElementType> GrayscaleDilateFilterType;

  typename GrayscaleErodeFilterType::Pointer grayscaleErode = GrayscaleErodeFilterType::New();
  typename GrayscaleDilateFilterType::Pointer grayscaleDilate = GrayscaleDilateFilterType::New();
  grayscaleErode->SetKernel( structuringElement );
  grayscaleDilate->SetKernel( structuringElement );

  //  It is necessary to define what could be considered objects on the binary
  //  images. This is specified with the methods \code{SetErodeValue()} and
  //  \code{SetDilateValue()}. The value passed to these methods will be
  //  considered the value over which the dilation and erosion rules will apply
  binaryErode->SetErodeValue( (unsigned int ) dilateval );
  binaryDilate->SetDilateValue(  (unsigned int ) dilateval );

  typename TImage::Pointer temp;
  if( option == 1 )
    {
    antscout << " Dilate " << rad << std::endl;
    binaryDilate->SetInput( input );
    binaryDilate->Update();
    temp = binaryDilate->GetOutput();
    }
  else if( option == 0 )
    {
    antscout << " Erode " << rad << std::endl;
    binaryErode->SetInput( input );  // binaryDilate->GetOutput() );
    binaryErode->Update();
    temp = binaryErode->GetOutput();
    }
  else if( option == 2 )
    {
    // dilate(erode(img))
    antscout << " Binary Open " << rad << std::endl;
    // binaryOpen->SetInput( input );//binaryDilate->GetOutput() );
    // binaryOpen->Update();
    binaryErode->SetInput( input );
    binaryDilate->SetInput( binaryErode->GetOutput() );
    binaryDilate->Update();
    temp = binaryDilate->GetOutput();
    }
  else if( option == 3 )
    {
    antscout << " Binary Close " << rad << std::endl;
    // binaryClose->SetInput( input );//binaryDilate->GetOutput() );
    // binaryClose->Update();
    binaryDilate->SetInput( input );
    binaryErode->SetInput( binaryDilate->GetOutput() );
    binaryErode->Update();
    temp = binaryErode->GetOutput();
    }
  else if( option == 4 )
    {
    antscout << " Grayscale Erode " << rad << std::endl;
    grayscaleErode->SetInput( input ); // binaryDilate->GetOutput() );
    grayscaleErode->Update();
    temp = grayscaleErode->GetOutput();
    }
  else if( option == 5 )
    {
    antscout << " Grayscale Dilate " << rad << std::endl;
    grayscaleDilate->SetInput( input ); // binaryDilate->GetOutput() );
    grayscaleDilate->Update();
    temp = grayscaleDilate->GetOutput();
    antscout << " Grayscale Dilate Done " << temp << std::endl;
{'fraction_non_alphanumeric': 0.19731264496520834, 'fraction_numerical': 0.0027193473566344078, 'mean_word_length': 3.9093050647820964, 'pattern_counts': {'":': 0, '<': 175, '<?xml version=': 0, '>': 111, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5182252', 'n_tokens_mistral': 3875, 'n_tokens_neox': 3636, 'n_words': 843}