MATLAB实现的利用小波算法进行图像去噪

基于MATLAB实现的利用小波算法进行图像去噪

1. 生成含噪图像

% 读取图像
I = imread('cameraman.tif'); % 替换为实际图像路径
I = im2double(I);

% 添加高斯噪声
noiseVar = 0.01; % 噪声方差
I_noisy = imnoise(I, 'gaussian', 0, noiseVar);

2. 小波分解

% 选择小波基和分解层数
waveletName = 'db1'; % 小波基
level = 2; % 分解层数

% 对含噪图像进行小波分解
[coeffs, sizes] = wavedec2(I_noisy, level, waveletName);

3. 阈值处理

% 选择阈值方法
thresholdMethod = 'global'; % 全局阈值或层阈值

% 计算阈值
if strcmp(thresholdMethod, 'global')
    sigma = median(abs(coeffs(:))) / 0.6745; % 估计噪声标准差
    threshold = sigma * sqrt(2 * log(numel(I_noisy)));
elseif strcmp(thresholdMethod, 'level')
    threshold = cell(1, level);
    for i = 1:level
        sigma = median(abs(coeffs(sizes(i, 1)+1:sizes(i+1, 1)))) / 0.6745;
        threshold{i} = sigma * sqrt(2 * log(numel(I_noisy)));
    end
end

% 阈值处理
coeffs_new = coeffs;
for i = 1:level
    if strcmp(thresholdMethod, 'global')
        coeffs_new(sizes(i, 1)+1:sizes(i+1, 1)) = wthresh(coeffs(sizes(i, 1)+1:sizes(i+1, 1)), 's', threshold);
    elseif strcmp(thresholdMethod, 'level')
        coeffs_new(sizes(i, 1)+1:sizes(i+1, 1)) = wthresh(coeffs(sizes(i, 1)+1:sizes(i+1, 1)), 's', threshold{i});
    end
end

4. 小波重构

% 对处理后的系数进行小波重构
I_denoised = waverec2(coeffs_new, sizes, waveletName);

5. 显示结果

% 显示原始图像、含噪图像和去噪后的图像
figure;
subplot(1, 3, 1);
imshow(I);
title('原始图像');

subplot(1, 3, 2);
imshow(I_noisy);
title('含噪图像');

subplot(1, 3, 3);
imshow(I_denoised);
title('去噪后的图像');

参考代码 youwenfan.com/contentcnb/81411.html 利用小波算法实现图像去噪,包含软硬阈值去噪,中值,均值滤波等,内含图片灰度与彩色实例。

posted @ 2025-08-06 09:38  康帅服  阅读(17)  评论(0)    收藏  举报