ITK-中值滤波

作者:翟天保Steven

版权声明:著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处

中值滤波原理

中值滤波是一种常用的非线性滤波技术,用于去除图像中的噪声,特别是椒盐噪声和脉冲噪声。它通过在图像中移动一个窗口,并用窗口内像素的中值替换中心像素的值,从而达到去噪的效果。

可以概括为以下几个步骤:

  1. 选择滤波窗口:定义一个窗口(通常是矩形或方形),该窗口覆盖目标像素及其邻域像素。窗口的大小由用户指定,例如3×3、5×5等。

  2. 移动窗口:将窗口依次移动到图像中的每一个像素位置,逐个处理每个像素。

  3. 计算中值:对于窗口覆盖的每一个位置,按像素值大小排序,然后选择中间的值作为中值。如果窗口包含奇数个像素,中值就是排序后中间位置的像素值;如果窗口包含偶数个像素,中值是排序后中间两个像素值的平均值或其中之一。

  4. 替换像素值:用计算得到的中值替换当前窗口中心像素的值。

中值滤波具有以下效果和优点:

  1. 去除噪声:中值滤波特别适用于去除椒盐噪声和脉冲噪声,因为它能够有效地过滤掉极值点(即噪声),而不会过多影响其他像素。

  2. 保留边缘:与均值滤波不同,中值滤波能够更好地保留图像中的边缘和细节。因为中值滤波是基于像素排序的非线性操作,边缘不会像均值滤波那样被模糊掉。

  3. 鲁棒性:中值滤波对图像中的孤立噪声点有很强的鲁棒性,能够保持图像的结构和细节。

中值滤波在图像处理和计算机视觉中有广泛的应用,尤其是在图像预处理阶段,用于去除噪声、平滑图像等。它在医学图像处理、卫星图像分析和工业检测等领域中得到了广泛应用。

环境准备

参见:Windows下用CMake编译ITK及配置测试_itk配置-CSDN博客

功能解析

1.引入必要的头文件:

复制代码
#include <itkImage.h>
#include <itkImageFileReader.h>
#include <itkImageFileWriter.h>
#include <itkMedianImageFilter.h>
#include <itkRescaleIntensityImageFilter.h>
#include <itkJPEGImageIOFactory.h>
#include <itkCastImageFilter.h>

2.初始化图像类型和读写器:

复制代码
typedef itk::Image<unsigned char, 2> CharImageType;
typedef itk::Image<float, 2> FloatImageType;
typedef itk::ImageFileReader<CharImageType> ReaderType;
typedef itk::ImageFileWriter<CharImageType> WriterType;
itk::JPEGImageIOFactory::RegisterOneFactory();
ReaderType::Pointer reader = ReaderType::New();
WriterType::Pointer writer = WriterType::New();

3.设置文件名:

复制代码
// 设置
reader->SetFileName("test.jpg");            // 要读取的文件名
writer->SetFileName("median_output3.jpg");    // 写入的文件名

4.类型转换:

复制代码
// 类型转换
typedef itk::CastImageFilter<CharImageType, FloatImageType> CastFilterType;
CastFilterType::Pointer castfilter = CastFilterType::New();
castfilter->SetInput(reader->GetOutput());

5.创建中值滤波器,并配置参数:

复制代码
// 创建中值滤波器
typedef itk::MedianImageFilter<FloatImageType, FloatImageType> FilterType;
FilterType::Pointer filter = FilterType::New();
filter->SetInput(castfilter->GetOutput());
// 设置参数
FilterType::InputSizeType radius;
radius.Fill(3); // 设置中值滤波器的半径
filter->SetRadius(radius);

6.类型转换:

复制代码
// 将浮点图像转换为unsigned char类型
typedef itk::RescaleIntensityImageFilter<FloatImageType, CharImageType> RescaleFilterType;
RescaleFilterType::Pointer castfilter2 = RescaleFilterType::New();
castfilter2->SetInput(filter->GetOutput());

7.连接过滤器输出到写入器并执行写入操作:

