| 242 | typename image_type |
| 243 | > |
| 244 | std::vector<point> find_peaks ( |
| 245 | const image_type& img_, |
| 246 | const double non_max_suppression_radius, |
| 247 | const typename pixel_traits<typename image_traits<image_type>::pixel_type>::basic_pixel_type& thresh |
| 248 | ) |
| 249 | { |
| 250 | DLIB_CASSERT(non_max_suppression_radius >= 0); |
| 251 | const_image_view<image_type> img(img_); |
| 252 | |
| 253 | using basic_pixel_type = typename pixel_traits<typename image_traits<image_type>::pixel_type>::basic_pixel_type; |
| 254 | |
| 255 | std::vector<std::pair<basic_pixel_type,point>> peaks; |
| 256 | |
| 257 | for (long r = 1; r+1 < img.nr(); ++r) |
| 258 | { |
| 259 | for (long c = 1; c+1 < img.nc(); ++c) |
| 260 | { |
| 261 | auto val = img[r][c]; |
| 262 | if (val < thresh) |
| 263 | continue; |
| 264 | |
| 265 | if ( |
| 266 | val <= img[r-1][c] || |
| 267 | val <= img[r+1][c] || |
| 268 | val <= img[r][c+1] || |
| 269 | val <= img[r][c-1] || |
| 270 | val <= img[r-1][c-1] || |
| 271 | val <= img[r+1][c+1] || |
| 272 | val <= img[r-1][c+1] || |
| 273 | val <= img[r+1][c-1] |
| 274 | ) |
| 275 | { |
| 276 | continue; |
| 277 | } |
| 278 | |
| 279 | peaks.emplace_back(val,point(c,r)); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | |
| 284 | // now do non-max suppression of the peaks according to the supplied radius. |
| 285 | using pt = std::pair<basic_pixel_type,point>; |
| 286 | // First sort the peaks so the strongest peaks come first. We will greedily accept |
| 287 | // them and then do the normal peak sorting/non-max suppression thing. |
| 288 | std::sort(peaks.rbegin(), peaks.rend(), [](const pt& a, const pt&b ){ return a.first < b.first; }); |
| 289 | std::vector<point> final_peaks; |
| 290 | const double radius_sqr = non_max_suppression_radius*non_max_suppression_radius; |
| 291 | |
| 292 | // If there are a lot of peaks then we will make a mask image and use that to do |
| 293 | // the non-max suppression since this is fast when peaks.size() is large. Otherwise we |
| 294 | // will do the simpler thing in the else block that doesn't require us to allocate a |
| 295 | // temporary mask image. |
| 296 | if (peaks.size() > 500 && radius_sqr != 0) |
| 297 | { |
| 298 | // hit will record which areas of the image have already been accounted for by some |
| 299 | // peak. So it is our mask image. |
| 300 | matrix<unsigned char> hit(img.nr(), img.nc()); |
| 301 | // initially nothing has been hit. |
no test coverage detected