Changed the behavior of gaussian_blur() to make it a little more user friendly.

This commit is contained in:
Davis King 2011-09-29 19:19:32 -04:00
parent 128cc0cb51
commit 3c04d06abb
2 changed files with 26 additions and 17 deletions

View File

@ -369,33 +369,41 @@ namespace dlib
> >
matrix<T,0,1> create_gaussian_filter ( matrix<T,0,1> create_gaussian_filter (
double sigma, double sigma,
int size int max_size
) )
/*! /*!
requires requires
- sigma > 0 - sigma > 0
- size > 0 - max_size > 0
- size is an odd number - max_size is an odd number
ensures ensures
- returns a separable Gaussian filter F such that: - returns a separable Gaussian filter F such that:
- is_vector(F) == true - is_vector(F) == true
- F.size() == size - F.size() <= max_size
- F is suitable for use with the spatially_filter_image_separable() routine - F is suitable for use with the spatially_filter_image_separable() routine
and its use with this function corresponds to running a Gaussian filter and its use with this function corresponds to running a Gaussian filter
of sigma width over an image. of sigma width over an image.
!*/ !*/
{ {
DLIB_ASSERT(sigma > 0 && size > 0 && (size%2)==1, DLIB_ASSERT(sigma > 0 && max_size > 0 && (max_size%2)==1,
"\t matrix<T,0,1> create_gaussian_filter()" "\t matrix<T,0,1> create_gaussian_filter()"
<< "\n\t Invalid inputs were given to this function." << "\n\t Invalid inputs were given to this function."
<< "\n\t sigma: " << sigma << "\n\t sigma: " << sigma
<< "\n\t size: " << size << "\n\t max_size: " << max_size
); );
matrix<double,0,1> f(size); // Adjust the size so that the ratio of the gaussian values isn't huge.
// This only matters when T is an integer type. However, we do it for
// all types so that the behavior of this function is always relatively
// the same.
while (gaussian(0,sigma)/gaussian(max_size/2,sigma) > 50)
--max_size;
matrix<double,0,1> f(max_size);
for (long i = 0; i < f.size(); ++i) for (long i = 0; i < f.size(); ++i)
{ {
f(i) = gaussian(i-size/2, sigma); f(i) = gaussian(i-max_size/2, sigma);
} }
if (is_float_type<T>::value == false) if (is_float_type<T>::value == false)
@ -419,22 +427,22 @@ namespace dlib
const in_image_type& in_img, const in_image_type& in_img,
out_image_type& out_img, out_image_type& out_img,
double sigma = 1, double sigma = 1,
int size = 7 int max_size = 1001
) )
{ {
DLIB_ASSERT(sigma > 0 && size > 0 && (size%2)==1 && DLIB_ASSERT(sigma > 0 && max_size > 0 && (max_size%2)==1 &&
is_same_object(in_img, out_img) == false, is_same_object(in_img, out_img) == false,
"\t void gaussian_blur()" "\t void gaussian_blur()"
<< "\n\t Invalid inputs were given to this function." << "\n\t Invalid inputs were given to this function."
<< "\n\t sigma: " << sigma << "\n\t sigma: " << sigma
<< "\n\t size: " << size << "\n\t max_size: " << max_size
<< "\n\t is_same_object(in_img,out_img): " << is_same_object(in_img,out_img) << "\n\t is_same_object(in_img,out_img): " << is_same_object(in_img,out_img)
); );
typedef typename pixel_traits<typename out_image_type::type>::basic_pixel_type type; typedef typename pixel_traits<typename out_image_type::type>::basic_pixel_type type;
typedef typename promote<type>::type ptype; typedef typename promote<type>::type ptype;
const matrix<ptype,0,1>& filt = create_gaussian_filter<ptype>(sigma, size); const matrix<ptype,0,1>& filt = create_gaussian_filter<ptype>(sigma, max_size);
ptype scale = sum(filt); ptype scale = sum(filt);
scale = scale*scale; scale = scale*scale;

View File

@ -225,7 +225,7 @@ namespace dlib
const in_image_type& in_img, const in_image_type& in_img,
out_image_type& out_img, out_image_type& out_img,
double sigma = 1, double sigma = 1,
int size = 7 int max_size = 1001
); );
/*! /*!
requires requires
@ -235,12 +235,13 @@ namespace dlib
- pixel_traits<typename out_image_type::type>::has_alpha == false - pixel_traits<typename out_image_type::type>::has_alpha == false
- is_same_object(in_img, out_img) == false - is_same_object(in_img, out_img) == false
- sigma > 0 - sigma > 0
- size > 0 - max_size > 0
- size is an odd number - max_size is an odd number
ensures ensures
- Filters in_img with a Gaussian filter of sigma width. The actual spatial filter will - Filters in_img with a Gaussian filter of sigma width. The actual spatial filter will
be applied to pixel blocks that are size wide and size tall. The results are stored be applied to pixel blocks that are at most max_size wide and max_size tall (note that
into #out_img. this function will automatically select a smaller block size as appropriate). The
results are stored into #out_img.
- Pixel values are stored into out_img using the assign_pixel() function and therefore - Pixel values are stored into out_img using the assign_pixel() function and therefore
any applicable color space conversion or value saturation is performed. any applicable color space conversion or value saturation is performed.
- if (pixel_traits<typename in_image_type::type>::grayscale == false) then - if (pixel_traits<typename in_image_type::type>::grayscale == false) then