复制代码
writer->SetInput(castfilter2->GetOutput());
try
{
    writer->Update();
}
catch (itk::ExceptionObject &error)
{
    std::cerr << "Error: " << error << std::endl;
    return EXIT_FAILURE;
}

完整代码

复制代码
#include <itkImage.h>
#include <itkImageFileReader.h>
#include <itkImageFileWriter.h>
#include <itkMedianImageFilter.h>
#include <itkRescaleIntensityImageFilter.h>
#include <itkJPEGImageIOFactory.h>
#include <itkCastImageFilter.h>

int main()
{
	// 初始化
	typedef itk::Image<unsigned char, 2> CharImageType;
	typedef itk::Image<float, 2> FloatImageType;
	typedef itk::ImageFileReader<CharImageType> ReaderType;
	typedef itk::ImageFileWriter<CharImageType> WriterType;
	itk::JPEGImageIOFactory::RegisterOneFactory();
	ReaderType::Pointer reader = ReaderType::New();
	WriterType::Pointer writer = WriterType::New();

	// 设置
	reader->SetFileName("test.jpg");            // 要读取的文件名
	writer->SetFileName("median_output3.jpg");    // 写入的文件名

	// 类型转换
	typedef itk::CastImageFilter<CharImageType, FloatImageType> CastFilterType;
	CastFilterType::Pointer castfilter = CastFilterType::New();
	castfilter->SetInput(reader->GetOutput());

	// 创建中值滤波器
	typedef itk::MedianImageFilter<FloatImageType, FloatImageType> FilterType;
	FilterType::Pointer filter = FilterType::New();
	filter->SetInput(castfilter->GetOutput());

	// 设置参数
	FilterType::InputSizeType radius;
	radius.Fill(3); // 设置中值滤波器的半径
	filter->SetRadius(radius);

	// 将浮点图像转换为unsigned char类型
	typedef itk::RescaleIntensityImageFilter<FloatImageType, CharImageType> RescaleFilterType;
	RescaleFilterType::Pointer castfilter2 = RescaleFilterType::New();
	castfilter2->SetInput(filter->GetOutput());

	// 输出
	writer->SetInput(castfilter2->GetOutput());
	try
	{
		writer->Update();
	}
	catch (itk::ExceptionObject &error)
	{
		std::cerr << "Error: " << error << std::endl;
		return EXIT_FAILURE;
	}

	std::cout << "Median filtering completed successfully." << std::endl;
	return EXIT_SUCCESS;
}

测试效果

通过调整参数,可以实现不同的滤波效果。

如果文章帮助到你了,可以点个赞让我知道,我会很快乐~加油!

相关推荐
Geek 研究僧3 小时前
iPhone 17 Pro Max 的影像升级全解:从长焦、前置聊到 ProRes RAW
图像处理·ios·iphone·影像
edward11104 小时前
[C++]探索现代C++中的移动语义与完美转发从底层原理到高级应用
计算机视觉
CoovallyAIHub8 小时前
告别等待!十条高效PyTorch数据增强流水线,让你的GPU不再"饥饿"
深度学习·算法·计算机视觉
CoovallyAIHub9 小时前
量子计算迎来诺奖时刻!谷歌赢麻了
深度学习·算法·计算机视觉
菜鸟‍9 小时前
【论文学习】2025年图像处理顶会论文
图像处理·人工智能·学习
AI视觉网奇10 小时前
开源3d数字人学习笔记2025
人工智能·计算机视觉
深瞳智检21 小时前
YOLO算法原理详解系列 第002期-YOLOv2 算法原理详解
人工智能·算法·yolo·目标检测·计算机视觉·目标跟踪
春末的南方城市1 天前
开放指令编辑创新突破!小米开源 Lego-Edit 登顶 SOTA:用强化学习为 MLLM 编辑开辟全新赛道!
人工智能·深度学习·机器学习·计算机视觉·aigc
WWZZ20251 天前
ORB_SLAM2原理及代码解析:SetPose() 函数
人工智能·opencv·算法·计算机视觉·机器人·自动驾驶