我想知道有没有办法定义我自己的2D过滤器。假设存在矩阵A,我想将A的每个元素分配给其8个邻居的最大值。例如:
A = [1 2 3 4 5
6 7 8 9 10
-1 3 4 8 13
4 2 0 9 1]
过滤后:
A = [1 2 3 4 5
6 8 9 10 10
-1 8 9 13 13
4 2 0 9 1]
由于
答案 0 :(得分:1)
你可以使用imdilate
来获得9x9邻居的最大值 -
%%// Dilate to get the max of a 9x9 neighborhood (including the element itself)
A1 = imdilate(A, true(3));
%%// Since you are looking to keep the boundary elements
m1 = ones(size(A));
m1(2:end-1,2:end-1)=0;
A1 = m1.*A+~m1.*A1
如果您希望获得8x8邻居的最大值,即排除元素本身,请使用修改后的内核用于imdilate
-
%%// Dilate to get the max of a 8x8 neighborhood (excluding the element itself)
h = true(3);
h(2,2)=false;
A1 = imdilate(A,h);
%%// Since you are looking to keep the boundary elements
m1 = ones(size(A));
m1(2:end-1,2:end-1)=0;
A1 = m1.*A+~m1.*A1
答案 1 :(得分:1)
您可以使用nlfilter
创建一个任意过滤器,例如here。在你的情况下:
fun = @(x) max(x(:)),
B = nlfilter(YourImage,[3 3],fun);
如果您要排除中心像素,请将fun
替换为:
fun=@(x) max([x(1:3, 1)' x(1,2) x(3,2) x(1:3,3)'])
答案 2 :(得分:0)