From ca13ff7cc036f9407a0f5c2a26a61d427b98f0b6 Mon Sep 17 00:00:00 2001 From: Davis King Date: Sun, 15 Jul 2012 20:40:22 -0400 Subject: [PATCH] Refactored code and added find_points_above_thresh() --- dlib/image_processing/scan_image.h | 124 +++++++++----------- dlib/image_processing/scan_image_abstract.h | 31 +++++ 2 files changed, 84 insertions(+), 71 deletions(-) diff --git a/dlib/image_processing/scan_image.h b/dlib/image_processing/scan_image.h index fde2f74e4..f721a0a30 100644 --- a/dlib/image_processing/scan_image.h +++ b/dlib/image_processing/scan_image.h @@ -184,6 +184,57 @@ namespace dlib return static_cast(temp); } +// ---------------------------------------------------------------------------------------- + + template < + typename image_type + > + void find_points_above_thresh ( + std::vector >& dets, + const image_type& img, + const double thresh, + const unsigned long max_dets + ) + { + typedef typename image_type::type ptype; + + dets.clear(); + if (max_dets == 0) + return; + + unsigned long count = 0; + dlib::rand rnd; + for (long r = 0; r < img.nr(); ++r) + { + for (long c = 0; c < img.nc(); ++c) + { + const ptype val = img[r][c]; + if (val >= thresh) + { + ++count; + + if (dets.size() < max_dets) + { + dets.push_back(std::make_pair(val, point(c,r))); + } + else + { + // The idea here is to cause us to randomly sample possible detection + // locations throughout the image rather than just stopping the detection + // procedure once we hit the max_dets limit. So this method will result + // in a random subsample of all the detections >= thresh being in dets + // at the end of scan_image(). + const unsigned long random_index = rnd.get_random_32bit_number()%count; + if (random_index < dets.size()) + { + dets[random_index] = std::make_pair(val, point(c,r)); + } + } + } + } + } + } + // ---------------------------------------------------------------------------------------- template < @@ -217,9 +268,6 @@ namespace dlib #endif - dets.clear(); - if (max_dets == 0) - return; typedef typename image_array_type::type::type pixel_type; @@ -231,37 +279,7 @@ namespace dlib for (unsigned long i = 0; i < rects.size(); ++i) sum_filter(images[rects[i].first], accum, rects[i].second); - unsigned long count = 0; - dlib::rand rnd; - for (long r = 0; r < accum.nr(); ++r) - { - for (long c = 0; c < accum.nc(); ++c) - { - const ptype cur_sum = accum[r][c]; - if (cur_sum >= thresh) - { - ++count; - - if (dets.size() < max_dets) - { - dets.push_back(std::make_pair(cur_sum, point(c,r))); - } - else - { - // The idea here is to cause us to randomly sample possible detection - // locations throughout the image rather than just stopping the detection - // procedure once we hit the max_dets limit. So this method will result - // in a random subsample of all the detections >= thresh being in dets - // at the end of scan_image(). - const unsigned long random_index = rnd.get_random_32bit_number()%count; - if (random_index < dets.size()) - { - dets[random_index] = std::make_pair(cur_sum, point(c,r)); - } - } - } - } - } + find_points_above_thresh(dets, accum, thresh, max_dets); } // ---------------------------------------------------------------------------------------- @@ -318,14 +336,9 @@ namespace dlib } #endif - - dets.clear(); - if (max_dets == 0) - return; if (movable_rects.size() == 0 && fixed_rects.size() == 0) return; - typedef typename image_array_type::type::type pixel_type; typedef typename promote::type ptype; @@ -344,38 +357,7 @@ namespace dlib max_filter(temp, accum, window.width(), window.height(), 0); } - // TODO, make this block its own function and reuse it in scan_image(). - unsigned long count = 0; - dlib::rand rnd; - for (long r = 0; r < accum.nr(); ++r) - { - for (long c = 0; c < accum.nc(); ++c) - { - const ptype cur_sum = accum[r][c]; - if (cur_sum >= thresh) - { - ++count; - - if (dets.size() < max_dets) - { - dets.push_back(std::make_pair(cur_sum, point(c,r))); - } - else - { - // The idea here is to cause us to randomly sample possible detection - // locations throughout the image rather than just stopping the detection - // procedure once we hit the max_dets limit. So this method will result - // in a random subsample of all the detections >= thresh being in dets - // at the end of scan_image_movable_parts(). - const unsigned long random_index = rnd.get_random_32bit_number()%count; - if (random_index < dets.size()) - { - dets[random_index] = std::make_pair(cur_sum, point(c,r)); - } - } - } - } - } + find_points_above_thresh(dets, accum, thresh, max_dets); } // ---------------------------------------------------------------------------------------- diff --git a/dlib/image_processing/scan_image_abstract.h b/dlib/image_processing/scan_image_abstract.h index 055a0eb96..08f2c8cb5 100644 --- a/dlib/image_processing/scan_image_abstract.h +++ b/dlib/image_processing/scan_image_abstract.h @@ -96,6 +96,37 @@ namespace dlib Then this function returns TOTAL_FIXED + TOTAL_MOVABLE. !*/ +// ---------------------------------------------------------------------------------------- + + template < + typename image_type + > + void find_points_above_thresh ( + std::vector >& dets, + const image_type& img, + const double thresh, + const unsigned long max_dets + ); + /*! + requires + - image_type == an implementation of array2d/array2d_kernel_abstract.h + - image_type::type == a scalar pixel type (e.g. int rather than rgb_pixel) + ensures + - #dets == a list of points from img which had pixel values >= thresh. + - Specifically, we have: + - #dets.size() <= max_dets + (note that dets is cleared before new detections are added by find_points_above_thresh()) + - for all valid i: + - #dets[i].first == img[#dets[i].second.y()][#dets[i].second.x()] + (i.e. the first field contains the value of the pixel at this detection location) + - #dets[i].first >= thresh + - if (there are more than max_dets locations that pass the above threshold test) then + - #dets == a random subsample of all the locations which passed the threshold + test. + - else + - #dets == all the points which passed the threshold test. + !*/ + // ---------------------------------------------------------------------------------------- template <