20 篇文章 0 订阅
17 篇文章 0 订阅

# Matlab实现图像滤波（叁）：高效的中值滤波

## 0 背景介绍

Matlab实现图像滤波（贰）中介绍到了有关中值滤波的相关知识。

% imgOrg = rgb2gray(imread("peppers.png"));
imgOrg = imnoise(imgOrg, 'salt & pepper'); % Adding salt-pepper noise
subplot(1,3,1);
imshow(imgOrg);
title("Originall img");

% median filter
imgMedian = medianfilter(imgOrg, 3); % Generally, the size of a filter is an odd num
subplot(1,3,2);
imshow(imgMedian);
title("Median filter img");

% Using Matlab API
subplot(1,3,3);
[~, ~, c] = size(imgOrg);
newImg = zeros(size(imgOrg));
for i = 1:c
newImg(:,:,i) = medfilt2(imgOrg(:,:,i));
end
newImg = uint8(newImg);
imshow(newImg);
title("Medfilt img by API");

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function imgMedian = medianfilter(img,filterSize)
% img: image
% filterSize: if=5, it means 5*5

% Generate new blank image
[h, w, c] = size(img);
imgMedian = zeros([h, w, c]);

for k = 1:c
for i = 1:h
for j = 1:w
block = imgMedianT(i:i-1+filterSize, j:j-1+filterSize, k);
imgMedian(i, j, k) = median(block, 'all');
end
end
end

imgMedian = uint8(imgMedian);

end


Elapsed time is 0.895780 seconds.

## 1 高效的中值滤波算法

### Step 2

n m n_m 相当于当前中值的位置，例如下图 3 ∗ 3 3*3 滤波器窗口排序好后， 105 105 为中值，此时 n m = 5 n_m=5 ，即此时中值的位置为 5 5 号位置。

## 2 算法的Matlab实现

img = imnoise(imread("football.jpg"),'salt & pepper',0.1);
filterSize = 5; % odd
tic;
newImg = effMedian(img, filterSize);
toc;
subplot(1,2,1);
imshow(img);
title("Original Image");
subplot(1,2,2);
imshow(newImg);
title("Filtered Image");



function [resultImg] = effMedian(inputImg, fSize)
%EFFMEDIAN Summary of this function goes here
%   Detailed explanation goes here
%   inpuImg: input image
%   fSize: filter size x*x

fSize = [fSize, fSize];
[ih, iw, ic] = size(inputImg);     % height, width and channel of inputImg
ch = round((fSize(1)-1)/2);
cw = round((fSize(2)-1)/2);

tempImg = zeros([ih+ch*2, iw+cw*2, ic]);
tempImg(1+ch:ih+ch, 1+cw:iw+cw, :) = inputImg;
resultImg = zeros([ih, iw, ic]);

% step1
t = ceil(fSize(1)*fSize(2)/2);

for c = 1:ic
for h = 1+ch:ih+ch  % Step 9
w = 1+cw;
H = zeros([1,256]); % histogram
filterBlock = tempImg(h-ch:h+ch, w-cw:w+cw, c);
tempBlock = filterBlock(:);
for tmpi = 1:fSize(1)*fSize(2)
H(tempBlock(tmpi)+1) = H(tempBlock(tmpi)+1)+1;% +1：亮度0对应位置1
end
m = median(tempBlock);
n_m = length(find(tempBlock <= m));
resultImg(h-ch,w-1,c) = m;
for w = 2+cw:iw+cw  % Step 8
moveOut = tempImg(h-ch:h+ch,w-1-cw,c);
moveIn = tempImg(h-ch:h+ch,w+cw,c);
tempBlock = moveOut(:);
for tmpi = 1:fSize(1)  % Step 3
H(tempBlock(tmpi)+1) = H(tempBlock(tmpi)+1)-1;
end
n_m = n_m - length(find(tempBlock <= m)); % Step 3
tempBlock = moveIn(:);
for tmpi = 1:fSize(1)  % Step 4
H(tempBlock(tmpi)+1) = H(tempBlock(tmpi)+1)+1;
end
n_m = n_m + length(find(tempBlock <= m));  % Step 4
jmpFlag = n_m - t;                         % Step 5
if (jmpFlag < 0)                           % Step 6
while(n_m < t)
m = m + 1;
n_m = n_m + H(m+1);
end
elseif (jmpFlag > 0)                       % Step 7
while(n_m > t)
n_m = n_m - H(m+1);
m = m - 1;
end
end
resultImg(h-cw,w-cw,c) = m;
end
end
end

resultImg = uint8(resultImg);

end



## 3 结果验证

Elapsed time is 0.759880 seconds.

• 3
点赞
• 0
评论
• 19
收藏
• 一键三连
• 扫一扫，分享海报

04-07 6万+
12-04 1222
11-19 2万+
07-10 4万+
05-13 1万+
09-14 1万+
05-19
12-12
06-29
05-30
03-04
09-28 1952
02-16 5万+