本文主要是介绍ITK学习笔记——中值滤波和均值滤波,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
均值滤波和和中值滤波都可以起到平滑图像,虑去噪声的功能。
均值滤波采用线性的方法,平均整个窗口范围内的像素值,均值滤波本身存在着固有的缺陷,即它不能很好地保护图像细节,在图像去噪的同时也破坏了图像的细节部分,从而使图像变得模糊,不能很好地去除噪声点。均值滤波对高斯噪声表现较好,对椒盐噪声表现较差。
中值滤波采用非线性的方法,它在平滑脉冲噪声方面非常有效,同时它可以保护图像尖锐的边缘,选择适当的点来替代污染点的值,所以处理效果好,对椒盐噪声表现较好,对高斯噪声表现较差。
中值滤波:
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkMedianImageFilter.h"
#include "itkSubtractImageFilter.h"#include "itksys/SystemTools.hxx"
#include <sstream>#include "QuickView.h"int main(int argc, char * argv[])
{std::string inputFilename = "C:/input/Lenna.jpeg";typedef itk::Image<float, 2 > ImageType;typedef itk::ImageFileReader<ImageType> ReaderType;typedef itk::MedianImageFilter<ImageType, ImageType > FilterType;typedef itk::SubtractImageFilter<ImageType> SubtractType;ReaderType::Pointer reader = ReaderType::New();reader->SetFileName( inputFilename );FilterType::Pointer medianFilter = FilterType::New();FilterType::InputSizeType radius;radius.Fill(2);medianFilter->SetRadius(radius);medianFilter->SetInput( reader->GetOutput() );SubtractType::Pointer diff = SubtractType::New();diff->SetInput1(reader->GetOutput());diff->SetInput2(medianFilter->GetOutput());QuickView viewer;viewer.AddImage(reader->GetOutput(),true,itksys::SystemTools::GetFilenameName(inputFilename)); std::stringstream desc;desc << "MedianImageFilter, radius = " << radius;viewer.AddImage(medianFilter->GetOutput(),true,desc.str()); std::stringstream desc2;desc2 << "Original - Median";viewer.AddImage(diff->GetOutput(),true,desc2.str()); viewer.Visualize();return EXIT_SUCCESS;
}
由于读取没用RGB格式,所以会自动转化为灰度图。
均值滤波:
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkMeanImageFilter.h"
#include "itkSubtractImageFilter.h"#include "QuickView.h"int main(int argc, char * argv[])
{typedef itk::Image< unsigned char, 2 > UnsignedCharImageType;typedef itk::Image< float, 2 > FloatImageType;typedef itk::ImageFileReader< UnsignedCharImageType > ReaderType;typedef itk::MeanImageFilter<UnsignedCharImageType, UnsignedCharImageType > filterType;typedef itk::SubtractImageFilter<UnsignedCharImageType, UnsignedCharImageType, FloatImageType> SubtractType;ReaderType::Pointer reader = ReaderType::New();reader->SetFileName( "C:/input/Lenna.jpeg" );filterType::Pointer meanFilter = filterType::New();meanFilter->SetInput( reader->GetOutput() );SubtractType::Pointer diff = SubtractType::New();diff->SetInput1(reader->GetOutput());diff->SetInput2(meanFilter->GetOutput());QuickView viewer;viewer.AddImage(reader->GetOutput(),true,itksys::SystemTools::GetFilenameName("C:/input/Lenna.jpeg")); std::stringstream desc;desc << "Mean";viewer.AddImage(meanFilter->GetOutput(),true,desc.str()); std::stringstream desc2;desc2 << "Original - Mean";viewer.AddImage(diff->GetOutput(),true,desc2.str()); viewer.Visualize();return EXIT_SUCCESS;
}
这篇关于ITK学习笔记——中值滤波和均值滤波的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!