对二值图像扩展边缘用于图像掩膜,以下思路基于符号距离函数实现其边缘扩展,上代码:
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkSignedMaurerDistanceMapImageFilter.h"
#include "itkImageRegionIterator.h"
int main(int argc, char ** argv[])
{
if (argc < 3)
{
std::cerr << "Please Input InputImage and OutputImage" << std::endl;
return EXIT_FAILURE;
}
const char * inputFileName = argv[1];
const char * outputFileName = argv[2];
const int extendEdgeDistance = argv[3];
typedef unsigned char InputPixelType;
typedef float DistancePixelType;
const unsigned short Dimension = 3;
typedef itk::Image<InputPixelType, Dimension> InputImageType;
typedef itk::Image<DistancePixelType, Dimension> DistanceImageType;
typedef itk::ImageFileReader<InputImageType> ReaderType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(inputFileName);
reader->Update();
InputImageType::Pointer inputImage = reader->GetOutput();
typedef itk::SignedMaurerDistanceMapImageFilter< InputImageType, DistanceImageType > SignedMaurerDistanceMapImageFilterType;
SignedMaurerDistanceMapImageFilterType::Pointer distanceMapImageFilter = SignedMaurerDistanceMapImageFilterType::New();
distanceMapImageFilter->SetInput(inputImage);
distanceMapImageFilter->Update();
DistanceImageType::Pointer distanceImage = distanceMapImageFilter->GetOutput();
typedef itk::ImageRegionIterator<DistanceImageType> ImageIteratorType;
ImageIteratorType It(distanceImage, distanceImage->GetLargestPossibleRegion());
It.GoToBegin();
while (!It.IsAtEnd())
{
if (It.Get() < extendEdgeDistance)
{
It.Set(1);
}
else
{
It.Set(0);
}
++It;
}
typedef itk::ImageFileWriter< DistanceImageType > WriterType;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(outputFileName);
writer->SetInput(distanceImage);
try
{
writer->Update();
}
catch (itk::ExceptionObject &exp)
{
std::cerr << "Save File Error!" << std::endl;
std::cerr << exp << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}