
fspecial(生成点扩散函数 PSF,模拟模糊退化)、imfilter(应用 PSF 实现图像退化);imnoise(添加高斯噪声、椒盐噪声等);deconvwnr(维纳滤波)、deconvblind(盲去卷积)、逆滤波(基于频域逆变换)。读取彩色图像→转换为灰度图→通过运动模糊 PSF 实现退化→可视化原图、灰度图及退化图像。
% 读取彩色图像
I = imread('天火三玄变.jpg');
% 转换为灰度图
I_gray = rgb2gray(I);
% 设置运动模糊参数并生成点扩散函数(PSF)
LEN = 20; % 运动模糊长度(像素移动距离)
THETA = 30; % 运动模糊角度(度)
PSF = fspecial('motion', LEN, THETA);
% 应用PSF进行退化处理(circular表示循环边界条件,conv表示卷积)
I_degraded = imfilter(I_gray, PSF, 'circular', 'conv');
% 可视化结果
figure('Name', '单一运动模糊退化', 'NumberTitle', 'off');
subplot(1,3,1); imshow(I); title('原图');
subplot(1,3,2); imshow(I_gray); title('灰度图');
subplot(1,3,3); imshow(I_degraded); title('退化图像');
实现运动模糊、圆盘状模糊、钝化模糊三种退化,对比不同模糊效果及像素矩阵变化。
% 1. 读取图像并转换为灰度图
I = imread('ALi.jpg');
I_gray = rgb2gray(I);
% 2. 生成三种模糊的点扩散函数(PSF)
psf_motion = fspecial('motion', 30, 30); % 运动模糊(长度30,角度30度)
psf_disk = fspecial('disk', 20); % 圆盘状模糊(半径20,模拟散焦模糊)
psf_unsharp = fspecial('unsharp'); % 钝化模糊(削弱高频细节,使图像变钝)
% 3. 应用模糊处理
I_motion = imfilter(I_gray, psf_motion, 'circular', 'conv');
I_disk = imfilter(I_gray, psf_disk, 'circular', 'conv');
I_unsharp = imfilter(I_gray, psf_unsharp, 'circular', 'conv');
% 4. 可视化所有结果
figure('Name', '多种模糊退化对比', 'NumberTitle', 'off');
subplot(1,4,1); imshow(I_gray); title('原图');
subplot(1,4,2); imshow(I_motion); title('运动模糊(motion)');
subplot(1,4,3); imshow(I_disk); title('圆盘状模糊(disk)');
subplot(1,4,4); imshow(I_unsharp); title('钝化模糊(unsharp)');
% 5. 输出部分像素矩阵,观察变化
fprintf('原图左上角5x5像素值:\n');
disp(I_gray(1:5,1:5));
fprintf('\n运动模糊后左上角5x5像素值:\n');
disp(I_motion(1:5,1:5));
实现 “原图→运动模糊退化→添加高斯噪声→逆滤波(维纳滤波)复原” 流程,对比各阶段图像特征。
% 1. 读取图像并预处理
I_original = imread('CSGO.jpg');
% 判断是否为彩色图,转换为灰度图
if size(I_original, 3) == 3
I_gray = rgb2gray(I_original);
else
I_gray = I_original;
end
I_gray_double = im2double(I_gray); % 转换为double类型便于运算
% 2. 图像退化(运动模糊)
PSF_blur = fspecial('motion', 15, 30); % 运动长度15,角度30度
I_blurred = imfilter(I_gray_double, PSF_blur, 'circular', 'conv');
% 3. 添加高斯噪声
noise_mean = 0; % 噪声均值
noise_var = 0.001; % 噪声方差
I_noisy = imnoise(I_blurred, 'gaussian', noise_mean, noise_var);
% 4. 逆滤波(维纳滤波)复原
NSR = noise_var / var(I_gray_double(:)); % 噪声信号比(用于维纳滤波参数)
I_restored = deconvwnr(I_noisy, PSF_blur, NSR); % 维纳滤波复原
% 5. 可视化结果
figure('Name', '逆滤波复原实验', 'NumberTitle', 'off', 'Position', [100, 100, 1200, 600]);
subplot(2,2,1); imshow(I_original); title('原图');
subplot(2,2,2); imshow(I_blurred); title('退化图像');
subplot(2,2,3); imshow(I_noisy); title('加噪图像');
subplot(2,2,4); imshow(I_restored); title('逆滤波复原');
仅通过运动模糊退化图像,使用维纳滤波实现复原,对比原图、模糊图与恢复图。
% 1. 读取图像并预处理
I_rgb = imread('阿狸2.jpg');
I_gray = rgb2gray(I_rgb); % 彩色转灰度
I_gray = im2double(I_gray); % 转为double类型
% 2. 图像退化(运动模糊)
len = 15; % 运动模糊长度
theta = 30; % 运动角度
PSF = fspecial('motion', len, theta); % 生成点扩散函数
I_blurred = imfilter(I_gray, PSF, 'conv', 'circular'); % 应用模糊
% 3. 维纳滤波复原
noise_var = 0.001; % 假设微小噪声方差(实际无额外噪声,用于稳定算法)
SNR = var(I_gray(:)) / noise_var; % 信号噪声比
I_restored = deconvwnr(I_blurred, PSF, 1/SNR); % 维纳滤波
% 4. 可视化结果
figure('Name', '维纳滤波复原(无额外噪声)', 'NumberTitle', 'off');
subplot(1,3,1); imshow(I_gray); title('原图');
subplot(1,3,2); imshow(I_blurred); title('模糊后图像');
subplot(1,3,3); imshow(I_restored); title('恢复后图像');
通过高斯模糊退化图像(PSF 未知),使用deconvblind函数实现盲去卷积复原,无需已知退化函数。
% 1. 读取图像并预处理
I_rgb = imread('AALi.jpg');
I_gray = rgb2gray(I_rgb); % 彩色转灰度
I_gray = im2double(I_gray); % 转为double类型
% 2. 图像模糊处理(高斯模糊,PSF未知)
sigma = 1.5; % 高斯模糊标准差
PSF_true = fspecial('gaussian', 7, sigma); % 真实PSF(实验中模拟未知)
I_blurred = imfilter(I_gray, PSF_true, 'conv', 'same'); % 生成模糊图像
% 3. 盲去卷积复原(无需已知真实PSF)
PSF_init = ones(7); % 初始化猜测的PSF(简单全1矩阵)
[I_restored, PSF_estimated] = deconvblind(I_blurred, PSF_init); % 盲去卷积
% 4. 可视化结果
figure('Name', '盲去卷积复原', 'NumberTitle', 'off');
subplot(1,3,1); imshow(I_gray); title('原图');
subplot(1,3,2); imshow(I_blurred); title('模糊后图像');
subplot(1,3,3); imshow(I_restored); title('恢复后图像');
% 输出估计的PSF,观察与真实PSF的差异
fprintf('真实PSF(高斯模糊):\n');
disp(PSF_true);
fprintf('\n估计的PSF(盲去卷积输出):\n');
disp(PSF_estimated);
实验模块 | 核心函数 | 核心结论 |
|---|---|---|
图像退化 | fspecial、imfilter | 不同 PSF 对应不同退化效果(运动模糊有方向,圆盘模糊无方向) |
噪声添加 | imnoise | 噪声会加剧图像退化,降低复原难度 |
维纳滤波复原 | deconvwnr | 需已知 PSF 和 NSR,兼顾去噪与细节保留,效果稳定 |
盲去卷积复原 | deconvblind | 无需已知 PSF,通过迭代估计退化函数,适用于 PSF 未知场景 |
double类型,避免uint8溢出导致失真;imfilter使用circular(循环边界)可避免图像边缘出现黑边;通过本次实验,系统掌握了图像退化模型的建立与多种复原算法的实现,理解了不同算法的适用场景与参数调节技巧,为实际图像修复任务(如老照片修复、监控图像增强)提供了技术支撑。