下面的代码进行复杂化%高志远 2220202262
% 读取图像
img = imread('GZY.jpg');
% 转换为灰度图像
grayImg = rgb2gray(img);
Img = img;
% 设置参数
width = size(grayImg, 2);
height = size(grayImg, 1);
smooth_type = 'SMOOTH_GS';
smooth_mask_width = 4;
smooth_mask_height = 4;
gaussian_sigma = 1;
k = 1.5;
% 应用Unsharp Masking算法
dst_orginal = UnsharpMasking(double(Img), width, height, smooth_type, smooth_mask_width, smooth_mask_height, gaussian_sigma, k);
dst = UnsharpMasking(double(grayImg), width, height, smooth_type, smooth_mask_width, smooth_mask_height, gaussian_sigma, k);
% 显示原始图像和处理后的图像
figure;
subplot(2, 2, 1);
imshow(Img);
title('原始图像');
subplot(2, 2, 4);
imshow(uint8(dst));
title('处理后的图像');
subplot(2,2,3);
imshow(uint8(dst_orginal));
title('灰度处理后');
subplot(2,2,2);
imshow(grayImg);
title('Unsharp Masking');
function dst = UnsharpMasking(src, width, height, smooth_type, smooth_mask_width, smooth_mask_height, gaussian_sigma, k)
temp = zeros(width, height);
switch smooth_type
case 'SMOOTH_GS'
temp = GaussianFilter(src, smooth_mask_width, smooth_mask_height, gaussian_sigma);
case 'SMOOTH_M'
temp = MeanFilter(src, smooth_mask_width, smooth_mask_height);
otherwise
error('Invalid smooth_type');
end
% 计算差值图像
dst = src - temp;
% 缩放差值图像
dst = dst * k;
% 添加差值图像到原始图像
dst = src + dst;
end
function smoothed = GaussianFilter(src, mask_width, mask_height, sigma)
% 创建高斯滤波掩模
mask = fspecial('gaussian', [mask_width, mask_height], sigma);
% 进行滤波操作
smoothed = imfilter(src, mask);
end
function smoothed = MeanFilter(src, mask_width, mask_height)
% 创建均值滤波掩模
mask = ones(mask_width, mask_height) / (mask_width * mask_height);
% 进行滤波操作
smoothed = imfilter(src, mask);
end