用低通滤波器实现在频域的锐化处理的两种matlab代码
时间: 2024-02-05 21:13:53 浏览: 33
以下是两种 MATLAB 代码实现在频域的锐化处理,其中使用了低通滤波器:
方法一:
```matlab
% 读入图像
img = imread('lena.jpg');
% 转换为灰度图像
gray_img = rgb2gray(img);
% 将图像转换为双倍精度类型
gray_img = im2double(gray_img);
% 计算傅里叶变换
f = fft2(gray_img);
% 将零频率移到频谱的中心
fshift = fftshift(f);
% 设置低通滤波器
[M, N] = size(gray_img);
R = 20;
u = 0:(M-1);
v = 0:(N-1);
idx = find(u>M/2);
u(idx) = u(idx)-M;
idy = find(v>N/2);
v(idy) = v(idy)-N;
[V, U] = meshgrid(v, u);
D = sqrt(U.^2 + V.^2);
H = double(D <= R);
% 应用滤波器
g = fshift .* H;
% 傅里叶逆变换
gshift = ifftshift(g);
filtered_img = real(ifft2(gshift));
% 将图像的值缩放到0-255之间
filtered_img = uint8(255 * filtered_img / max(filtered_img(:)));
% 显示原始图像和处理后的图像
subplot(1,2,1),imshow(gray_img),title('Original Image');
subplot(1,2,2),imshow(filtered_img),title('Sharpened Image');
```
方法二:
```matlab
% 读入图像
img = imread('lena.jpg');
% 转换为灰度图像
gray_img = rgb2gray(img);
% 将图像转换为双倍精度类型
gray_img = im2double(gray_img);
% 计算傅里叶变换
f = fft2(gray_img);
% 将零频率移到频谱的中心
fshift = fftshift(f);
% 设置低通滤波器
[M, N] = size(gray_img);
R = 20;
H = fspecial('gaussian', [M N], R);
% 应用滤波器
g = fshift .* (1+5*H);
% 傅里叶逆变换
gshift = ifftshift(g);
filtered_img = real(ifft2(gshift));
% 将图像的值缩放到0-255之间
filtered_img = uint8(255 * filtered_img / max(filtered_img(:)));
% 显示原始图像和处理后的图像
subplot(1,2,1),imshow(gray_img),title('Original Image');
subplot(1,2,2),imshow(filtered_img),title('Sharpened Image');
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)