图像处理——引导均值滤波

引导均值滤波

%% guided_filtering.m
%功能:引导均值滤波

clc;
close all;

% 图像输入
I = imread('t3.bmp');
[R, C] = size(I);
figure(1), imshow(I), title('原图');

% CLAHE
I_clahe = adapthisteq(I);
figure(2), imshow(I_clahe), title('clahe-限制对比度自适应直方图均衡化-默认参数');

% 滤波
filter = fspecial('disk',3); 
I_disk = imfilter(I_clahe, filter, 'replicate');
figure(3), imshow(I_disk), title('滤波');

% 边缘检测
[I_edge, value]= edge(I_disk, 'canny');
figure(4), imshow(I_edge), title('边缘检测');

% 均值滤波
N = 3;%均值模块尺寸(奇数)
E = (N - 1) / 2;%均值模块加减值
I_aver = I_clahe;
% 图像遍历
for i = (1 + E) : (R - E)
    for j = (1 + E) : (C - E)
        sum = 0.0;%模块求和
        flag_edge = 0;%边缘标志
        %模板遍历
        for m = (i - E) : (i + E)
            for n = (j - E) : (j + E)
                if(I_edge(m, n) == 1)%边缘处,边缘标志置一
                    flag_edge = 1;
                end
                sum = sum + double(I_clahe(m, n));%模板求和
            end
        end
        if(flag_edge)%边缘处,不滤波
            I_aver(i, j) = I_clahe(i, j);
        else%非边缘处,滤波
            I_aver(i, j) = uint8(sum / double(N * N));
        end
    end
end
figure(5), imshow(I_aver), title('均值滤波');

猜你喜欢

转载自blog.csdn.net/family5love/article/details/110235736