ivl 679
ivl/details/image/functions/image_functions.hpp
00001 /* This file is part of the ivl C++ library <http://image.ntua.gr/ivl>.
00002    A C++ template library extending syntax towards mathematical notation.
00003 
00004    Copyright (C) 2012 Yannis Avrithis <iavr@image.ntua.gr>
00005    Copyright (C) 2012 Kimon Kontosis <kimonas@image.ntua.gr>
00006 
00007    ivl is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU Lesser General Public License 
00009    version 3 as published by the Free Software Foundation.
00010 
00011    Alternatively, you can redistribute it and/or modify it under the terms 
00012    of the GNU General Public License version 2 as published by the Free 
00013    Software Foundation.
00014 
00015    ivl is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00018    See the GNU General Public License for more details.
00019 
00020    You should have received a copy of the GNU General Public License 
00021    and a copy of the GNU Lesser General Public License along 
00022    with ivl. If not, see <http://www.gnu.org/licenses/>. */
00023 
00024 #ifndef IVL_IMAGE_DETAILS_IMAGE_FUNCTIONS_HPP
00025 #define IVL_IMAGE_DETAILS_IMAGE_FUNCTIONS_HPP
00026 
00027 namespace ivl {
00028 
00029 template <class T>
00030 inline
00031 std::ostream& print(std::ostream& os, const image<T>& in)
00032 {
00033         return print_array_nd(os, in);
00034 }
00035 
00036 
00042 // Misc function declarations
00044 //template<class T, class D>
00045 //void show(const image<T, D>& img, const char* title);
00046 
00048 template <class T, class D>
00049 void show(
00050         const image<T, D>& im1, const char* title1,
00051         const image<T, D>& im2 = image<T, D>(), const char* title2 = NULL,
00052         const image<T, D>& im3 = image<T, D>(), const char* title3 = NULL,
00053         const image<T, D>& im4 = image<T, D>(), const char* title4 = NULL,
00054         const image<T, D>& im5 = image<T, D>(), const char* title5 = NULL);
00055 
00056 
00057 template <
00058         class T1, class D1, class T2, class D2, 
00059         class T3, class D3, class T4, class D4,
00060         class T5, class D5>
00061 void imshow_impl(
00062         const image<T1, D1>& im1, const char* title1,
00063         const image<T2, D2>& im2, const char* title2,
00064         const image<T3, D3>& im3, const char* title3,
00065         const image<T4, D4>& im4, const char* title4,
00066         const image<T5, D5>& im5, const char* title5);
00067 
00068 template <class T, class D>
00069 void imshow(
00070         const image<T, D>& im1, const char* title1 = "")
00071 {
00072         ivl::imshow_impl(im1, title1, 
00073                 image<T, D>(), NULL, image<T, D>(), NULL, image<T, D>(), NULL, 
00074                 image<T, D>(), NULL);
00075 }
00076 // ---------------------------------------------------
00077 
00078 template <class T, class D, class T2, class D2>
00079 void imshow(
00080         const image<T, D>& im1, const char* title1,
00081         const image<T2, D2>& im2, const char* title2 = ""
00082         )
00083 {
00084         ivl::imshow_impl(im1, title1, im2, title2, 
00085                 image<T, D>(), NULL, image<T, D>(), 
00086                 NULL, image<T, D>(), NULL);
00087 }
00088 template <class T, class D, class T2, class D2>
00089 void imshow(
00090         const image<T, D>& im1, const image<T2, D2>& im2)
00091 {
00092         ivl::imshow_impl(im1, "", im2, "", 
00093                 image<T, D>(), NULL, image<T, D>(), NULL, 
00094                 image<T, D>(), NULL);
00095 }
00096 // ---------------------------------------------------
00097 template <class T, class D, class T2, class D2, class T3, class D3>
00098 void imshow(
00099         const image<T, D>& im1, const char* title1,
00100         const image<T2, D2>& im2, const char* title2,
00101         const image<T3, D3>& im3, const char* title3 = ""
00102         )
00103 {
00104         ivl::imshow_impl(im1, title1, im2, title2, im3, title3,
00105                 image<T, D>(), NULL, image<T, D>(), NULL);
00106 }
00107 template <class T, class D, class T2, class D2, class T3, class D3>
00108 void imshow(
00109         const image<T, D>& im1, const image<T2, D2>& im2,       
00110         const image<T3, D3>& im3)
00111 {
00112         ivl::imshow_impl(im1, "", im2, "", im3, "",
00113                 image<T, D>(), NULL, image<T, D>(), NULL);
00114 }
00115 
00116 
00117 // Misc function declarations
00119 template<class T, class D>
00120 void view(const image<T, D>& img, const char* title);
00121 
00122 /* IMAGE PROCESSING FUNCTIONS */
00123 
00125 template <class T, class D>
00126 typename image<T, D>::create_similar flipud(const image<T, D>& a);
00127 
00128 template <class T, class D>
00129 typename image<T, D>::create_similar fliplr(const image<T, D>& a);
00130 
00132 template <class T, class D>
00133 typename image<T, D>::create_similar grayscale(const image<T, D>& a);
00134 
00136 template <class T, class D>
00137 typename image<T, D>::create_new sobel(const image<T, D>& a, int y_order, int x_order,
00138                int aperture_size = 3);
00139 
00141 template <class T, class D>
00142 typename image<T, D>::create_new laplace(const image<T, D>& a, int aperture_size = 3);
00143 
00145 template <class T, class D>
00146 typename image<T, D>::create_new edges(const image<T, D>& a, double threshold1,
00147         double threshold2, int aperture_size = 3);
00148 
00150 template <class T, class D>
00151 typename image<T, D>::create_new pre_corner_detect(const image<T, D>& a, int aperture_size = 3);
00152 
00153 template <class T, class D>
00154 typename image<T, D>::create_new corner_eigenvals(const image<T, D>& a, int block_size,
00155         int aperture_size = 3);
00156 
00157 template <class T, class D>
00158 typename image<T, D>::create_new corner_min_eigenval(const image<T, D>& a, int block_size,
00159         int aperture_size = 3);
00160 
00161 template <class T, class D>
00162 typename image<T, D>::create_new corner_harris(const image<T, D>& a, int block_size,
00163         int aperture_size = 3, double k = 0.04);
00164 
00166 template <class T, class D>
00167 typename image<T, D>::create_new pyramid_up(const image<T, D>& a);
00168 
00169 template <class T, class D>
00170 typename image<T, D>::create_new pyramid_down(const image<T, D>& a);
00171 
00173 template <class T, class D>
00174 typename image<T, D>::create_new resample(const image<T, D>& a, size_t rows, size_t cols,
00175         interpolation interp = linear);
00176 
00178 template <class T, class D>
00179 typename image<T, D>::create_new blur_no_scale(const image<T, D>& a, int neighbourhood_rows = 0,
00180         int neighbourhood_cols = 3);
00181 
00182 template <class T, class D>
00183 typename image<T, D>::create_new blur(const image<T, D>& a, int neighbourhood_rows = 0,
00184         int neighbourhood_cols = 3);
00185 
00186 template <class T, class D>
00187 typename image<T, D>::create_new gaussian_blur(const image<T, D>& a, int neighbourhood_rows = 0,
00188         int neighbourhood_cols = 3, int vertical_sigma = 0, int horizontal_sigma = 0);
00189 
00190 template <class T, class D>
00191 typename image<T, D>::create_new median_blur(const image<T, D>& a, int neighbourhood = 3);
00192 
00193 template <class T, class D>
00194 typename image<T, D>::create_new bilateral_blur(const image<T, D>& a, int color_sigma = 3,
00195         int space_sigma = 3);
00196 
00198 template <class T, class D>
00199 typename image<T, D>::create_new rotate(const image<T, D>& a, double angle, const float_point<2>& center,
00200         double scale = 1.0, interpolation interp = linear,
00201         warp_method warp_m = fill_outliers,
00202         color<T, D::Ch> fill_color = ivl::black(color_type<T, rgb>()));
00203 
00205 template <class T, class D>
00206 typename image<T, D>::create_new get_quadrange_sub_pix(const image<T, D>& a,
00207                 const array_2d<double>& map_matrix, const size_array& dst_size = idx(0, 0));
00209 
00210 
00211 // -------------------- TODO coding style
00212 /*
00213 template<class T, class D>
00214 void AA_trig_artsy(image<T, D>& im, double_point<2> a, double_point<2> b, double_point<2> c, color<T, D::Ch> col, int multisample = 32, double opacity = 1.);
00215 
00216 
00217 template<class T, class D>
00218 void AA_trig(image<T, D>& im, double_point<2> a, double_point<2> b, double_point<2> c, color<T, D::Ch> col, int multisample = 8, double opacity = 1.);
00219 
00220 
00221 template<class T, class D>
00222 void AA_quad(image<T, D>& im, double_point<2> a, double_point<2> b, double_point<2> c, double_point<2> d,
00223                          typename image<T, D>::color_type col, int multisample = 8, double opacity = 1.);
00224 
00225 template<class T, class D>
00226 void AA_circ(image<T, D>& im, double_point<2> a, double radius, color<T, D::Ch> col, int multisample = 8, double opacity = 1.);
00227 
00228 template<class T, class D>
00229 void AA_circle(image<T, D>& im, double_point<2> a, double outradius, double inradius, color<T, D::Ch> col, int multisample = 8, double opacity = 1.);
00230 
00231 template<class T, class D>
00232 void AA_fillellipse(image<T, D>& im, double_point<2> a, double_point<2> b, double radius, color<T, D::Ch> col, int multisample = 8, double opacity = 1.);
00233 
00234 template<class T, class D>
00235 void AA_ellipse(image<T, D>& im, double_point<2> a, double_point<2> b, double radius, double thickness, color<T, D::Ch> col, int multisample = 8, double opacity = 1.);
00236 */
00237 // ------------------------------
00238 
00239 template <class T, class D>
00240 void line_on(image<T, D>& im, double_point<2> a, double_point<2> b, color<T, D::Ch> col = ivl::white<T, rgb>(), double thickness = 1.5, double opacity = 1.)
00241 {
00242         double_point<2> vec(b.y - a.y, b.x - a.x);
00243         double theta = std::atan2(vec.y, vec.x);
00244         double rtheta = theta + 3.141592653589793 / 2; // TODO: do something with pi
00245         thickness /= 2;
00246         double_point<2> edge(std::sin(rtheta) * thickness, std::cos(rtheta) * thickness);
00247 
00248         ivl::AA_quad(im,
00249                 double_point<2>(1),
00250                 double_point<2>(1),
00251                 double_point<2>(1),
00252                 double_point<2>(1),
00253                                 col, 8, opacity);
00254 }
00255 
00256 template <class T, class D>
00257 typename image<T, D>::derived_type& circle_on(image<T, D>& im, double_point<2> a, double radius, color<T, D::Ch> col = ivl::white<T, rgb>(), double thickness = 1.5, double opacity = 1.)
00258 {
00259         thickness /= 2;
00260         AA_circle(im, a, radius + thickness, radius - thickness, col, 8, opacity);
00261         return im.derived();
00262 }
00263 
00264 template <class T, class D>
00265 typename image<T, D>::derived_type& rect_on(image<T, D>& im, double_point<2> a, double_point<2> b, color<T, D::Ch> col = ivl::white<T, rgb>(), double thickness = 1.5, double opacity = 1.)
00266 {
00267         double_point<2> c(a.y, b.x);
00268         double_point<2> d(b.y, a.x);
00269 
00270         im.line(a, c, col, thickness, opacity);
00271         im.line(c, b, col, thickness, opacity);
00272         im.line(b, d, col, thickness, opacity);
00273         return line_on(im, d, a, col, thickness, opacity);
00274 }
00275 
00276 template <class T, class D>
00277 typename image<T, D>::derived_type& ellipse_on(image<T, D>& im, double_point<2> a, double_point<2> b, double radius, color<T, D::Ch> col = ivl::white<T, rgb>(), double thickness = 1.5, double opacity = 1.)
00278 {
00279         thickness /= 2;
00280         AA_ellipse(im, a, b, 2 * radius, thickness, col, 8, opacity);
00281         return im.derived();
00282 }
00283 
00284 template <class T, class D>
00285 typename image<T, D>::derived_type& ellipse_on(image<T, D>& im, double_point<2> c, double radius1, double radius2, double angle, color<T, D::Ch> col = ivl::white<T, rgb>(), double thickness = 1.5, double opacity = 1.)
00286 {
00287         double pi = 3.141592653589793; // todo: ivl::pi
00288         if(radius1 < radius2) {
00289                 std::swap(radius1, radius2);
00290                 angle += pi / 2;
00291                 if(angle > 2 * pi) angle -= 2 * pi;
00292         }
00293 
00294         double displace = std::sqrt(radius1 * radius1 - radius2 * radius2);
00295         double_point<2> displ(std::sin(angle) * displace, std::cos(angle) * displace);
00296 
00297         AA_ellipse(im, double_point<2>(c + displ), double_point<2>(c - displ), 2 * radius1,
00298                                 thickness, col, 8, opacity);
00299         return im.derived();
00300 }
00301 
00302 template <class T, class D>
00303 typename image<T, D>::derived_type& fill_circle_on(image<T, D>& im, double_point<2> a, double radius, color<T, D::Ch> col = ivl::white<T, rgb>(), double opacity = 1.)
00304 {
00305         AA_circ(im, a, radius, col, 8, opacity);
00306         return im.derived();
00307 }
00308 
00309 template <class T, class D>
00310 typename image<T, D>::derived_type& fill_rect_on(image<T, D>& im, double_point<2> a, double_point<2> b, color<T, D::Ch> col = ivl::white<T, rgb>(), double opacity = 1.)
00311 {
00312         a = double_point<2>(std::min(a.y, b.y), std::min(a.x, b.x));
00313         b = double_point<2>(std::max(a.y, b.y), std::max(a.x, b.x));
00314 
00315         double_point<2> c(a.y, b.x);
00316         double_point<2> d(b.y, a.x);
00317 
00318         AA_quad(im, a, c, b, d, col, 8, opacity);
00319         return im.derived();
00320 }
00321 
00322 template <class T, class D>
00323 typename image<T, D>::derived_type& fill_ellipse_on(image<T, D>& im, double_point<2> a, double_point<2> b, double radius, color<T, D::Ch> col = ivl::white<T, rgb>(), double opacity = 1.)
00324 {
00325         AA_fillellipse(im, a, b, 2 * radius, col, 8, opacity);
00326         return im.derived();
00327 }
00328 
00329 template <class T, class D>
00330 typename image<T, D>::derived_type& fill_ellipse_on(image<T, D>& im, double_point<2> c, double radius1, double radius2, double angle, color<T, D::Ch> col = ivl::white<T, rgb>(), double opacity = 1.)
00331 {
00332         double pi = 3.141592653589793; // todo: ivl::pi
00333         if(radius1 < radius2) {
00334                 std::swap(radius1, radius2);
00335                 angle += pi / 2;
00336                 if(angle > 2 * pi) angle -= 2 * pi;
00337         }
00338 
00339         double displace = std::sqrt(radius1 * radius1 - radius2 * radius2);
00340         double_point<2> displ(std::sin(angle) * displace, std::cos(angle) * displace);
00341 
00342         AA_fillellipse(im, double_point<2>(c + displ), double_point<2>(c - displ),
00343                                         2 * radius1, col, 8, opacity);
00344         return im.derived();
00345 }
00346 
00347 
00348 template <class T, class D>
00349 typename image<T, D>::derived_type& fill_triangle_on(image<T, D>& im, double_point<2> a, double_point<2> b, double_point<2> c, color<T, D::Ch> col = ivl::white<T, rgb>(), double opacity = 1.)
00350 {
00351         AA_trig(im, a, b, c, col, 8, opacity);
00352         return im.derived();
00353 }
00354 
00355 template <class T, class D>
00356 typename image<T, D>::derived_type& fill_quad_on(image<T, D>& im, double_point<2> a, double_point<2> b, double_point<2> c, double_point<2> d, color<T, D::Ch> col = ivl::white<T, rgb>(), double opacity = 1.)
00357 {
00358         AA_quad(im, a, b, c, d, col, 8, opacity);
00359         return im.derived();
00360 }
00361 
00362 
00363 //
00364 
00365 template<class T>
00366 image<T> imread(const char* str)
00367 {
00368         image<T> im;
00369         im.load(str);
00370         return im;
00371 }
00372 
00373 //
00374 
00375 
00376 } /* namespace ivl */
00377 
00378 #endif // IVL_IMAGE_DETAILS_IMAGE_FUNCTIONS_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations