二阶:Marr-Hildreth(LOG)算子,Canny算子,Laplacian算子。
好了,这里不讲大道理,理论知识网上都说的很清楚了,主要和itk官方大牛学算法代码。本文带大家了解下itk中的边缘检测算法。
1.Canny算子
//---------canny ,最重要的三个参数
float variance = 1.0;
float maximumError = 0.10;
float upperThreshold = 0.5;
typedef itk::MedianImageFilter FilterType;
FilterType::Pointer medianFilter = FilterType::New();
FilterType::InputSizeType radius;
radius.Fill(3);
medianFilter->SetRadius(radius);
medianFilter->SetInput( input_data);
//------------------------------------------------------------
typedef double RealPixelType; // Operations 注意这里需要转换成double
typedef itk::Image< RealPixelType, ImageDimension> RealImageType;
typedef itk::CastImageFilter< ImageType, RealImageType > CastToRealFilterType;
typedef itk::CannyEdgeDetectionImageFilter< RealImageType, RealImageType > CannyFilterType;
typedef itk::RescaleIntensityImageFilter< RealImageType, ImageType > RescaleFilterType;
CastToRealFilterType::Pointer toReal = CastToRealFilterType::New();
CannyFilterType::Pointer cannyFilter = CannyFilterType::New();
RescaleFilterType::Pointer rescale = RescaleFilterType::New();
toReal->SetInput( medianFilter->GetOutput() );
cannyFilter->SetInput( toReal->GetOutput() );
rescale->SetInput( cannyFilter->GetOutput() );
cannyFilter->SetVariance( variance );
cannyFilter->SetUpperThreshold( upperThreshold );
cannyFilter->SetMaximumError( maximumError );
//------------------------------------------------------------
output_data = rescale->GetOutput();
2.Sobel算子
typedef itk::SobelEdgeDetectionImageFilter ImageFilterType;
ImageFilterType::Pointer sobelFilter = ImageFilterType::New();
sobelFilter->SetInput(input_data);
output_data = sobelFilter->GetOutput();
This filter uses the Sobel operator to calculate the image gradient and then finds the magnitude of this gradient vector. The Sobel gradient magnitude (square-root sum of squares) is an indication of edge strength.
typedef NeighborhoodOperatorImageFilter OpFilter;
typedef MultiplyImageFilter MultFilter;
typedef NaryAddImageFilter AddFilter; //累计求和方案
typedef SqrtImageFilter SqrtFilter; //
unsigned int i;
typename TOutputImage::Pointer output = this->GetOutput();
output->SetBufferedRegion(output->GetRequestedRegion());
output->Allocate();
// Create the sobel operator
SobelOperator opers[ImageDimension];
ZeroFluxNeumannBoundaryCondition nbc;
// Setup mini-pipelines along each axis.
typename OpFilter::Pointer opFilter[ImageDimension]; //这个写法比较帅啊~
typename MultFilter::Pointer multFilter[ImageDimension];
typename AddFilter::Pointer addFilter = AddFilter::New();
typename SqrtFilter::Pointer sqrtFilter = SqrtFilter::New();
for(i=0; i < ImageDimension; ++i)
{
// Create the filters for this axis.
opFilter[i] = OpFilter::New();
multFilter[i] = MultFilter::New();
// Set boundary condition and operator for this axis.
opers[i].SetDirection(i);
opers[i].CreateDirectional();
opFilter[i]->OverrideBoundaryCondition(&nbc);
opFilter[i]->SetOperator(opers[i]);
// Setup the mini-pipeline for this axis.
opFilter[i]->SetInput(this->GetInput());
multFilter[i]->SetInput1(opFilter[i]->GetOutput()); //这个平方。。。
multFilter[i]->SetInput2(opFilter[i]->GetOutput());
// All axes' mini-pipelines come together in addFilter.
addFilter->SetInput(i, multFilter[i]->GetOutput());
}
// calculate the gradient magnitude
sqrtFilter->SetInput(addFilter->GetOutput()); //开方
// setup the mini-pipeline to calculate the correct regions and
// write to the appropriate bulk data block
sqrtFilter->GraftOutput( this->GetOutput() );
// execute the mini-pipeline
sqrtFilter->Update();
3.Zero-crossing算子
double var = 5.0;
typedef itk::ZeroCrossingBasedEdgeDetectionImageFilterFilterType;
FilterType::Pointer edgeDetector = FilterType::New();
edgeDetector->SetInput( reader->GetOutput() );
FilterType::ArrayType variance;
variance.Fill(var);
edgeDetector->SetVariance(variance);