ITK-均值滤波

作者:翟天保Steven

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

均值滤波原理

均值滤波是一种常用的图像平滑技术,用于减少图像中的噪声。其基本原理是通过计算图像中每个像素及其邻域像素的平均值来替换该像素的值,从而达到平滑图像的效果。

概括为以下几个步骤:

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

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

  3. 计算平均值:对于窗口覆盖的每一个位置,计算窗口内所有像素的平均值。

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

均值滤波可以有效地平滑图像,减少高频噪声,使图像看起来更加平滑。但它也有一些缺点,例如会模糊图像中的边缘,因为它同样平滑了边缘像素和非边缘像素。对于边缘检测等需要保留图像细节的任务,均值滤波可能不是最优选择。

环境准备

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

功能解析

1.引入必要的头文件:

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

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

cpp 复制代码
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.设置文件名:

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

4.类型转换:

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

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

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

6.类型转换:

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

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

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

完整代码

cpp 复制代码
#include <itkImage.h>
#include <itkImageFileReader.h>
#include <itkImageFileWriter.h>
#include <itkMeanImageFilter.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("mean_output.jpg");    // 写入的文件名

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

	// 创建均值滤波器
	typedef itk::MeanImageFilter<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 << "Mean filtering completed successfully." << std::endl;
	return EXIT_SUCCESS;
}

测试效果

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

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

相关推荐
BothSavage1 小时前
Trae远程开发中DeepSeek自定义模型4054错误的排查与修复
算法
小林ixn2 小时前
从暴力到KMP:一道题彻底搞懂字符串匹配的前世今生
算法
烬羽3 小时前
字符串算法入门:从反转字符串到回文判断,面试不再慌
算法·面试
先吃饱再说19 小时前
判断回文字符串,从一行代码到双指针优化
算法
黄敬峰1 天前
深入理解算法核心:从递归思想、数组扁平化到快速排序
算法
得物技术1 天前
从狂野代码到按目标生产:得物推荐 AI Harness 的工程化实践|AICon 演讲整理
人工智能·算法·架构
AI小老六1 天前
SkillOpt 架构拆解:把 Skill 文本当参数,用执行轨迹训练 Agent
后端·算法·ai编程
胡萝卜术1 天前
从“分数打架”到“排名投票”:为什么你的ChatBI必须用RRF?
算法·设计模式·面试
Asize1 天前
初识DFS 与 BFS:递归、队列与图遍历
算法