matlab中频域滤波使图像慢慢变清晰的代码
时间: 2023-05-23 07:05:15 浏览: 80
A:
以下是使用matlab中频域滤波使图像慢慢变清晰的代码:
原始图像:
```matlab
I = imread('lena.jpg');
figure, imshow(I)
```
高斯低通滤波:
```matlab
%计算傅里叶变换并中心化
F = fftshift(fft2(I));
%定义滤波器
[M, N] = size(F);
H = fspecial('gaussian', [M N], 20);
H = fftshift(H);
%滤波
G = H.*F;
%计算傅里叶反变换并中心化
g = ifft2(ifftshift(G));
g = uint8(real(g));
figure, imshow(g)
```
中值滤波:
```matlab
g = medfilt2(I);
figure, imshow(g)
```
总变差滤波:
```matlab
g = TV_Denoising(I, 200, 1);
figure, imshow(g)
```
其中TV_Denoising是一个总变差滤波函数的实现:
```matlab
function [u, error] = TV_Denoising(u0, lambda, Nit)
u = u0;
[row, col] = size(u);
error = zeros(Nit, 1);
for i = 1:Nit
u_old = u;
u_x = [diff(u,1,2), u(:,1) - u(:,end)];
u_y = [diff(u,1,1); u(1,:) - u(end,:)];
normDu = sqrt(u_x.^2 + u_y.^2);
rho = (normDu < 1e-10) + normDu;
u_x = u_x./rho;
u_y = u_y./rho;
div_u = [diff(u_x,1,2); u_x(:,1)] + [diff(u_y,1,1), u_y(1,:)];
u = u - lambda*div_u;
error(i) = norm(u - u_old)/norm(u_old);
end
end
```
阅读全文