代码如下:
主函数:
% 输入图像
img_1 = imread('Fig0334(a)(hubble-original).tif');
[r, c] = size(img_1);
% 滤波器模板
m = 15;
% 作用看我上一篇博客
img_1s = m_10_Smooth_create_img(img_1, r, c, m - 1);
img_2 = m_10_Smooth_process_img(img_1s, r, c, m);
% 找到图像中的最大灰度级
max_level = m_11_Smooth_Apply_find_max_level(img_2);
% 阈值
threshold = double(max_level) * 0.25;
% 阈值处理
img_3 = m_11_Smooth_Apply_threshold_process(img_2, threshold);
subplot(131), imshow(img_1);
subplot(132), imshow(img_2);
subplot(133), imshow(img_3);
m_11_Smooth_Apply_find_max_level.m 代码:
function level = m_11_Smooth_Apply_find_max_level(img)
% 该函数返回一张图像的最高灰度值
[r, c] = size(img);
level = 0;
for x = 1 : r
for y = 1 : c
if img(x, y) > level
level = img(x, y);
end
end
end
end
m_11_Smooth_Apply_threshold_process.m 代码:
function img_1 = m_11_Smooth_Apply_threshold_process(img_1, threshold)
% 用threshold阈值,处理img_1
[r, c] = size(img_1);
img_1 = double(img_1);
for x = 1 : r
for y = 1 : c
if img_1(x, y) >= threshold
img_1(x, y) = 1;
else
img_1(x, y) = 0;
end
end
end
end
实验结果如下: