《itk实用demo》-序列图像hough变换

版权声明:本文为博主原创文章,转载请注明出处,谢谢 https://blog.csdn.net/rabbitbride/article/details/82415141

序列图像hough变换,并保存为序列图像

#include "itkHoughTransform2DCirclesImageFilter.h"
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkImageRegionIterator.h"
#include "itkThresholdImageFilter.h"
#include "itkMinimumMaximumImageCalculator.h"
#include <itkGradientMagnitudeImageFilter.h>
#include <itkDiscreteGaussianImageFilter.h>
#include <list>
#include "itkCastImageFilter.h"
#include "vnl/vnl_math.h"

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageToVTKImageFilter.h"
#include "vtkImageViewer.h"
#include "vtkWin32RenderWindowInteractor.h"
#include "vtkImageCast.h"

#include "itkLabelOverlayImageFilter.h"
#include "itkImageFileWriter.h"
#include <iostream>
#include <string>
using namespace std;
int main( int argc, char *argv[] )
{
 // Software Guide : BeginCodeSnippet
 const     unsigned int    Dimension = 2;
 typedef   float           AccumulatorPixelType;
 typedef  unsigned char   InputPixelType;
 typedef  unsigned char   OutputPixelType;
 typedef itk::Image< InputPixelType, Dimension >  InputImageType;
 typedef itk::Image< OutputPixelType, Dimension >  OutputImageType;

 InputImageType::IndexType localIndex;
 typedef itk::Image< AccumulatorPixelType, Dimension > AccumulatorImageType;  

 typedef  itk::ImageFileReader< InputImageType > ReaderType;

 typedef itk::RGBPixel< unsigned char >   RGBPixelType;
 typedef itk::Image< RGBPixelType, Dimension >  RGBImageType;
 typedef itk::LabelOverlayImageFilter< InputImageType, OutputImageType, RGBImageType > OverlayType;
 std::string bmp = ".bmp";
 typedef  itk::ImageFileWriter< RGBImageType  > WriterType;

 for (int num= 0;num<214;num++)
 {
  std::string  inputFilename = "D:\\se6\\rgb\\IM";
  std::string  outputFilename = "D:\\se6\\hough2\\IM";

  char t[256];
  string s;
  sprintf(t, "%d", num);
  s = t;
  inputFilename = inputFilename + s + bmp;
  outputFilename = outputFilename + s + bmp;

  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName(inputFilename);
  reader->Update();

  InputImageType::Pointer localImage = reader->GetOutput();

  // Software Guide : BeginCodeSnippet
  std::cout << "Computing Hough Map" << std::endl;

  typedef itk::HoughTransform2DCirclesImageFilter<InputPixelType,
   AccumulatorPixelType> HoughTransformFilterType;
  HoughTransformFilterType::Pointer houghFilter = HoughTransformFilterType::New();
  // Software Guide : BeginCodeSnippet
  houghFilter->SetInput( reader->GetOutput() );

  houghFilter->SetNumberOfCircles(1);
  houghFilter->SetMinimumRadius(5);
  houghFilter->SetMaximumRadius(30);

  houghFilter->Update();
  AccumulatorImageType::Pointer localAccumulator = houghFilter->GetOutput();  
  // Software Guide : BeginCodeSnippet
  HoughTransformFilterType::CirclesListType circles;
  circles = houghFilter->GetCircles(1);
  std::cout << "Found " << circles.size() << " circle(s)." << std::endl;

  // Software Guide : BeginCodeSnippet
  typedef  unsigned char                            OutputPixelType;
  typedef  itk::Image< OutputPixelType, Dimension > OutputImageType;  

  OutputImageType::Pointer  localOutputImage = OutputImageType::New();

  OutputImageType::RegionType region;
  region.SetSize(localImage->GetLargestPossibleRegion().GetSize());
  region.SetIndex(localImage->GetLargestPossibleRegion().GetIndex());
  localOutputImage->SetRegions( region );
  localOutputImage->SetOrigin(localImage->GetOrigin());
  localOutputImage->SetSpacing(localImage->GetSpacing());
  localOutputImage->Allocate();
  localOutputImage->FillBuffer(0);
  // Software Guide : BeginCodeSnippet
  typedef HoughTransformFilterType::CirclesListType CirclesListType;
  CirclesListType::const_iterator itCircles = circles.begin();

  while( itCircles != circles.end() )
  {
   std::cout << "Center: ";
   std::cout << (*itCircles)->GetObjectToParentTransform()->GetOffset()
    << std::endl;
   std::cout << "Radius: " << (*itCircles)->GetRadius()[0] << std::endl;

   // Software Guide : BeginCodeSnippet
   for(double angle = 0;angle <= 2*vnl_math::pi; angle += vnl_math::pi/60.0 )
   {
    localIndex[0] =
     (long int)((*itCircles)->GetObjectToParentTransform()->GetOffset()[0]
    + (*itCircles)->GetRadius()[0]*vcl_cos(angle));
    localIndex[1] =
     (long int)((*itCircles)->GetObjectToParentTransform()->GetOffset()[1]
    + (*itCircles)->GetRadius()[0]*vcl_sin(angle));
    OutputImageType::RegionType outputRegion =
     localOutputImage->GetLargestPossibleRegion();

    if( outputRegion.IsInside( localIndex ) )
    {
     localOutputImage->SetPixel( localIndex, 255 );
    }
   }
   itCircles++;
  }

  OverlayType::Pointer overlay = OverlayType::New();
  overlay->SetInput( localImage );
  overlay->SetLabelImage( localOutputImage);


  WriterType::Pointer writer = WriterType::New();
  writer->SetFileName(outputFilename);
  writer->SetInput(overlay->GetOutput());
  writer->Update();
 }
 return 0;
}
}

猜你喜欢

转载自blog.csdn.net/rabbitbride/article/details/82415141