From 0aad39c4e8cdf6e291ad92e427cac57c9bb79eaa Mon Sep 17 00:00:00 2001 From: Davis King Date: Sat, 21 Mar 2015 08:45:17 -0400 Subject: [PATCH] Added get_best_hough_point() to the hough_transform object. --- dlib/image_transforms/hough_transform.h | 139 ++++++++++++++++++ .../hough_transform_abstract.h | 27 ++++ 2 files changed, 166 insertions(+) diff --git a/dlib/image_transforms/hough_transform.h b/dlib/image_transforms/hough_transform.h index e46886d0f..74f1be624 100644 --- a/dlib/image_transforms/hough_transform.h +++ b/dlib/image_transforms/hough_transform.h @@ -8,6 +8,7 @@ #include "../geometry.h" #include "../algs.h" #include "assign_image.h" +#include namespace dlib { @@ -99,6 +100,144 @@ namespace dlib return std::make_pair(p1,p2); } + template < + typename image_type + > + point get_best_hough_point ( + const point& p, + const image_type& himg + ) + { + DLIB_ASSERT(himg.nr() == size() && himg.nc() == size() && + rectangle(0,0,size()-1,size()-1).contains(p) == true, + "\t point hough_transform::get_best_hough_point()" + << "\n\t Invalid arguments given to this function." + << "\n\t himg.nr(): " << himg.nr() + << "\n\t himg.nc(): " << himg.nc() + << "\n\t size(): " << size() + << "\n\t p: " << p + ); + + + typedef typename image_traits::pixel_type pixel_type; + COMPILE_TIME_ASSERT(pixel_traits::grayscale == true); + pixel_type best_val = std::numeric_limits::min(); + point best_point; + + + const long max_n8 = (himg.nc()/8)*8; + const long max_n4 = (himg.nc()/4)*4; + const long r = p.y(); + const long c = p.x(); + + const int32* ysin = &ysin_theta(r,0); + const int32* xcos = &xcos_theta(c,0); + long t = 0; + while(t < max_n8) + { + long rr0 = (*xcos++ + *ysin++)>>16; + long rr1 = (*xcos++ + *ysin++)>>16; + long rr2 = (*xcos++ + *ysin++)>>16; + long rr3 = (*xcos++ + *ysin++)>>16; + long rr4 = (*xcos++ + *ysin++)>>16; + long rr5 = (*xcos++ + *ysin++)>>16; + long rr6 = (*xcos++ + *ysin++)>>16; + long rr7 = (*xcos++ + *ysin++)>>16; + + if (himg[rr0][t++] > best_val) + { + best_val = himg[rr0][t-1]; + best_point.x() = t-1; + best_point.y() = rr0; + } + if (himg[rr1][t++] > best_val) + { + best_val = himg[rr1][t-1]; + best_point.x() = t-1; + best_point.y() = rr1; + } + if (himg[rr2][t++] > best_val) + { + best_val = himg[rr2][t-1]; + best_point.x() = t-1; + best_point.y() = rr2; + } + if (himg[rr3][t++] > best_val) + { + best_val = himg[rr3][t-1]; + best_point.x() = t-1; + best_point.y() = rr3; + } + if (himg[rr4][t++] > best_val) + { + best_val = himg[rr4][t-1]; + best_point.x() = t-1; + best_point.y() = rr4; + } + if (himg[rr5][t++] > best_val) + { + best_val = himg[rr5][t-1]; + best_point.x() = t-1; + best_point.y() = rr5; + } + if (himg[rr6][t++] > best_val) + { + best_val = himg[rr6][t-1]; + best_point.x() = t-1; + best_point.y() = rr6; + } + if (himg[rr7][t++] > best_val) + { + best_val = himg[rr7][t-1]; + best_point.x() = t-1; + best_point.y() = rr7; + } + } + while(t < max_n4) + { + long rr0 = (*xcos++ + *ysin++)>>16; + long rr1 = (*xcos++ + *ysin++)>>16; + long rr2 = (*xcos++ + *ysin++)>>16; + long rr3 = (*xcos++ + *ysin++)>>16; + if (himg[rr0][t++] > best_val) + { + best_val = himg[rr0][t-1]; + best_point.x() = t-1; + best_point.y() = rr0; + } + if (himg[rr1][t++] > best_val) + { + best_val = himg[rr1][t-1]; + best_point.x() = t-1; + best_point.y() = rr1; + } + if (himg[rr2][t++] > best_val) + { + best_val = himg[rr2][t-1]; + best_point.x() = t-1; + best_point.y() = rr2; + } + if (himg[rr3][t++] > best_val) + { + best_val = himg[rr3][t-1]; + best_point.x() = t-1; + best_point.y() = rr3; + } + } + while(t < himg.nc()) + { + long rr0 = (*xcos++ + *ysin++)>>16; + if (himg[rr0][t++] > best_val) + { + best_val = himg[rr0][t-1]; + best_point.x() = t-1; + best_point.y() = rr0; + } + } + + return best_point; + } + template < typename in_image_type, typename out_image_type diff --git a/dlib/image_transforms/hough_transform_abstract.h b/dlib/image_transforms/hough_transform_abstract.h index 9a22200c0..f0ff2b550 100644 --- a/dlib/image_transforms/hough_transform_abstract.h +++ b/dlib/image_transforms/hough_transform_abstract.h @@ -21,6 +21,10 @@ namespace dlib contribute correspondingly more to the output of the Hough transform, allowing stronger edges to create correspondingly stronger line detections in the final Hough transform. + + THREAD SAFETY + It is safe for multiple threads to make concurrent accesses to this object + without synchronization. !*/ public: @@ -74,6 +78,29 @@ namespace dlib - The returned points are inside rectangle(0,0,size()-1,size()-1). !*/ + template < + typename image_type + > + point get_best_hough_point ( + const point& p, + const image_type& himg + ); + /*! + requires + - image_type == an image object that implements the interface defined in + dlib/image_processing/generic_image.h and it must contain grayscale pixels. + - himg.nr() == size() + - himg.nc() == size() + - rectangle(0,0,size()-1,size()-1).contains(p) == true + ensures + - This function interprets himg as a Hough image and p as a point in the + original image space. Given this, it finds the maximum scoring line that + passes though p. That is, it checks all the Hough accumulator bins in + himg corresponding to lines though p and returns the location with the + largest score. + - returns a point X such that get_rect(himg).contains(X) == true + !*/ + template < typename in_image_type, typename out_image_type