• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

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