// -*- c-basic-offset: 4 -*- /** @file ROI.h * * functions to manage ROI's * * @author Pablo d'Angelo * * $Id: utils.h,v 1.2 2007/01/27 05:00:36 acmihal Exp $ * * This is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this software; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #ifndef VIGRA_EXT_UTILS_H #define VIGRA_EXT_UTILS_H #include #include #include namespace vigra { template struct PromoteTraits, T2 > { typedef RGBValue::Promote> Promote; }; } namespace vigra_ext { using VIGRA_CSTD::pow; using VIGRA_CSTD::log; inline float pow(float a, double b) { return std::pow(a,(float) b); } /// component-wise absolute value template inline vigra::RGBValue pow(vigra::RGBValue const & v, double e) { return vigra::RGBValue(pow(v.red(),e), pow(v.green(),e), pow(v.blue(),e)); } /// add a scalar to all components template inline vigra::RGBValue & operator+=(vigra::RGBValue & l, V2 const & r) { l.red() += r; l.green() += r; l.blue() += r; return l; } /// component-wise logarithm template inline vigra::RGBValue log(vigra::RGBValue const & v) { return vigra::RGBValue(std::log(v.red()), std::log(v.green()), std::log(v.blue())); } /// component-wise logarithm template inline vigra::RGBValue log10(vigra::RGBValue const & v) { return vigra::RGBValue(std::log10(v.red()), std::log10(v.green()), std::log10(v.blue())); } /// add a scalar to all components template inline // WARNING: This is a hack.. //vigra::RGBValue typename vigra::PromoteTraits, V2 >::Promote operator+(vigra::RGBValue const & r1, V2 const & r2) { typename vigra::PromoteTraits, V2 >::Promote res(r1); res += r2; return res; } /** Apply pow() function to each vector component. */ template inline vigra::TinyVector pow(vigra::TinyVector const & v, double e) { vigra::TinyVector res; for (int i=0; i inline vigra::TinyVector log(vigra::TinyVector const & v, double e) { vigra::TinyVector res; for (int i=0; i 0 in both images */ struct OverlapSizeCounter { OverlapSizeCounter() : size(0) { } template void operator()(PIXEL const & img1, PIXEL const & img2) { if (img1 > 0 && img2 > 0) { size++; } } unsigned int getSize() { return size; } unsigned int size; }; /** functor to combine two functors: result = f1( f2(v) ) * * The functors are copied, so there is no way to get * their internal state after they have been applied. * * This is quite useful for multithreaded processing. */ template struct NestFunctor { F1 f1; F2 f2; NestFunctor(const F1 & fu1, const F2 & fu2) : f1(fu1), f2(fu2) { } /** the functor's second argument type */ typedef typename F1::result_type result_type; template result_type operator()(T1 const & v) const { return f1(f2(v)); } /** if F2 takes 2 arguments */ template result_type operator()(T1 const & v1, T2 const & v2) const { return f1(f2(v1,v2)); } /** if F2 takes 3 arguments */ template result_type operator()(T1 const & v1, T2 const & v2, T3 const & v3) const { return f1(f2(v1,v2,v3)); } }; /** count pixels that are > 0 in a single image */ struct MaskPixelCounter { MaskPixelCounter() : count(0) { } template void operator()(PIXEL const & img1) { if (img1 > 0) { count++; } } int getCount() { return count; } int count; }; /** Apply a circular crop to \p img * * Sets all pixels that are outside of * the circle specified by \p middle and \p radius * to zero. */ template void circularCrop(vigra::triple img, FDiff2D middle, double radius) { vigra::Diff2D imgSize = img.second - img.first; double r2 = radius*radius; // create dest y iterator SrcImageIterator yd(img.first); // loop over the image and transform for(int y=0; y < imgSize.y; ++y, ++yd.y) { // create x iterators SrcImageIterator xd(yd); for(int x=0; x < imgSize.x; ++x, ++xd.x) { double dx = x-middle.x; double dy = y-middle.y; if (dx*dx+dy*dy > r2) { *xd = 0; } } } } } // namespace #endif // VIGRA_EXT_UTILS_H