Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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
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
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
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
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