MCPcopy Create free account
hub / github.com/davisking/dlib / find_peaks

Function find_peaks

dlib/image_processing/scan_image.h:244–353  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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.

Callers 2

py_find_peaksFunction · 0.85
py_find_peaks2Function · 0.85

Calls 15

roundFunction · 0.85
get_rectFunction · 0.85
centered_rectFunction · 0.85
partition_pixelsFunction · 0.85
emplace_backMethod · 0.80
rbeginMethod · 0.80
rendMethod · 0.80
pointClass · 0.50
length_squaredFunction · 0.50
nrMethod · 0.45
ncMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected