diff --git a/dlib/image_transforms/interpolation.h b/dlib/image_transforms/interpolation.h index 2d7ce00f497882b4732dd91ded9797e7fa7066d6..df2d391baa5081149bc1b972aa21a6a8a16af498 100644 --- a/dlib/image_transforms/interpolation.h +++ b/dlib/image_transforms/interpolation.h @@ -433,7 +433,6 @@ namespace dlib // ---------------------------------------------------------------------------------------- - template < typename image_type > @@ -453,6 +452,77 @@ namespace dlib rotate_image(in_img, out_img, angle, interpolate_quadratic()); } +// ---------------------------------------------------------------------------------------- + + namespace impl + { + class helper_resize_image + { + public: + helper_resize_image( + double x_scale_, + double y_scale_ + ): + x_scale(x_scale_), + y_scale(y_scale_) + {} + + dlib::vector<double,2> operator() ( + const dlib::vector<double,2>& p + ) const + { + return dlib::vector<double,2>(p.x()*x_scale, p.y()*y_scale); + } + + private: + const double x_scale; + const double y_scale; + }; + } + + template < + typename image_type, + typename interpolation_type + > + void resize_image ( + const image_type& in_img, + image_type& out_img, + const interpolation_type& interp + ) + { + // make sure requires clause is not broken + DLIB_ASSERT( is_same_object(in_img, out_img) == false , + "\t void resize_image()" + << "\n\t Invalid inputs were given to this function." + << "\n\t is_same_object(in_img, out_img): " << is_same_object(in_img, out_img) + ); + + const double x_scale = (in_img.nc()-1)/(double)std::max<long>((out_img.nc()-1),1); + const double y_scale = (in_img.nr()-1)/(double)std::max<long>((out_img.nr()-1),1); + transform_image(in_img, out_img, interp, + dlib::impl::helper_resize_image(x_scale,y_scale)); + } + +// ---------------------------------------------------------------------------------------- + + template < + typename image_type + > + void resize_image ( + const image_type& in_img, + image_type& out_img + ) + { + // make sure requires clause is not broken + DLIB_ASSERT( is_same_object(in_img, out_img) == false , + "\t void resize_image()" + << "\n\t Invalid inputs were given to this function." + << "\n\t is_same_object(in_img, out_img): " << is_same_object(in_img, out_img) + ); + + resize_image(in_img, out_img, interpolate_quadratic()); + } + // ---------------------------------------------------------------------------------------- template < diff --git a/dlib/image_transforms/interpolation_abstract.h b/dlib/image_transforms/interpolation_abstract.h index 258d2dfd6d22ee85d6977b7ed8b0e87c8b747f18..f74a8129eb89417e0ab3056ff0b64acfedbeacfc 100644 --- a/dlib/image_transforms/interpolation_abstract.h +++ b/dlib/image_transforms/interpolation_abstract.h @@ -316,6 +316,58 @@ namespace dlib - uses the interpolate_quadratic object to perform the necessary pixel interpolation. !*/ +// ---------------------------------------------------------------------------------------- + + template < + typename image_type, + typename interpolation_type + > + void resize_image ( + const image_type& in_img, + image_type& out_img, + const interpolation_type& interp + ); + /*! + requires + - image_type == is an implementation of array2d/array2d_kernel_abstract.h + - interpolation_type == interpolate_nearest_neighbor, interpolate_bilinear, + interpolate_quadratic, or a type with a compatible interface. + - is_same_object(in_img, out_img) == false + ensures + - #out_img == A copy of in_img which has been stretched so that it + fits exactly into out_img. + - The size of out_img is not modified. I.e. + - #out_img.nr() == out_img.nr() + - #out_img.nc() == out_img.nc() + - uses the supplied interpolation routine interp to perform the necessary + pixel interpolation. + !*/ + +// ---------------------------------------------------------------------------------------- + + + template < + typename image_type + > + void resize_image ( + const image_type& in_img, + image_type& out_img + ); + /*! + requires + - image_type == is an implementation of array2d/array2d_kernel_abstract.h + - pixel_traits<typename image_type::type>::has_alpha == false + - is_same_object(in_img, out_img) == false + ensures + - #out_img == A copy of in_img which has been stretched so that it + fits exactly into out_img. + - The size of out_img is not modified. I.e. + - #out_img.nr() == out_img.nr() + - #out_img.nc() == out_img.nc() + - uses the interpolate_quadratic object to perform the necessary pixel + interpolation. + !*/ + // ---------------------------------------------------------------------------------------- template <