Advertisement
Guest User

Untitled

a guest
May 26th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.52 KB | None | 0 0
  1. void calculate_adjacency() {
  2.  
  3.     //The computing of the adjacence for each region is the tricky part of the Split and merge algorithm.
  4.     //After the split phase, we got into our regions vector all the splitted region of the image, of various size.
  5.     //A good approach to find all the adjacence without mantaining a list of adjacence for every region, is to declare
  6.     //a set STL container into each region object: with the property of the set, which is an associative container in
  7.     //which each element is unique (cause the key of each element is the element itself) we can mantain a list of adjacence
  8.     //for each point easily.
  9.     //How?
  10.     //We can start by sorting the regions vector from the smallest to the largest. Then, for each region, we need to find
  11.     //his adjacent neighbor, identified in the Nord, Sud, East and West one (The diagonal are not adjacent).
  12.     //With that said, how can we achieve that? Sorted the vector to the smallest to the largest, we extract the first region
  13.     //from the vector and calculate the midpoint of each of his side orientation: Nord, sud, east, west, in form of a Point(x,y).
  14.     //We then add -1 or +1 to the rows or cols, based on which side we're facing, to retrieve a point of another region.
  15.     //NOTE: -1 should result, sometimes, still in the midpoint. For better results, add -2 or 2.
  16.     //Before checking if a point belong to a certain region, we must check another thing: the current analyzed region, should be
  17.     //smaller or equal to the region being extracted from the list. Thats because if a region is smaller the the current one, that
  18.     //means that the region extracted from the list is splitted into various region aswell, so on a certain side we could have N neighbor
  19.     //instead of just one. We left the calculation of the adjacency of the smaller region to the smaller region itself, because we can't
  20.     //decide how many regions are on a single side. We must compare with JUST ONE, that is equal or bigger than the current region.
  21.     //Then If the resultant point BELONG to one of the neighbor region (N,S,E,W), then the region is adjacent to the current region analyzed.
  22.     //We then proceed to push both the region extracted from the list into the current analyzed region adjacency list, and also we're going to
  23.     //push the current analyzed region into the adjacency list of the extracted region to respect the adjacency criteria.
  24.  
  25.     //Sort the regions vector inline
  26.     sort(regions.begin(), regions.end(), [](const Region& r1, const Region& r2) { return r1.pixel_num < r2.pixel_num; });
  27.  
  28.     //aux tmp regions variables
  29.     Region analyzed, extracted;
  30.  
  31.     //for the entire regions vector, extracting the current analyzed region
  32.     for(int i=0; i<regions.size(); i++) {
  33.    
  34.         //analyzed is the region being analyzed to all the other region
  35.         analyzed = regions.at(i);
  36.        
  37.         //calculating the midpoint of the region: since we're operating on squared images, we can extract the midpoint in this way:
  38.         //calculate the center of the square by subtracting, for each row/col: (endIndex - startIndex)/2, and the distance from the center
  39.         //to one of his border: given the simmetry of the square, we can use this distance to move and shift alongside the border.
  40.         //Note: remember that Point, Rect, Circle and all the drawning object represent on the x coordinate the columns and on the y the
  41.         //rows.
  42.         int row_cen = round((analyzed.area.height + analyzed.area.y)/2);
  43.         int col_cen = round((analyzed.area.width + analyzed.area.x)/2);
  44.         Point c(col_cen, row_cen);
  45.        
  46.        
  47.         //Calculate the distance from a side (doesn't matter who: is symmetric) to the center. We choose the rows to do this.
  48.         //We then do: (center.x/y - start_row / start_cols)
  49.         //For example, if the region is a square from row/col 90 to row/col 180, the center is located in (135,135).
  50.         //We calculate the distance for the row as: (Center.y - region.area.y).
  51.         //Where area is the Rect object of the region that holds the coordinate inverted as explained before.
  52.         //Then the calculus is: (135 - 90) -> 45. The distance from the center to any of his side is 45.
  53.         int distance = (c.y - analyzed.area.y);
  54.                
  55.         //Building the midpoint:
  56.         //Nord: the midpoint on the nord side relay on the same column of the center, but on the start row of the region.
  57.         //We subtract -2 from the row to shift from the current region to the nord one.
  58.         //So we use the same columns as the center,  and the center row (135) minus the distance (45) -> 90: the start row.
  59.         //if exists. We still consider x as columns and y as row.
  60.         Point N(c.x, (c.y - distance)-2);
  61.        
  62.         //East: the midpoint on the east side relay on the same row of the center, but on the start column of the region.
  63.         //We subtract -2 to shift the column from the current region to the east one.
  64.         //So we use the same row of the center and the center column (135) minus the distance (45) -> 90: the start col
  65.         Point E((c.x - distance)-2, c.y);
  66.        
  67.         //Sud: the midpoint on the south side relay on the same column of the center, but on the end row of the region.
  68.         //We add +2 to the row to shift from the current region to the south one.
  69.         //So we use the same column as the center, and the center row(135) plus the distance(45) -> 180.
  70.         Point S(c.x, (c.y + distance)+2);
  71.        
  72.         //West: the midpoint on the west side relay on the same row of the center, but on the end col of the region.
  73.         //We add +2 to the col to shift from the current region to the west one.
  74.         //So we use the same row as the center, and the center column(135) plus the distance(45) -> 180.
  75.         Point W((c.x + distance)+2, c.y);
  76.        
  77.         //for the entire regions vector, extracting a region to compare with the current analyzed region from the vector
  78.         for(int j=0; j<regions.size(); j++) {
  79.             //if j == i that means we're analyzing the same region being analyzed. skip this iteration
  80.             if(j==i) continue;
  81.            
  82.             //extracted is the i-th region extracted to compare to the analyzed region
  83.             extracted = regions.at(j);
  84.                    
  85.             //check if the analyzed region is smaller or equal than the extracted region
  86.             if(analyzed.pixel_num <= extracted.pixel_num) {
  87.                 //check if the extracted region contains any of the N,S,E,W point: if yes, the region are adjacent.
  88.                 if(extracted.contains(N) || extracted.contains(S) || extracted.contains(E) || extracted.contains(W)) {
  89.                     //The extracted region contains one of the N,S,E,W point: it's adjacent to the analyzed region.
  90.                     //Push back the region for both the analyzed and extracted region
  91.                     regions.at(i).adjacency.insert(extracted);
  92.                     regions.at(j).adjacency.insert(analyzed);
  93.                 }  
  94.             }
  95.         }  
  96.        
  97.     }
  98.    
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement