使用nlfilter将中心像素与matlab中特定数量的邻域像素进行比较,如果条件满足,则将其设置为像素1

使用nlfilter将中心像素与matlab中特定数量的邻域像素进行比较,如果条件满足,则将其设置为像素1,matlab,compare,coordinates,nearest-neighbor,Matlab,Compare,Coordinates,Nearest Neighbor,我尝试制作一个函数,在矩阵(或灰度图像)上滑动一个7x7遮罩,并使用matlab中的nlfilter将中心像素与36个最近邻域像素进行比较 function [ b ] = Slide(A) b= nlfilter (A,[7 7],@maske); end function sammenligne = maske(A) t=5; g=18; center=A(25); %pixel in position 25 is the center pixel of the 7x7 matrix

我尝试制作一个函数,在矩阵(或灰度图像)上滑动一个7x7遮罩,并使用matlab中的nlfilter将中心像素与36个最近邻域像素进行比较

function [ b ] = Slide(A)

b= nlfilter (A,[7 7],@maske);

end

function sammenligne = maske(A)
t=5;
g=18;
center=A(25); %pixel in position 25 is the center pixel of the 7x7 matrix
neigh= A([3:1:5 9:1:13 15:1:24 26:135 37:1:41 45:1:47]); %these are the pixel I want to test. So I want to test pixel in position 3,4,5,15,16 so on in the 7x7 matrix
if abs(neigh-center)<=t
neigh=1;
else
neigh=0;
end

% if (nnz(neigh)>g)
% find position of center pixel 
% else
% 
% end
sammenligne=neigh;
end
函数[b]=幻灯片(A)
b=nlfilter(A,[7],@maske);
结束
函数sammenligne=maske(A)
t=5;
g=18;
中心=A(25);%位置25的像素是7x7矩阵的中心像素
neigh=A([3:1:59:1:13 15:1:24 26:135 37:1:41 45:1:47]);%这些是我想测试的像素。所以我想测试7x7矩阵中3,4,5,15,16等位置的像素
如果防抱死制动系统(neigh center)g)
%找到中心像素的位置
%否则
% 
%结束
sammenligne=嘶鸣;
结束
如果邻域像素和中心之间的差值的绝对值小于设定的阈值(t)。我想将测试像素设置为1,否则设置为0。 如果矩阵中的数字1大于另一个阈值(g),我想找到中心像素的位置,这样我可以绘制它


如果我在正确的道路上,以及我应该如何前进,我有点被卡住了。任何帮助都将不胜感激

首先,使用布尔标量而不是向量,并返回邻居数而不是1:

function N = maske(A)
t=5;
center=A(25); %pixel in position 25 is the center pixel of the 7x7 matrix
neigh= A([3:1:5 9:1:13 15:1:24 26:1:35 37:1:41 45:1:47]); %these are the pixel I want to test. So I want to test pixel in position 3,4,5,15,16 so on in the 7x7 matrix
N = nnz(abs(neigh-center)<=t);

首先,
abs(neigh center)你不想和48个最近的邻居而不是36个进行比较吗?使用7x7过滤器会产生49个值,如果你减去中间的值,剩下48个……谢谢你的回答@user2999345:)我不知道all或any函数,我会尝试一下。是否需要使用if函数中的任意/全部将所选像素设置为1(或0)?我更新了问题中的代码,看到我混合了两个代码。我想数一数面具上的1。所以我可以有0到36个1。不知道我是否说得更清楚。要回答Max,不,我只想测试特定的邻居,所以不想测试所有48个。我尝试运行它时出错。使用>=太多输出参数时出错。幻灯片(第8行)中的错误[yy,xx]=b>=g;你说得对,我忘了
查找(…)
。现在应该没事了,很抱歉再次打扰你。但是通过设置A=magic(7)尝试了它,这将生成一个7x7矩阵。面具不应该超过一次吗?并将其用作主功能的输入。当t=2时,我得到的1比我应该得到的多得多。它遗漏了什么吗?你是对的,它应该是
find(B>=g)
,而不是
find(B>=g)
关于输出大小-
nlfilter
在执行之前用零填充输入数组,这样输出将具有与输入相同的大小。您可以删除输入的3个(如果过滤器大小为[7])像素宽度边缘,以获得“有效”输出:
B=B(4:end-3,4:end-3)
B = nlfilter (A,[7 7],@maske);
b = B > 0;
g = 18;
[yy,xx] = find(B >= g);