【发布时间】:2016-10-04 14:29:28
【问题描述】:
我在矩阵中有一个点“A”,并且我已经能够在“A”的 8 连通邻域(值为“00”)周围搜索特定的像素值(比如“30”)。 “查找”函数返回点“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”的位置之外我什么都不知道来找到这个。 无论如何,我不知道该怎么做。任何帮助/建议/建议都将不胜感激。
【问题讨论】:
-
您是否尝试在第一个结果的 8 连通邻域中进行搜索?即如果第二个'30'位于(2,5),你还想找到它吗?另外我认为你想要
find(img(Aneighbors)==30)而不是find(img(Aneighbors==30))。 -
是的!我想在中心 00 的第二个邻域中找到所有可能的 30,而且你对“find(img(Aneighbors)==30)”是正确的。我猜有些疏忽。谢谢。
-
我仍然不太确定您要做什么。您刚刚描述的内容与找到“另一个像素......连接到'30'的初始像素值”并不完全相同。您所说的“中心00的第二个邻域”并不取决于第一个'30'的位置。
-
我的错!在这一点上,你可以想象我脑子里有这么多的排列。 (2,5) 的像素位置未连接到 (3,3) 的像素位置,因此我对您的问题的回答应该是“否”。我只需要找到与 (3,3) 处的第一个连接的所有可能的 30,这是我不想使用第二个邻域“neigh_Aneighbors”的主要原因。希望现在更清楚了吗?