| 86 | // ---------------------------------------------------------------------------------------- |
| 87 | |
| 88 | void pick_best_window_size ( |
| 89 | const std::vector<std::vector<rectangle> >& boxes, |
| 90 | unsigned long& width, |
| 91 | unsigned long& height, |
| 92 | const unsigned long target_size |
| 93 | ) |
| 94 | /*! |
| 95 | ensures |
| 96 | - Finds the average aspect ratio of the elements of boxes and outputs a width |
| 97 | and height such that the aspect ratio is equal to the average and also the |
| 98 | area is equal to target_size. That is, the following will be approximately true: |
| 99 | - #width*#height == target_size |
| 100 | - #width/#height == the average aspect ratio of the elements of boxes. |
| 101 | !*/ |
| 102 | { |
| 103 | // find the average width and height |
| 104 | running_stats<double> avg_width, avg_height; |
| 105 | for (unsigned long i = 0; i < boxes.size(); ++i) |
| 106 | { |
| 107 | for (unsigned long j = 0; j < boxes[i].size(); ++j) |
| 108 | { |
| 109 | avg_width.add(boxes[i][j].width()); |
| 110 | avg_height.add(boxes[i][j].height()); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // now adjust the box size so that it is about target_pixels pixels in size |
| 115 | double size = avg_width.mean()*avg_height.mean(); |
| 116 | double scale = std::sqrt(target_size/size); |
| 117 | |
| 118 | width = (unsigned long)(avg_width.mean()*scale+0.5); |
| 119 | height = (unsigned long)(avg_height.mean()*scale+0.5); |
| 120 | // make sure the width and height never round to zero. |
| 121 | if (width == 0) |
| 122 | width = 1; |
| 123 | if (height == 0) |
| 124 | height = 1; |
| 125 | } |
| 126 | |
| 127 | // ---------------------------------------------------------------------------------------- |
| 128 | |