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

测试效果

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

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

相关推荐
CoderCodingNo1 小时前
【GESP】C++五级练习题 luogu-P1865 A % B Problem
开发语言·c++·算法
大闲在人1 小时前
7. 供应链与制造过程术语:“周期时间”
算法·供应链管理·智能制造·工业工程
小熳芋1 小时前
443. 压缩字符串-python-双指针
算法
Charlie_lll2 小时前
力扣解题-移动零
后端·算法·leetcode
chaser&upper2 小时前
矩阵革命:在 AtomGit 解码 CANN ops-nn 如何构建 AIGC 的“线性基石”
程序人生·算法
weixin_499771552 小时前
C++中的组合模式
开发语言·c++·算法
iAkuya2 小时前
(leetcode)力扣100 62N皇后问题 (普通回溯(使用set存储),位运算回溯)
算法·leetcode·职场和发展
近津薪荼2 小时前
dfs专题5——(二叉搜索树中第 K 小的元素)
c++·学习·算法·深度优先
xiaoye-duck2 小时前
吃透 C++ STL list:从基础使用到特性对比,解锁链表容器高效用法
c++·算法·stl
松☆2 小时前
CANN与大模型推理:在边缘端高效运行7B参数语言模型的实践指南
人工智能·算法·语言模型