2017-03-18 62 views
0

我正在嘗試使用洪泛填充的隊列實現來填充輪廓之外的所有內容。不過,我有一些問題推斷出要添加到隊列中的起點。在C++中找不到FloodFill實現的正確startvalue

我定義了一個名爲tmpSlice的400 * 400的新double,並用零填充。然後我映射標籤值爲1.0的輪廓。這部分工作得很好。

,會出現問題時,我必須起點推到隊列最好的情況是推左上角或點0

但是我無法推送0爲起點沒有得到一個分段故障。我目前推160000這是tmpSlice的大小這是我已經能夠插入沒有錯誤的唯一號碼。 然而,當我將160000推到隊列上並且在洪水填充執行後,我運行從0-160000運行的循環(在代碼的底部)。

此循環應將所有值爲0和1的所有顏色都作爲一個顏色,所有值爲2的所有顏色都應爲零。但目前tmpSlice裏面一切都值0,除了擁有價值1

爲了澄清2表示輪廓外的原始輪廓,1代表輪廓的邊界,0表示輪廓

裏面什麼所以基本上,填充填充對我的數據集tmpSlice不起作用。我認爲這是因爲插入隊列的起始點,但我一直無法插入任何值的工作。

想法? PS。 我僅限於使用新的double,因爲vector :: std不能與我正在使用的minc庫中的一些函數一起使用。

 /* Flood fill */ 
    //Colour we are looking for as background 
    int TargetColour = 0.0; 
    //The new colour we will write 
    int NewColour = 2.0; 
    //create set to secure unique entries into the queue, otherwise we etc. insert the 9th element in a 3x3 array 6 times. 
    set <int> Set; 
    //Create queue 
    queue <int> MyQue; 
    //Insert first point into the queue 
    MyQue.push(sizes[1]*sizes[2]); 
    int Node; 
    //While loop for iterating over the nodes. 
    while (!MyQue.empty()){ 
     //Set front element to Node, and pop the front element from queue 
     Node = MyQue.front(); 
     MyQue.pop(); 

     //Change the colour to newcolour 
     tmpSlice[Node] = NewColour; 
     //Define the Node directions 
     int WestNode = Node-1; 
     int EastNode = Node+1; 


     //sizes are the lengths x,y 
     int NorthNode = Node-sizes[1]; 
     int SouthNode = Node+sizes[2]; 

     //Boundary checks 
     EastNodeBoundaryCheck = floor((Node-sizes[1]*sizes[2]*floor(Node/(sizes[1]*sizes[2])))/sizes[1]) == floor((EastNode-sizes[1]*sizes[2]*floor(EastNode/(sizes[1]*sizes[2])))/sizes[1]); 
     SouthNodeBoundaryCheck = floor(Node/(sizes[1]*sizes[2])) == floor(SouthNode/(sizes[1]*sizes[2])); 
     WestNodeBoundaryCheck = floor((Node-sizes[1]*sizes[2]*floor(Node/(sizes[1]*sizes[2])))/sizes[1]) == floor((WestNode-sizes[1]*sizes[2]*floor(WestNode/(sizes[1]*sizes[2])))/sizes[1]); 
     NorthNodeBoundaryCheck = floor(Node/(sizes[1]*sizes[2])) == floor(NorthNode/(sizes[1]*sizes[2])); 


     //East Node 
     if (Set.insert(EastNode).second) { 
      if (tmpSlice[EastNode] == TargetColour && EastNodeBoundaryCheck == 1){ 
       MyQue.push(EastNode); 
      } 
      } 

      //South Node 
     if (Set.insert(SouthNode).second) { 
      if (tmpSlice[SouthNode] == TargetColour && SouthNodeBoundaryCheck == 1){ 
       MyQue.push(SouthNode); 
      } 
     } 

     //West Node 
     if (Set.insert(WestNode).second) { 
      if (tmpSlice[WestNode] == TargetColour && WestNodeBoundaryCheck == 1){ 
       MyQue.push(WestNode); 
      } 
     } 

     //North Node 
     if (Set.insert(NorthNode).second) { 
      if (tmpSlice[NorthNode] == TargetColour && NorthNodeBoundaryCheck == 1){ 
       MyQue.push(NorthNode); 
       } 
     } 


    } 

    // Insert the colored points as 0 and everything else as 1 
    for(i = 0; i < sizes[1]*sizes[2]; i++){ 
     if(tmpSlice[i] == 0.0 || 1.0){ 
      slab[first_voxel_at_slice + i] = 1.0; 
     } 
     if(tmpSlice[i] == 2.0){ 
      slab[first_voxel_at_slice + i] = 0.0; 
     } 
} 
+0

什麼是尺寸類型[] []? – em2er

+0

@ em2er對不起我的壞。他們是int,在我的測試案例中,他們是400和400.我似乎可以插入0到起點,如果我刪除'tmpSlice [NorthNode] == TargetColour'。顯然我無法檢查'tmpSlice [-399]'的值,這實際上是有意義的。但是現在輪廓的底部消失了。 –

+0

考慮到Node的類型是int和size [] [],爲什麼你要使用類似'floor(Node /(sizes [1] * sizes [2]))''的表達式?如果int除以更大的int,結果始終爲0,否則結果將已經被定位。 floor是浮點值,它在使用它的方式上沒有意義。 – em2er

回答

0

,我發現自己的答案回答:-) 我需要檢查NorthNode如果第一行,以避免分段錯誤是大於0評論時。

  //North Node 
     if (Set.insert(NorthNode).second) { 
      if (NorthNode > 0){ 
       if (tmpSlice[NorthNode] == TargetColour && NorthNodeBoundaryCheck == 1){ 
         MyQue.push(NorthNode); 
        } 
      } 
     }