当前位置: 首页 > news >正文

MATLAB 灰度图像的二维傅里叶变换

基本傅里叶变换

基础实现代码

functionbasic_fft2_demo()% 读取灰度图像I=imread('cameraman.tif');% 使用MATLAB自带图像或替换为你的图像路径% 转换为double类型以便计算I_double=im2double(I);% 执行二维傅里叶变换F=fft2(I_double);% 将零频率分量移到中心F_shift=fftshift(F);% 计算幅度谱和相位谱magnitude_spectrum=abs(F_shift);phase_spectrum=angle(F_shift);% 对幅度谱取对数以便更好地显示log_magnitude=log(1+magnitude_spectrum);% 显示结果figure('Position',[100,100,1200,300]);subplot(1,4,1);imshow(I);title('原始图像');subplot(1,4,2);imshow(log_magnitude,[]);title('对数幅度谱');subplot(1,4,3);imshow(phase_spectrum,[]);title('相位谱');% 逆变换重建图像F_ishift=ifftshift(F_shift);I_reconstructed=real(ifft2(F_ishift));subplot(1,4,4);imshow(I_reconstructed,[]);title('重建图像');% 计算重建误差reconstruction_error=norm(I_double-I_reconstructed,'fro');fprintf('重建误差: %e\n',reconstruction_error);end

频域滤波应用

频域滤波器实现

functionfrequency_filtering_demo()% 读取图像I=imread('cameraman.tif');I=im2double(I);[M,N]=size(I);% 傅里叶变换F=fft2(I);F_shift=fftshift(F);% 创建频率坐标网格u=(0:M-1)-floor(M/2);v=(0:N-1)-floor(N/2);[U,V]=meshgrid(v,u);D=sqrt(U.^2+V.^2);% 到中心的距离% 设计不同的滤波器cutoff_low=30;% 低通截止频率cutoff_high=30;% 高通截止频率cutoff_band=50;% 带通中心频率% 低通滤波器 (理想低通)H_low=double(D<=cutoff_low);% 高通滤波器 (理想高通)H_high=double(D>=cutoff_high);% 高斯低通滤波器D0=30;% 截止频率H_gaussian=exp(-(D.^2)/(2*D0^2));% 应用滤波器F_low=F_shift.*H_low;F_high=F_shift.*H_high;F_gaussian=F_shift.*H_gaussian;% 逆变换I_low=real(ifft2(ifftshift(F_low)));I_high=real(ifft2(ifftshift(F_high)));I_gaussian=real(ifft2(ifftshift(F_gaussian)));% 显示结果figure('Position',[100,100,1200,600]);subplot(2,4,1);imshow(I);title('原始图像');subplot(2,4,2);imshow(H_low,[]);title('理想低通滤波器');subplot(2,4,3);imshow(H_high,[]);title('理想高通滤波器');subplot(2,4,4);imshow(H_gaussian,[]);title('高斯低通滤波器');subplot(2,4,5);imshow(I,[]);title('原始图像');subplot(2,4,6);imshow(I_low,[]);title('理想低通滤波结果');subplot(2,4,7);imshow(I_high,[]);title('理想高通滤波结果');subplot(2,4,8);imshow(I_gaussian,[]);title('高斯低通滤波结果');end

图像增强应用

高频增强和同态滤波

functionimage_enhancement_demo()% 读取图像I=imread('pout.tif');% 使用低对比度图像I=im2double(I);[M,N]=size(I);% 傅里叶变换F=fft2(I);F_shift=fftshift(F);% 高频增强滤波器u=(0:M-1)-floor(M/2);v=(0:N-1)-floor(N/2);[U,V]=meshgrid(v,u);D=sqrt(U.^2+V.^2);D_max=max(D(:));% 高频增强传递函数alpha=1.5;% 增强系数beta=0.5;H_enhance=alpha+beta*(D/D_max);% 应用高频增强F_enhanced=F_shift.*H_enhance;I_enhanced=real(ifft2(ifftshift(F_enhanced)));% 同态滤波 (用于同时增强对比度和压缩动态范围)% 对图像取对数I_log=log(I+0.01);% 加小值避免log(0)% 傅里叶变换F_log=fft2(I_log);F_log_shift=fftshift(F_log);% 同态滤波函数 (高频增强,低频抑制)gammaL=0.5;% 低频增益gammaH=2.0;% 高频增益c=1;% 锐化常数D0=30;% 截止频率H_homomorphic=(gammaH-gammaL)*(1-exp(-c*(D.^2/D0^2)))+gammaL;% 应用同态滤波F_homomorphic=F_log_shift.*H_homomorphic;I_homomorphic=real(ifft2(ifftshift(F_homomorphic)));I_homomorphic=exp(I_homomorphic);% 指数变换% 显示结果figure('Position',[100,100,1200,400]);subplot(1,4,1);imshow(I);title('原始图像');subplot(1,4,2);imshow(H_enhance,[]);title('高频增强滤波器');subplot(1,4,3);imshow(I_enhanced,[]);title('高频增强结果');subplot(1,4,4);imshow(I_homomorphic,[]);title('同态滤波结果');end

