Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void calculate_adjacency() {
- //The computing of the adjacence for each region is the tricky part of the Split and merge algorithm.
- //After the split phase, we got into our regions vector all the splitted region of the image, of various size.
- //A good approach to find all the adjacence without mantaining a list of adjacence for every region, is to declare
- //a set STL container into each region object: with the property of the set, which is an associative container in
- //which each element is unique (cause the key of each element is the element itself) we can mantain a list of adjacence
- //for each point easily.
- //How?
- //We can start by sorting the regions vector from the smallest to the largest. Then, for each region, we need to find
- //his adjacent neighbor, identified in the Nord, Sud, East and West one (The diagonal are not adjacent).
- //With that said, how can we achieve that? Sorted the vector to the smallest to the largest, we extract the first region
- //from the vector and calculate the midpoint of each of his side orientation: Nord, sud, east, west, in form of a Point(x,y).
- //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.
- //NOTE: -1 should result, sometimes, still in the midpoint. For better results, add -2 or 2.
- //Before checking if a point belong to a certain region, we must check another thing: the current analyzed region, should be
- //smaller or equal to the region being extracted from the list. Thats because if a region is smaller the the current one, that
- //means that the region extracted from the list is splitted into various region aswell, so on a certain side we could have N neighbor
- //instead of just one. We left the calculation of the adjacency of the smaller region to the smaller region itself, because we can't
- //decide how many regions are on a single side. We must compare with JUST ONE, that is equal or bigger than the current region.
- //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.
- //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
- //push the current analyzed region into the adjacency list of the extracted region to respect the adjacency criteria.
- //Sort the regions vector inline
- sort(regions.begin(), regions.end(), [](const Region& r1, const Region& r2) { return r1.pixel_num < r2.pixel_num; });
- //aux tmp regions variables
- Region analyzed, extracted;
- //for the entire regions vector, extracting the current analyzed region
- for(int i=0; i<regions.size(); i++) {
- //analyzed is the region being analyzed to all the other region
- analyzed = regions.at(i);
- //calculating the midpoint of the region: since we're operating on squared images, we can extract the midpoint in this way:
- //calculate the center of the square by subtracting, for each row/col: (endIndex - startIndex)/2, and the distance from the center
- //to one of his border: given the simmetry of the square, we can use this distance to move and shift alongside the border.
- //Note: remember that Point, Rect, Circle and all the drawning object represent on the x coordinate the columns and on the y the
- //rows.
- int row_cen = round((analyzed.area.height + analyzed.area.y)/2);
- int col_cen = round((analyzed.area.width + analyzed.area.x)/2);
- Point c(col_cen, row_cen);
- //Calculate the distance from a side (doesn't matter who: is symmetric) to the center. We choose the rows to do this.
- //We then do: (center.x/y - start_row / start_cols)
- //For example, if the region is a square from row/col 90 to row/col 180, the center is located in (135,135).
- //We calculate the distance for the row as: (Center.y - region.area.y).
- //Where area is the Rect object of the region that holds the coordinate inverted as explained before.
- //Then the calculus is: (135 - 90) -> 45. The distance from the center to any of his side is 45.
- int distance = (c.y - analyzed.area.y);
- //Building the midpoint:
- //Nord: the midpoint on the nord side relay on the same column of the center, but on the start row of the region.
- //We subtract -2 from the row to shift from the current region to the nord one.
- //So we use the same columns as the center, and the center row (135) minus the distance (45) -> 90: the start row.
- //if exists. We still consider x as columns and y as row.
- Point N(c.x, (c.y - distance)-2);
- //East: the midpoint on the east side relay on the same row of the center, but on the start column of the region.
- //We subtract -2 to shift the column from the current region to the east one.
- //So we use the same row of the center and the center column (135) minus the distance (45) -> 90: the start col
- Point E((c.x - distance)-2, c.y);
- //Sud: the midpoint on the south side relay on the same column of the center, but on the end row of the region.
- //We add +2 to the row to shift from the current region to the south one.
- //So we use the same column as the center, and the center row(135) plus the distance(45) -> 180.
- Point S(c.x, (c.y + distance)+2);
- //West: the midpoint on the west side relay on the same row of the center, but on the end col of the region.
- //We add +2 to the col to shift from the current region to the west one.
- //So we use the same row as the center, and the center column(135) plus the distance(45) -> 180.
- Point W((c.x + distance)+2, c.y);
- //for the entire regions vector, extracting a region to compare with the current analyzed region from the vector
- for(int j=0; j<regions.size(); j++) {
- //if j == i that means we're analyzing the same region being analyzed. skip this iteration
- if(j==i) continue;
- //extracted is the i-th region extracted to compare to the analyzed region
- extracted = regions.at(j);
- //check if the analyzed region is smaller or equal than the extracted region
- if(analyzed.pixel_num <= extracted.pixel_num) {
- //check if the extracted region contains any of the N,S,E,W point: if yes, the region are adjacent.
- if(extracted.contains(N) || extracted.contains(S) || extracted.contains(E) || extracted.contains(W)) {
- //The extracted region contains one of the N,S,E,W point: it's adjacent to the analyzed region.
- //Push back the region for both the analyzed and extracted region
- regions.at(i).adjacency.insert(extracted);
- regions.at(j).adjacency.insert(analyzed);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement