basic Weighted Median filter

is there a beginner method for this filter?
if:
I = [10, 200, 21; 7, 9, 10; 200, 20, 50];
and after adding zeros:
0 0 0 0 0
0 10 200 21 0
0 7 9 10 0
0 200 20 50 0
0 0 0 0 0
w = [1, 2, 1; 2, 3, 2; 1, 2, 1];
it should result in [0, 0, 0, 0, 0, 0, 10, 10, 10, 200, 200, 0, 7, 7, 9] for number 10 (2,2)
and so on for each number.
without using Matlab functions (so only by using For loops, If conditions and so on... )

1 Comment

i managed to write this:
I = [10, 200, 21; 7, 9, 10; 200, 20, 50];
[r, c] = size(I);
IZ = zeros(r + 2, c + 2);
IZ(2:end - 1, 2:end - 1) = I;
w = [1, 2, 1; 2, 3, 2; 1, 2, 1];
for n = 1:r
for m = 1:c
P = 1;
for i = 1:3
for j = 1:3
for t=1:w(i,j)
temp(P) = IZ(i+(n-1), j+(m-1));
P = P + 1;
end
end
end
temp = sort(temp);
O(n, m) = temp(round(length(temp)/2));
end
end
is there a way to simplify the loops more?

Sign in to comment.

Answers (0)

Products

Asked:

on 5 Mar 2021

Commented:

on 5 Mar 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!