why the image processing usually use this eqution?
5 views (last 30 days)
Show older comments
w=ones(3,3);
for x=2:1:r-1
for y= 2:1:c-1
g=[w(1)*a(x-1,y-1) w(2)*a(x-1,y) w(3)*a(x-1,y+1)...
w(4)*a(x,y-1) w(5)*a(x,y) w(6)*a(x,y+1)...
w(7)*a(x+1,y-1) w(8)*a(x+1,y) w(9)*a(x+1,y+1)];
img(x,y)=min(g);
end
end
I don't understand why the many code usually use this equation like this
" w(1)*a(x-1,y-1) w(2)*a(x-1,y) w(3)*a(x-1,y+1)...
w(4)*a(x,y-1) w(5)*a(x,y) w(6)*a(x,y+1)...
w(7)*a(x+1,y-1) w(8)*a(x+1,y) w(9)*a(x+1,y+1) "
a is the imge, I don't know this part (x-1,y-1),(x-1,y).......
and how many w() can I use ?
0 Comments
Answers (1)
DGM
on 4 Jun 2021
Edited: DGM
on 4 Jun 2021
This example code implements a type of sliding filter. In this case, the filter size is defined by the filter kernel w (3x3). To be more specific, this is a minimizing filter; that is, it's erosion with a 3x3 square structuring element.
w=ones(3,3);
for x=2:1:r-1
for y= 2:1:c-1
g=[w(1)*a(x-1,y-1) w(2)*a(x-1,y) w(3)*a(x-1,y+1)...
w(4)*a(x,y-1) w(5)*a(x,y) w(6)*a(x,y+1)...
w(7)*a(x+1,y-1) w(8)*a(x+1,y) w(9)*a(x+1,y+1)];
img(x,y)=min(g);
end
end
At any given position, the result is a function of the kernel (w) and the region of the image upon which the filter is applied. The expression
[w(1)*a(x-1,y-1) w(2)*a(x-1,y) w(3)*a(x-1,y+1)...
w(4)*a(x,y-1) w(5)*a(x,y) w(6)*a(x,y+1)...
w(7)*a(x+1,y-1) w(8)*a(x+1,y) w(9)*a(x+1,y+1)]
is equivalent to
w.*a(y-1:y+1,x-1:x+1)
(or at least it would be if it didn't have x and y subscripts flipped in what I assume is an undiscovered error)
In other words, the kernel w is a weighting array, and g is the product of the image region under the kernel and the kernel weights. If this were an averaging filter, the values in w might be 1/9 instead of 1. That would make the sum of g the local average of the image.
In this case, so long as w is all 1, then g reduces to just a rectangular sample of the image at a given location
g = a(y-1:y+1,x-1:x+1);
and the only necessary information carried by w is its geometry, implicit in the subscript vectors used.
If a dilation/erosion routine needed to handle a non-rectangular kernel like this:
w =
0 0 1 0 0
0 1 1 1 0
1 1 1 1 1
0 1 1 1 0
0 0 1 0 0
Then the way g is calculated in your example would mean that g inherits a bunch of zeros from w, and min(g) would now be incorrect unless w is used differently in the calculation of g (as a logical mask, perhaps). On the other hand, erosion can still be done without making such a change to the calculation of g. Consider:
w = [0 0 1 0 0;0 1 1 1 0;1 1 1 1 1;0 1 1 1 0;0 0 1 0 0]; % some non-square kernel/strel
for row = 1:something % handwaving
for col = 1:somethingelse
% obviously the subscript ranges need to be
% a function of the size of w
g = w.*a(rowrange,colrange);
output(row,col) = 1-max(g(:)); % assuming normalized input
end
end
In other words, the zeros don't matter to a max filter (dilation). Erosion is just the inverse of dilation.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!