2012-06-19 73 views
0

我在這裏通過郵政編碼爲什麼我碰到,而探索一種技術。問題sub2ind和矩陣矩陣matlab與圖像

Y = repmat((1:m)', [1 n]); 
    X = repmat(1:n, [m 1]) - labels_left; 
    X(X<1) = 1; 
    indices = sub2ind([m,n],Y,X); 

    final_labels = labels_left; 
    final_labels(abs(labels_left - labels_right(indices))>=1) = -1; 

在上面的代碼標籤左邊是單通道圖像。[m n]是該圖像的大小。我想知道這個sub2ind如何工作上面code.And蔭也面臨着問題,其中包含

labels_right(indices) 

最後聲明一下上面的表達式計算to.Here標籤權也是圖像

+1

如果你解釋代碼應該做什麼,它可能會有所幫助?第一部分看起來像MESHGRID我認爲 – Amro

+0

@amro是的第一部分是網格。這是代碼的一部分,當我想在立體聲通信中去除遮擋時。我懷疑的是矩陣的labels_right(indices)矩陣也是如何評估的,我編輯了我的代碼,你可以檢查 – nbsrujan

回答

1

也許小例子可以幫助理解:

%# image matrix 
M = rand(4,3) 
[m n] = size(M) 

%# meshgrid, and convert to linear indices 
[X,Y] = meshgrid(1:n,1:m) 
indices = sub2ind([m,n],Y,X) 

%# extract those elements 
M(indices) 

矩陣M:

>> M 
M = 
     0.95717  0.42176  0.65574 
     0.48538  0.91574  0.035712 
     0.80028  0.79221  0.84913 
     0.14189  0.95949  0.93399 

所述的(X,Y)網格座標的所有點組成:

>> X,Y 
X = 
    1  2  3 
    1  2  3 
    1  2  3 
    1  2  3 
Y = 
    1  1  1 
    2  2  2 
    3  3  3 
    4  4  4 

轉換爲線性指數:

>> indices 
indices = 
    1  5  9 
    2  6 10 
    3  7 11 
    4  8 12 

然後我們索引到使用這些索引的矩陣。

>> M(indices) 
ans = 
     0.95717  0.42176  0.65574 
     0.48538  0.91574  0.035712 
     0.80028  0.79221  0.84913 
     0.14189  0.95949  0.93399 

請注意:M(indices(i,j)) = M(Y(i,j)),X(i,j))