Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Border Tracing Algorithm

$
0
0
Hi guys,
I am a third year student and I'm trying to implement the border tracing algorithm in c++, to return different groups of pixels belong to different polygons, below is the description of algorithm from (thesis.lib.ncu.edu.tw/ETD-db/ETD-search/getfile?URN=87325052&filename=87325052.pdf):

Border Tracing Algorithm:
1. Set variable “dir.” to be 7. (8-connectivity).
2. Search the 3x3 neighborhood of the current pixel in an anti-clockwise direction, beginning the neighborhood search in the pixel positioned in the direction.
( dir + 7 ) mod 8 if dir is even
( dir + 6 ) mod 8 if dir is odd

new boundary element P n . Update the dir. value.
3. If the current boundary element is equal to the second border element
P 1 and if the previous border element P n−1 is equal to P 0 , stop, otherwise repeat step 2.
4. The detected inner border is represented by pixels PK 0 P n− 2


What I have done is getting the first element in the outer counter of each polygon, but now I need to move on the outer border of each polygon, from that starting point, I was wondering if there someone could helps me on how to modify the code below to move on the outer boundary of each square. (I guess I need to use while loop by somehow, but I didn't get it yet).
Also, there is another issue because what I have in my code is four squares, but when I run the code I got only the first elements of just two of those squares.

I would be glad to get some help on how to modify the code to solve those issues.

Regards,


#include <fstream>
#include <iostream>
#include <vector>
#include <stdlib.h>

using namespace std;

int main ()

{
int n= 512;
int offset= n/2;
int i , j , a ;
vector< vector<int> > squares(n, vector<int>(n, 0));

for (i = 0 ; i <n ; i++)
{
    for (j = 0 ; j <n ; j++)
    {
        if ( (i > offset +50) && (i < offset+100) && (j> offset+50) && (j < offset+100))
        squares[i][j] = 1;  
        
        if ( (i < offset -50) && (i > offset-100) && (j< offset-50) && (j > offset-100))
        squares[i][j] = 1;
        
        if ( (i < offset -50) && (i > offset-100) && (j> offset+50) && (j < offset+100))
        squares[i][j] = 1;
        
        if ( (i > offset +50) && (i < offset+100) && (j< offset-50) && (j > offset-100))
        squares[i][j] = 1;
    }
}

int dir_i [8] = {1, 1, 0, -1, -1, -1, 0, 1};
int dir_j [8] = {0, -1, -1, -1, 0, 1, 1, 1};  
vector< vector<int> > id(n, vector<int>(n, 0)); 

int c = 1 ;    
int i_first , j_first ; 
    
     for ( i = 0 ; i <n ; i++)
     {
        for ( j = 0; j<n ; j++)
        {
            if (id[i][j] == 0 ) 
            {
                if ( squares [i] [j] == 1)
                {   
                    if ( squares[i-1][j] ==0)
                    {
                        i_first = i ; 
                        j_first = j ;
                        cout << "first/" << i_first << " " << j_first << "\n"; 
                        int next_i , next_j;
                        int start_dir = 7;      
                        int update_dir;                      
                        for ( a = 0 ; a < 8 ; a++)
                        {
                            update_dir = (start_dir+a)%8;
                            i = i_first+dir_i[update_dir];
                            j = j_first+dir_j[update_dir];
                                                        
                            if (squares[i][j] == 1) 
                            {
                                next_i = i ;
                                next_j = j ;
                                if (update_dir%2 == 0) 
                                start_dir = (update_dir+7)%8;
                                else                    
                                start_dir = (update_dir+6)%8;  
                                break;
                            }                          
                         }
                         cout << "next/" << next_i << " " << next_j << "\n"; 
                         c++;                              
                }
            }
        }
    }
}
return 0; 
}

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>