filter.h
Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2006 Pedro Felzenszwalb
00003 
00004 This program is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation; either version 2 of the License, or
00007 (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00017 */
00018 
00019 /* simple filters */
00020 
00021 #ifndef FILTER_H
00022 #define FILTER_H
00023 
00024 #include <vector>
00025 #include <cmath>
00026 #include "image.h"
00027 #include "misc.h"
00028 #include "convolve.h"
00029 #include "imconv.h"
00030 
00031 #define WIDTH 4.0
00032 
00033 /* normalize mask so it integrates to one */
00034 static void normalize(std::vector<float> &mask) {
00035   int len = mask.size();
00036   float sum = 0;
00037   for (int i = 1; i < len; i++) {
00038     sum += fabs(mask[i]);
00039   }
00040   sum = 2*sum + fabs(mask[0]);
00041   for (int i = 0; i < len; i++) {
00042     mask[i] /= sum;
00043   }
00044 }
00045 
00046 /* make filters */
00047 #define MAKE_FILTER(name, fun)                                \
00048 static std::vector<float> make_ ## name (float sigma) {       \
00049   sigma = std::max(sigma, 0.01F);                             \
00050   int len = (int)ceil(sigma * WIDTH) + 1;                     \
00051   std::vector<float> mask(len);                               \
00052   for (int i = 0; i < len; i++) {                             \
00053     mask[i] = fun;                                            \
00054   }                                                           \
00055   return mask;                                                \
00056 }
00057 
00058 MAKE_FILTER(fgauss, exp(-0.5*square(i/sigma)));
00059 
00060 /* convolve image with gaussian filter */
00061 static image<float> *smooth(image<float> *src, float sigma) {
00062   std::vector<float> mask = make_fgauss(sigma);
00063   normalize(mask);
00064 
00065   image<float> *tmp = new image<float>(src->height(), src->width(), false);
00066   image<float> *dst = new image<float>(src->width(), src->height(), false);
00067   convolve_even(src, tmp, mask);
00068   convolve_even(tmp, dst, mask);
00069 
00070   delete tmp;
00071   return dst;
00072 }
00073 
00074 /* convolve image with gaussian filter */
00075 image<float> *smooth(image<uchar> *src, float sigma) {
00076   image<float> *tmp = imageUCHARtoFLOAT(src);
00077   image<float> *dst = smooth(tmp, sigma);
00078   delete tmp;
00079   return dst;
00080 }
00081 
00082 /* compute laplacian */
00083 static image<float> *laplacian(image<float> *src) {
00084   int width = src->width();
00085   int height = src->height();
00086   image<float> *dst = new image<float>(width, height);  
00087 
00088   for (int y = 1; y < height-1; y++) {
00089     for (int x = 1; x < width-1; x++) {
00090       float d2x = imRef(src, x-1, y) + imRef(src, x+1, y) -
00091         2*imRef(src, x, y);
00092       float d2y = imRef(src, x, y-1) + imRef(src, x, y+1) -
00093         2*imRef(src, x, y);
00094       imRef(dst, x, y) = d2x + d2y;
00095     }
00096   }
00097   return dst;
00098 }
00099 
00100 #endif


zyonz_image_based_leaf_probing
Author(s): Sergi Foix
autogenerated on Fri Dec 6 2013 23:25:27