改进的matlab极值中值滤波程序
时间: 2023-08-01 15:07:52 浏览: 118
以下是改进的Matlab极值中值滤波程序:
```matlab
function y = medfilt2_extremes(x, w, p)
% x: input image
% w: window size, should be odd
% p: percentage of pixels to be replaced by median or max/min values
if nargin < 3
p = 0.1;
end
if mod(w, 2) == 0
error('Window size should be odd.');
end
[xh, xw] = size(x);
y = zeros(xh, xw);
% pad the image with zeros
pad_width = (w - 1) / 2;
x = padarray(x, [pad_width, pad_width], 'symmetric');
for i = 1:xh
for j = 1:xw
% get the window
window = x(i:i+w-1, j:j+w-1);
% sort the window values
window_sorted = sort(window(:));
% get the median value and the max/min values
mid = window_sorted((w*w+1)/2);
max_val = window_sorted(w*w - floor(w*w*p/2));
min_val = window_sorted(floor(w*w*p/2)+1);
% replace the pixel with the median value if it's an extreme value
if x(i,j) == max(window(:)) || x(i,j) == min(window(:))
y(i,j) = mid;
else
y(i,j) = x(i,j);
end
% replace a percentage of pixels with max/min values
if rand < p
if x(i,j) < min_val || x(i,j) > max_val
y(i,j) = min_val + (max_val - min_val) * rand;
end
end
end
end
y = uint8(y);
```
这个程序可以进行中值和极值滤波,并且可以选择替换一定比例的像素值为极值。其中,输入参数`x`是要进行滤波的图像,`w`是窗口大小,`p`是替换像素的比例,默认值为0.1。输出参数`y`是滤波后的图像。
阅读全文