纹理分析和特征提取

频谱分析和特征计算

functiontexture_analysis_demo()% 读取纹理图像I=imread('texture.png');% 替换为你的纹理图像I=im2double(I);ifsize(I,3)==3I=rgb2gray(I);end[M,N]=size(I);% 傅里叶变换F=fft2(I);F_shift=fftshift(F);magnitude_spectrum=abs(F_shift);% 计算径向功率谱center_x=floor(N/2)+1;center_y=floor(M/2)+1;[X,Y]=meshgrid(1:N,1:M);R=sqrt((X-center_x).^2+(Y-center_y).^2);R=round(R);% 计算径向平均功率谱r_max=min(floor(M/2),floor(N/2));radial_profile=zeros(1,r_max);forr=0:r_max-1mask=(R==r);ifsum(mask(:))>0radial_profile(r+1)=mean(magnitude_spectrum(mask));endend% 计算纹理特征total_power=sum(magnitude_spectrum(:).^2);% 低频能量比例 (中心区域)low_freq_mask=(R<=min(M,N)/8);low_freq_energy=sum(magnitude_spectrum(low_freq_mask).^2)/total_power;% 高频能量比例 (外围区域)high_freq_mask=(R>=min(M,N)/4);high_freq_energy=sum(magnitude_spectrum(high_freq_mask).^2)/total_power;% 显示结果figure('Position',[100,100,1000,400]);subplot(1,3,1);imshow(I);title('原始纹理图像');subplot(1,3,2);imshow(log(1+magnitude_spectrum),[]);title('对数幅度谱');subplot(1,3,3);plot(0:r_max-1,radial_profile);xlabel('径向频率');ylabel('平均幅度');title('径向功率谱');grid on;% 输出纹理特征fprintf('纹理特征分析:\n');fprintf('总功率: %.4f\n',total_power);fprintf('低频能量比例: %.4f\n',low_freq_energy);fprintf('高频能量比例: %.4f\n',high_freq_energy);fprintf('低频/高频能量比: %.4f\n',low_freq_energy/high_freq_energy);end

噪声检测和图像恢复

周期性噪声检测和去除

functionnoise_detection_removal()% 创建带有周期性噪声的图像I_clean=im2double(imread('cameraman.tif'));[M,N]=size(I_clean);% 添加周期性噪声[X,Y]=meshgrid(1:N,1:M);noise=0.1*sin(2*pi*X/20)+0.1*sin(2*pi*Y/15);I_noisy=I_clean+noise;I_noisy=min(max(I_noisy,0),1);% 限制在[0,1]范围内% 傅里叶变换F_noisy=fft2(I_noisy);F_shift=fftshift(F_noisy);magnitude_spectrum=abs(F_shift);log_magnitude=log(1+magnitude_spectrum);% 检测噪声频率 (寻找明显的峰值)threshold=mean(log_magnitude(:))+2*std(log_magnitude(:));noise_mask=(log_magnitude>threshold)&...~((X>=N/2-5&X<=N/2+5)&(Y>=M/2-5&Y<=M/2+5));% 排除DC分量% 创建陷波滤波器H_notch=ones(M,N);H_notch(noise_mask)=0;% 应用陷波滤波器F_filtered=F_shift.*H_notch;I_filtered=real(ifft2(ifftshift(F_filtered)));% 显示结果figure('Position',[100,100,1200,400]);subplot(1,4,1);imshow(I_noisy);title('带噪声图像');subplot(1,4,2);imshow(log_magnitude,[]);title('噪声图像频谱');hold on;[noise_y,noise_x]=find(noise_mask);plot(noise_x,noise_y,'r.','MarkerSize',10);title('频谱 (红点:检测到的噪声)');subplot(1,4,3);imshow(H_notch,[]);title('陷波滤波器');subplot(1,4,4);imshow(I_filtered,[]);title('去噪后图像');% 计算信噪比改善snr_original=10*log10(var(I_clean(:))/var(I_noisy(:)-I_clean(:)));snr_filtered=10*log10(var(I_clean(:))/var(I_filtered(:)-I_clean(:)));fprintf('信噪比分析:\n');fprintf('原始图像SNR: %.2f dB\n',snr_original);fprintf('滤波后SNR: %.2f dB\n',snr_filtered);fprintf('SNR改善: %.2f dB\n',snr_filtered-snr_original);end

实用工具函数

