如何在Matlab中使用先前发现的像素索引找到像素



我在矩阵中有一个点' a ',并且我已经能够在' a '的8连接邻域(值' 00 ')周围搜索特定的像素值(例如' 30 ')。'find'函数返回点'A'周围感兴趣的像素值的索引(例如'1')。

现在的问题是,我需要找到完全另一个像素(相同的值),这是连接到' 30 '的初始像素值使用从第一次搜索返回的索引信息。' 30 '的第二个像素值不在' A '的近邻中,而是在它的下一个近邻中。

目前的代码:

img = [20 20 20 20 20 20 20;
       20 30 20 20 20 20 20;
       20 20 30 20 20 20 20;
       40 40 10 00 20 20 20;
       40 40 10 40 40 40 40;
       40 10 10 40 40 40 40;
       10 10 10 40 40 40 40] 
[Arow, Acol]     = find(img==00)
AIdx             = sub2ind(size(img), Arow, Acol);
M                = size(img, 1);
neighbor_offsets = [-M-1, -M, -M+1, -1, 1, M-1, M, M+1];
%Compute index array of the immediate neighbors of ‘A’
Aneighbors       = bsxfun(@plus, AIdx, neighbor_offsets);
%search for pixel value of ‘30’ in the immediate neighbors of ‘A’
find(img(Aneighbors)==30)

代码的最后一行返回索引1。有可能用这个信息找到另外30个吗?

我知道我可以通过创建' A '(第二个' 30 '所在)的下一个近邻的另一个索引数组来轻松找到第二个像素值,如下所示:

    neigh_Aneighbor_offsets = [(-2*M)-2, (-2*M)-1, (-2*M), (-2*M)+1,(-2*M)+2, -M-2, -M+2, -2, 2, M-2, M+2, (2*M)-2, (2*M)-1, (2*M), (2*M)+1, (2*M)+2];
%Compute index array of the 2nd immediate neighbors of ‘A’
neigh_Aneighbors        = bsxfun(@plus, Aidx, neigh_Aneighbor_offsets);
%Search for the 2nd pixel value of ‘30’ in the next immediate neighborhood of ‘A’
find(img(neigh_Aneighbors)==30)

然而,我只想通过假设我除了已经找到的第一个"30"的位置之外什么都不知道来找到它。无论如何,我不知道该怎么做。如有任何帮助/建议/意见,我们将不胜感激。

find的结果保存到变量f中,从Aneighbors(f)中得到合适的Aidx:

f=find(img(Aneighbors)==30)
Aidx = Aneighbors(f)
Aneighbors=bsxfun(@plus, Aidx, neighbor_offsets)
f=find(img(Aneighbors)==30)

最新更新