2014-05-22 23 views
-1

我以這種方式在3D圖像中實現了連接組件。3D圖像中的連接組件及其索引

int isSafe(unsigned char *M, int row, int col, int idz, bool *visited, int *size) 
{ 
    return (row >= 0) && (row < size[0]) && (col >= 0) && (col < size[1]) && (idz >= 0) && (idz < size[2]) && 
     (M[idz * size[0]*size[1] + row*size[0] + col] && !visited[idz*size[0]*size[1] + row*size[0] + col]); 
} 

void DFS3D(unsigned char *M, bool *visited, stack<int> &s1, stack<int> &s2, stack<int> &s3, vector < vector <int>> &index, int *size) 
{ 
    int count_components_elements = 0; 
    vector <int> indexes; 

    while(!s1.empty() && !s2.empty() && !s3.empty()) 
    { 
     int row, col, idz; 
     row = s1.top(); 
     s1.pop(); 
     col = s2.top(); 
     s2.pop(); 
     idz = s3.top(); 
     s3.pop(); 

     //add index positions to array or vector 
     indexes.push_back(count_components_elements); 
     indexes.at(count_components_elements)= (idz*size[0]*size[1] + (row)*size[0] + col); 
     ++count_components_elements; 

     static int rowNbr[] = {-1, 0, 1, 0, 0, 0}; 
     static int colNbr[] = { 0, 1, 0, -1, 0, 0}; 
     static int zNbr[] = { 0, 0, 0, 0, 1, -1}; 

     visited[idz*size[0]*size[1] + row*size[0] + col ] = true; 

     for (int k = 0; k < 6; ++k) 
     {   
     if (isSafe(M, row + rowNbr[k], col + colNbr[k], idz + zNbr[k], visited, size)) 
     {    
      { 
       s1.push(row + rowNbr[k]); 
       s2.push(col + colNbr[k]); 
       s3.push(idz + zNbr[k]); 
       visited[(idz + zNbr[k])*size[0]*size[1] + (row + rowNbr[k])*size[0] + col + colNbr[k]] = true; 
      } 
     } 
    } 
} 
index.push_back(indexes); 
} 


void FindLargestComponent_3D(unsigned char *M, vector < vector <int>> &index, int *size) 
{ 
    stack <int> s1; 
    stack <int> s2; 
    stack <int> s3; 
    bool *visited = new bool[size[0]*size[1]*size[2]]; 
    memset(visited, 0, sizeof(bool)*size[0]*size[1]*size[2]);  

    for (int k = 0; k < 1; ++k) 
    { 
     for (int i = 0; i < size[0]; ++i) 
     { 
     for (int j = 0; j < size[1]; ++j) 
     { 
      if (M[k*size[0]*size[1] + i*size[0] + j ] && !visited[k*size[0]*size[1] + i*size[0] + j ]) 
      {     
       s1.push(i); 
       s2.push(j); 
       s3.push(k); 
       DFS3D(M, visited, s1, s2, s3, index, size);        
      } 
     } 
    } 
} 
delete [] visited; 
} 

其中「M」是圖像和在各組分點的「指數」向量存儲索引。此代碼正常工作以查找最大的連接組件,但它沒有正確找到所有其他組件。我將它的輸出與matlab函數「bwlabeln」的輸出進行比較。

請檢查此代碼,並讓我知道如果我失去了一些東西。

+1

請參閱[如何調試小程序](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。 – jthill

回答

2

在你FindLargestComponent_3D功能,您可以通過kij變量與迭代:

for (int k = 0; k < 1; ++k) // Pay attention to this line!! 
{ 
    for (int i = 0; i < size[0]; ++i) 
    { 
     for (int j = 0; j < size[1]; ++j) 
     { 

這限制了搜索的是觸摸平面k=1連接組件。較小的連接組件不太可能在k=1平面上具有元素,因此它們也不太可能被發現。

要通過在K尺寸所有平面迭代,以取代第一這些行:

for (int k = 0; k < size[2]; ++k) 

在未來,你可能想使用「迭代器模式」,以避免這個錯誤。

+0

非常感謝。現在它工作正常。 – Gaurav

+0

@Gaurav太棒了!還要記住在你的代碼中爲自己和其他人寫更多的評論。最後,如果這回答您的問題,請將其標記爲已接受。 ;) – Ryan

+1

當然,現在我會寫更多的評論和回答接受;) – Gaurav