通用的傅里叶变换工具包

functionfft_toolkit()% 这是一个综合的傅里叶变换工具包% 1. 图像傅里叶变换可视化functionvisualize_fft2(I,title_str)I=im2double(I);F=fft2(I);F_shift=fftshift(F);magnitude=abs(F_shift);phase=angle(F_shift);figure('Position',[100,100,800,200]);subplot(1,4,1);imshow(I);title([title_str,' - 原图']);subplot(1,4,2);imshow(log(1+magnitude),[]);title('幅度谱');subplot(1,4,3);imshow(phase,[]);title('相位谱');% 重建验证I_recon=real(ifft2(ifftshift(F_shift)));subplot(1,4,4);imshow(I_recon,[]);title('重建图像');end% 2. 频域滤波器设计functionH=create_filter(type,M,N,params)u=(0:M-1)-floor(M/2);v=(0:N-1)-floor(N/2);[U,V]=meshgrid(v,u);D=sqrt(U.^2+V.^2);switchtypecase'lowpass'D0=params.D0;H=double(D<=D0);case'highpass'D0=params.D0;H=double(D>=D0);case'gaussian_lowpass'D0=params.D0;H=exp(-(D.^2)/(2*D0^2));case'butterworth_lowpass'D0=params.D0;n=params.n;H=1./(1+(D./D0).^(2*n));otherwiseH=ones(M,N);endend% 3. 频域卷积functionI_filtered=frequency_convolution(I,H)I=im2double(I);F=fft2(I);F_shift=fftshift(F);F_filtered=F_shift.*H;I_filtered=real(ifft2(ifftshift(F_filtered)));end% 使用示例I=imread('cameraman.tif');visualize_fft2(I,'测试图像');[M,N]=size(I);params.D0=30;H=create_filter('gaussian_lowpass',M,N,params);I_filtered=frequency_convolution(I,H);figure;subplot(1,2,1);imshow(I);title('原图');subplot(1,2,2);imshow(I_filtered,[]);title('高斯低通滤波');end

参考代码 灰度图像的二维傅里叶变换www.3dddown.com/csa/69842.html

使用和技巧

  1. 图像预处理

    • 使用im2double()将图像转换为double类型
    • 确保图像是灰度图像
  2. 频谱显示

    • 使用fftshift()将零频率移到中心
    • 对幅度谱取对数(log(1 + magnitude))改善可视化效果
  3. 滤波器设计

    • 理想滤波器会产生振铃效应
    • 高斯滤波器过渡平滑,效果更好
  4. 性能优化

    • 对于大图像,考虑使用fft2的单精度版本
    • 多次使用时可以预计算频率网格
http://www.cnnetsun.cn/news/50986.html

相关文章:

  • 快速验证:基于CentOS 7.6的测试环境搭建
  • AI定价实战指南:快速构建电商智能定价系统
  • VGGT三维重建终极指南:从零开始构建你的3D世界
  • 电商网站秒开秘籍:快马AI加载优化案例
  • 15分钟快速验证:谷歌服务离线包生成器原型开发
  • 1小时搞定ElementUI原型:快马平台实战
  • 从天喵装机案例看中小企业IT设备采购新范式
  • Flink面试题实战:从问题到解决方案
  • 5分钟用C# Socket搭建文件传输原型
  • 论文查重不花一分钱?宏智树AI开启学术诚信新“净”界!
  • 最强安卓投屏神器QtScrcpy
  • 开题报告“自造机”VS“人工苦力”:宏智树AI凭何成为学术起航新引擎?
  • 还在为SPSS代码头疼?5款AI数据分析工具实测:有的只能画图,有的却能直接嵌入论文全流程
  • 单环PID控制Buck电路实现方案
  • 零基础必学:CSS div居中完全指南(图文详解)
  • COCO数据集工具库完整使用指南:从入门到实战应用
  • c盘红了怎么清理c盘空间?
  • 传统排错vsAI诊断:503错误处理效率提升300%
  • 清理后空间为什么很快又满了?
  • 企业级应用中的SSL证书故障排查实战
  • GPT-OSS-Safeguard-20B:开源AI安全推理模型重构内容审核范式
  • LogicFlow自定义节点:5步打造个性化流程图组件
  • ESP8266引脚实战:从零搭建智能温湿度监测系统
  • 1小时快速构建IDM集成模块原型
  • Ubuntu中文输入法在企业办公环境中的实战部署
  • 快速原型设计:用AI即时生成带省略号的UI组件
  • 零基础入门:用Cursor免费版写出你的第一行代码
  • 零基础入门:用大模型开启AI学习之旅
  • 零基础图解MinGW安装:小学生都能看懂
  • 对比评测:5种Ubuntu中文输入法的输入效率差异