Canny 边缘检测
本小节的源代码在文件 Examples/Filtering/CannyEdgeDetectionImageFilter.cxx 中。 这个例子介绍了 itk::CannyEdgeDetectionImageFilter 的用法。这个滤波器由于它灵敏度高、定位精确和抗噪声能力强可以得到最佳的解决方案而广泛应用在边缘检测中。 使用这个滤波器的第一步是包含它的头文件:#include "itkCannyEdgeDetectionImageFilter.h"
这个滤波器对浮点型数据类型的像素类型图像进行操作,然后必须转换通常是整型的图像类型。这里使用 itk::CastImageFilter 来达到这一目的。定义它的图像模板参数用来把输入图像类型转换为用于处理的浮点型。
typedef itk::CastImageFilter< CharImageType, RealImageType> CastToRealFilterType;
使用浮点型图像类型来对 itk::CannyEdgeDetectionImageFilter
进行实例化。
Canny 边缘检测后,进行亮度映射可增强显示效果.
Canny 边缘检测到的是内外双边缘.
#include "itkImageFileReader.h"//读取头文件
#include "itkImageFileWriter.h"//写入头文件
#include "itkGDCMImageIO.h"//ImageIo头文件
#include "itkCannyEdgeDetectionImageFilter.h"//canney边缘检测
#include "itkCastImageFilter.h"//类型转换
#include "itkRescaleIntensityImageFilter.h"//亮度映射
int main(int argc, char* argv[])
{
typedef signed short PixelType; // signed short 数据类型
typedef double OutputPixelType; // double 数据类型
typedef itk::Image< PixelType, 2 > ImageType;//image类型
typedef itk::Image< OutputPixelType, 2 > OutputImageType;//Outputimage类型
typedef itk::ImageFileReader< ImageType > ReaderType;
typedef itk::ImageFileWriter< ImageType > WriterType;
ReaderType::Pointer reader = ReaderType::New();//reader
WriterType::Pointer writer = WriterType::New();//writer
typedef itk::GDCMImageIO ImageIOType;
ImageIOType::Pointer gdcmImageIO = ImageIOType::New();//gdcmImageIO
reader->SetImageIO(gdcmImageIO);
reader->SetFileName("D:/008.dcm");//读取文件
reader->Update();
typedef itk::CastImageFilter< ImageType, OutputImageType> CastFilterType;
CastFilterType::Pointer castfilter = CastFilterType::New();
castfilter->SetInput(reader->GetOutput());
castfilter->Update();
typedef itk::CannyEdgeDetectionImageFilter< OutputImageType, OutputImageType>CannyFilterType;
CannyFilterType::Pointer cannyfilter = CannyFilterType::New();
cannyfilter->SetInput(castfilter->GetOutput());
cannyfilter->Update();
typedef itk::RescaleIntensityImageFilter< OutputImageType, ImageType >RescaleFilterType;
RescaleFilterType::Pointer rescale = RescaleFilterType::New();
rescale->SetInput(cannyfilter->GetOutput());
rescale->Update();
writer->SetImageIO(gdcmImageIO);
writer->SetInput(rescale->GetOutput());
writer->SetFileName("D:/0080.dcm");
writer->Update();
return EXIT_SUCCESS;
}