📄 amedfilt2_roifun.m
字号:
function J = amedfilt2_roifun(I) %#eml
% 2-D Adaptive Median Filter
% This filter ignores edge effects and boundary conditions, as such, the
% output is a cropped version of the original image, where the amount
% cropped is equal to the maximum window size vertically and horizontally.
% Define smax as a constant
smax = 9;
% Initialize Output Image (J)
J = I;
% Calculate valid region limits for filter
[nrows ncols] = size(I);
ll = ceil(smax/2);
ul = floor(smax/2);
% Loop over the entire image ignoring edge effects
for rows = ll:nrows-ul
for cols = ll:ncols-ul
window_ind = -ul:ul;
region = I(rows+window_ind,cols+window_ind);
centerpixel = region(ll,ll);
for s = 3:2:smax
% We need these 3 functions to operate on a defined region
% (ROI) within the window
rmin = roi_min(region,smax,s);
rmax = roi_max(region,smax,s);
rmed = roi_median(region,smax,s);
% adapt region size
if rmed > rmin && rmed < rmax
if centerpixel <= rmin || centerpixel >= rmax
J(rows,cols) = rmed;
end
% stop adapting
break;
end
end
end
end
function rmin = roi_min(region,smax,s)
% Limits for ROI
ll = ceil(smax/2)-floor(s/2);
ul = ceil(smax/2)+floor(s/2);
% Initialize minimum
rmin = region(ll,ll);
for i = ll:ul
for j = ll:ul
if region(i,j) < rmin
rmin = region(i,j);
end
end
end
function rmax = roi_max(region,smax,s)
% Limits for ROI
ll = ceil(smax/2)-floor(s/2);
ul = ceil(smax/2)+floor(s/2);
% Initialize maximum
rmax = region(ll,ll);
for i = ll:ul
for j = ll:ul
if region(i,j) > rmax
rmax = region(i,j);
end
end
end
function rmed = roi_median(region,smax,s)
% This is a bit complicated, we need to do a partial sort, or create a
% large vector with values out of range for the ROI and sort that
% vector.
% Limits for ROI
ll = ceil(smax/2)-floor(s/2);
ul = ceil(smax/2)+floor(s/2);
v = ones(smax*smax,1);
count = 1;
for i = ll:ul
for j = ll:ul
v(count) = region(i,j);
count = count+1;
end
end
% Lets sort vector v now, the elements we are interested in will be the
% fist s^2 elements. The median value is the mid point of the s^2 elements,
% which is a simple computation when s is odd.
v = sort(v);
rmed = v(ceil(s*s/2));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -