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;
}

测试效果

​

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

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

相关推荐
Zentceh31 分钟前
安防监控夜视方案2026:AI全彩+边缘计算+云平台
图像处理·人工智能·科技·计算机视觉·车载系统·无人机·边缘计算
YOLO数据集集合2 小时前
风机叶片表面损伤检测数据集 | 风机叶片 表面损伤 污渍检测 无人机巡检 风电运维9119期
运维·人工智能·计算机视觉·目标跟踪·无人机·智慧城市·电力巡检
小蒋观天下4 小时前
行业拆解|两轮车检测AI摄像头的核心技术迭代、产品演进及未来技术发展
大数据·人工智能·安全·计算机视觉·ai大模型
鬼手点金5 小时前
Claude Code示范案例-常用快捷命令
java·服务器·前端·计算机视觉·前向传播
兴通物联科技8 小时前
对俄出口诚实标识 2026 时间节点与产线赋码采集技术方案
大数据·服务器·单片机·嵌入式硬件·深度学习·计算机视觉
u0111026759 小时前
网页图片编辑器如何添加文字:字体、换行与导出一致性
图像处理·人工智能·ai作画·编辑器
欧特克_Glodon9 小时前
OpenCV计算机视觉开发入门与实践(基于C++):专栏内容介绍及目录
c++·人工智能·opencv·计算机视觉
AI浩9 小时前
WeDetect: 作为检索的快速开放词汇目标检测
人工智能·目标检测·计算机视觉
YFJ_mily9 小时前
CVISPR 2026计算机视觉模式识别智能系统|已上线IEEE官网 EI&Scopus检索
人工智能·计算机视觉·模式识别·智能系统·rdlink研发家·机器人自动化·ieee出版
sali-tec10 小时前
C# 基于OpenCv的视觉工作流-章110-YOLO 实例分割
图像处理·人工智能·opencv·算法·yolo·计算机视觉