2016-10-03 127 views
0

我在矩陣中有一個點'A',並且我能夠搜索8連接周圍的特定像素值(比如'30') 'A'(值爲'00')的鄰域。 'find'函數返回'A'點周圍感興趣像素值的索引(比如'1')。如何使用以前在Matlab中找到的像素索引找到像素

現在的問題是,我需要找到正確的另一個像素(具有相同的值),它是連接到'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'的位置之外我什麼也不知道。在任何情況下,我都不知道如何去解決這個問題。任何幫助/建議/建議非常感謝。

+0

你們是不是在第一個結果的8連附近進行搜索嗎?即如果第二個'30'位於(2,5)處,您是否仍然想要找到它?另外我想你想'find(img(Aneighbors)== 30)'而不是'find(img(Aneighbors == 30))'。 – KQS

+0

是的!我想在中心00的第二個鄰居中找到所有可能的30,再加上「find(img(Aneighbors)== 30)」。我猜想有些疏忽。謝謝。 – oma11

+1

我還不確定你想要做什麼。剛剛描述的與找到「連接到'30'的初始像素值的另一個像素」不完全相同。你所說的「中心00的第二個鄰居」不取決於第一'30'的位置。 – KQS

回答

1

只需保存的find結果給一個變量f並獲得適當的AidxAneighbors(f)

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

太棒了!感謝rahnema – oma11

相關問題