Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members   Related Pages   Examples  

LocalOp.cpp

00001 /* Copyright (c) 2001 S.E. Grigorescu */
00002 
00003 #include "LocalOp.h"
00004 
00005 template void LocalOp< int >(Image< int >&, string, int, int, Image< int >&);
00006 template void LocalOp< byte >(Image< byte >&, string, int, int, Image< byte >&);
00007 template void LocalOp< float >(Image< float >&, string, int, int, Image< float >&);
00008 template void LocalOp< int >(Image< int >&, string, int, Image< int >&);
00009 template void LocalOp< byte >(Image< byte >&, string, int, Image< byte >&);
00010 template void LocalOp< float >(Image< float >&, string, int, Image< float >&);
00011 template void LocalOp< int >(Image< int >&, string, int, int);
00012 template void LocalOp< byte >(Image< byte >&, string, int, int);
00013 template void LocalOp< float >(Image< float >&, string, int, int);
00014 template void LocalOp< int >(Image< int >&, string, int);
00015 template void LocalOp< byte >(Image< byte >&, string, int);
00016 template void LocalOp< float >(Image< float >&, string, int);
00017 
00018 template int   compute_mean< int >  (Image< int >&,   int, int, int, int);
00019 template byte  compute_mean< byte > (Image< byte >&,  int, int, int, int);
00020 template float compute_mean< float >(Image< float >&, int, int, int, int);
00021 template int   compute_median< int >  (Image< int >&,   int, int, int, int);
00022 template byte  compute_median< byte > (Image< byte >&,  int, int, int, int);
00023 template float compute_median< float >(Image< float >&, int, int, int, int);
00024 template int   compute_stdev< int >  (Image< int >&,   int, int, int, int);
00025 template byte  compute_stdev< byte > (Image< byte >&,  int, int, int, int);
00026 template float compute_stdev< float >(Image< float >&, int, int, int, int);
00027 template int   compute_var< int >  (Image< int >&,   int, int, int, int);
00028 template byte  compute_var< byte > (Image< byte >&,  int, int, int, int);
00029 template float compute_var< float >(Image< float >&, int, int, int, int);
00030 template int   compute_min< int >  (Image< int >&,   int, int, int, int);
00031 template byte  compute_min< byte > (Image< byte >&,  int, int, int, int);
00032 template float compute_min< float >(Image< float >&, int, int, int, int);
00033 template int   compute_max< int >  (Image< int >&,   int, int, int, int);
00034 template byte  compute_max< byte > (Image< byte >&,  int, int, int, int);
00035 template float compute_max< float >(Image< float >&, int, int, int, int);
00036 
00037 template< class T > void LocalOp(Image< T >& input, string op_type, int w_x, int w_y, Image< T >& output)
00038 {
00039   int i, j, i_min, i_max, j_min, j_max, wx, wy;
00040   Image< T > res;
00041   res = input;
00042   wx = (int)floor(w_x/2.0);
00043   wy = (int)floor(w_y/2.0);
00044   for (i = 0; i < input.getHeight(); i++) {
00045     i_min = (i - wy) > 0 ? i - wy : 0;
00046     i_max = (i_min+w_y-1) < input.getHeight() ? i_min+w_y-1 : input.getHeight()-1;
00047     for (j = 0; j < input.getWidth(); j++){
00048       j_min = (j - wx) > 0 ? j - wx : 0;
00049       j_max = (j_min+w_x-1) < input.getWidth() ? j_min+w_x-1 : input.getWidth()-1;
00050       if (op_type == "mean")   res[i][j] = compute_mean(input, i_min, i_max, j_min, j_max);
00051       if (op_type == "median") res[i][j] = compute_median(input, i_min, i_max, j_min, j_max);
00052       if (op_type == "stdev")  res[i][j] = compute_stdev(input, i_min, i_max, j_min, j_max);
00053       if (op_type == "var")    res[i][j] = compute_var(input, i_min, i_max, j_min, j_max);
00054       if (op_type == "min")    res[i][j] = compute_min(input, i_min, i_max, j_min, j_max);
00055       if (op_type == "max")    res[i][j] = compute_max(input, i_min, i_max, j_min, j_max);
00056     }
00057   }
00058   output = res;
00059 }
00060 
00061 template< class T > void LocalOp(Image< T >& input, string op_type, int w, Image< T >& output)
00062 {
00063   LocalOp(input, op_type, w, w, output);
00064 }
00065 
00066 template< class T > void LocalOp(Image< T >& input, string op_type, int w_x, int w_y)
00067 {
00068   LocalOp(input, op_type, w_x, w_y, input);
00069 }
00070 
00071 template< class T > void LocalOp(Image< T >& input, string op_type, int w)
00072 {
00073   LocalOp(input, op_type, w, w, input);
00074 }
00075 
00076 template<class T> T compute_mean(Image< T >& input, int y_min, int y_max, int x_min, int x_max)
00077 {
00078   double res;
00079   res = 0;
00080   for (int i = y_min; i <= y_max; i++)
00081     for (int j = x_min; j <= x_max; j++)
00082       res += input[i][j];
00083   res /= ((y_max-y_min+1)*(x_max-x_min+1));
00084   return (T)res;
00085 }
00086 
00087 template<class T> T compute_median(Image< T >& input, int y_min, int y_max, int x_min, int x_max)
00088 {
00089   vector<T> pixels;
00090   for (int i = y_min; i <= y_max; i++)
00091     for (int j = x_min; j <= x_max; j++)
00092       pixels.push_back(input[i][j]);
00093   sort(pixels.begin(), pixels.end());
00094   if (pixels.size()% 2 == 1) return pixels[(pixels.size()-1)/2];
00095   else return (T)((pixels[pixels.size()/2]+pixels[pixels.size()/2-1])/2.0);
00096 }
00097 
00098 template<class T> T compute_stdev(Image< T >& input, int y_min, int y_max, int x_min, int x_max)
00099 {
00100   double res, mu;
00101   int N;
00102   N = (y_max-y_min+1)*(x_max-x_min+1);
00103   res = 0;
00104   mu = 0;
00105   for (int i = y_min; i <= y_max; i++)
00106     for (int j = x_min; j <= x_max; j++) {
00107       res += input[i][j]*input[i][j];
00108       mu += input[i][j];
00109     }
00110   return (T)sqrt((res-mu)/N);
00111 }
00112 
00113 template<class T> T compute_var(Image< T >& input, int y_min, int y_max, int x_min, int x_max)
00114 {
00115   double res, mu;
00116   int N;
00117   N = (y_max-y_min+1)*(x_max-x_min+1);
00118   res = 0;
00119   mu = 0;
00120   for (int i = y_min; i <= y_max; i++)
00121     for (int j = x_min; j <= x_max; j++) {
00122       res += input[i][j]*input[i][j];
00123       mu += input[i][j];
00124     }
00125   return (T)((res-mu)/N);
00126 }
00127 
00128 template<class T> T compute_min(Image< T >& input, int y_min, int y_max, int x_min, int x_max)
00129 {
00130   T res;
00131   res = input[y_min][x_min];
00132   for (int i = y_min; i <= y_max; i++)
00133     for (int j = x_min; j <= x_max; j++)
00134       res = res > input[i][j] ? input[i][j] : res;
00135   return res;
00136 }
00137 
00138 template<class T> T compute_max(Image< T >& input, int y_min, int y_max, int x_min, int x_max)
00139 {
00140   T res;
00141   res = input[y_min][x_min];
00142   for (int i = y_min; i <= y_max; i++)
00143     for (int j = x_min; j <= x_max; j++)
00144       res = res < input[i][j] ? input[i][j] : res;
00145   return res;
00146 }
00147 
00148 
00149 
00150 
00151 
00152 
00153 
00154