00001
00002
00003 #include <math.h>
00004 #include "Edge.h"
00005
00006 template
00007 void GradientMap< int >(Image< int > &input, FloatImage &gx, FloatImage &gy);
00008 template
00009 void GradientMap< byte >(Image< byte > &input, FloatImage &gx, FloatImage &gy);
00010 template
00011 void GradientMap< float >(Image< float > &input, FloatImage &gx, FloatImage &gy);
00012
00013 template
00014 void EdgePrewitt< int >(Image< int > &input, int type, Image< int > &output);
00015 template
00016 void EdgePrewitt< byte >(Image< byte > &input, int type, Image< byte > &output);
00017 template
00018 void EdgePrewitt< float >(Image< float > &input, int type, Image< float > &output);
00019
00020 template
00021 void EdgeSobel< int >(Image< int > &input, int type, Image< int > &output);
00022 template
00023 void EdgeSobel< byte >(Image< byte > &input, int type, Image< byte > &output);
00024 template
00025 void EdgeSobel< float >(Image< float > &input, int type, Image< float > &output);
00026
00027 template
00028 void EdgeKirsch< int >(Image< int > &input, Image< int > &output);
00029 template
00030 void EdgeKirsch< byte >(Image< byte > &input, Image< byte > &output);
00031 template
00032 void EdgeKirsch< float >(Image< float > &input, Image< float > &output);
00033
00034 void Normalize(vector< FloatImage > &v)
00035 {
00036 bool flag = true;
00037 for(int i=1;i<v.size();i++)
00038 if (v[0].getWidth() != v[i].getWidth() ||
00039 v[0].getHeight() != v[i].getHeight())
00040 flag = false;
00041
00042 if (flag) {
00043 FloatImage mag;
00044 Magnitude(v, mag);
00045 for(int i=0;i<mag.getSize();i++)
00046 mag(i) = (mag(i) > 1E-10) ? 1/mag(i) : 0;
00047 for(int i=0;i<v.size();i++)
00048 v[i] *= mag;
00049 }
00050 }
00051
00052 void Magnitude(vector< FloatImage > &v, FloatImage &magnit)
00053 {
00054 bool flag = true;
00055 for(int i=1;i<v.size();i++)
00056 if (v[0].getWidth() != v[i].getWidth() ||
00057 v[0].getHeight() != v[i].getHeight())
00058 flag = false;
00059
00060 if (flag) {
00061 magnit = v[0];
00062 magnit.clearImage(0);
00063 for(int i=0;i<v.size();i++)
00064 for(int j=0;j<v[i].getSize();j++)
00065 magnit(j) += v[i](j)*v[i](j);
00066 for(int i=0;i<magnit.getSize();i++)
00067 magnit(i) = sqrt(magnit(i));
00068 }
00069 }
00070
00071 template< class T >
00072 void GradientMap(Image< T > &input, FloatImage &gx, FloatImage &gy)
00073 {
00074 int width = input.getWidth(), height = input.getHeight(), i, j;
00075
00076 gx.makeImage(width, height);
00077 gy.makeImage(width, height);
00078
00079 for(i=0;i<height;i++) {
00080 gx[i][0]=(float)(input[i][1]-input[i][0]);
00081 gx[i][width-1]=(float)(input[i][width-2]-input[i][width-1]);
00082 }
00083
00084 for(i=0;i<width;i++) {
00085 gy[0][i]=(float)(input[1][i]-input[0][i]);
00086 gy[height-1][i]=(float)(input[height-2][i]-input[height-1][i]);
00087 }
00088
00089 for(i=1;i<height-1;i++)
00090 for(j=1;j<width-1;j++) {
00091 gy[i][j]=(float)(input[i+1][j]-input[i-1][j]);
00092 gx[i][j]=(float)(input[i][j+1]-input[i][j-1]);
00093 }
00094 }
00095
00096 template< class T >
00097 void EdgePrewitt(Image< T > &input, int type, Image< T > &output)
00098 {
00099 IntKernel kh(PREWITT_H), kv(PREWITT_V);
00100 int width = input.getWidth(), height = input.getHeight();
00101
00102 Image< T > tmp;
00103 tmp.makeImage(width, height);
00104
00105 for(int i=1;i<height-1;i++)
00106 for(int j=1;j<width-1;j++) {
00107 T h = 0, v = 0;
00108 for(int k=-1;k<=1;k++)
00109 for(int m=-1;m<=1;m++) {
00110 h += (T)kh[k+1][m+1]*input[i+k][j+m];
00111 v += (T)kv[k+1][m+1]*input[i+k][j+m];
00112 }
00113 tmp[i][j] = (type) ? (T)sqrt(h*h+v*v) : (T)(fabs(h) + fabs(v));
00114 }
00115
00116 output = tmp;
00117 }
00118
00119 template< class T >
00120 void EdgeSobel(Image< T > &input, int type, Image< T > &output)
00121 {
00122 IntKernel kh(SOBEL_H), kv(SOBEL_V);
00123 int width = input.getWidth(), height = input.getHeight();
00124
00125 Image< T > tmp;
00126 tmp.makeImage(width, height);
00127
00128 for(int i=1;i<height-1;i++)
00129 for(int j=1;j<width-1;j++) {
00130 T h = 0, v = 0;
00131 for(int k=-1;k<=1;k++)
00132 for(int m=-1;m<=1;m++) {
00133 h += (T)kh[k+1][m+1]*input[i+k][j+m];
00134 v += (T)kv[k+1][m+1]*input[i+k][j+m];
00135 }
00136 tmp[i][j] = (type) ? (T)sqrt(h*h+v*v) : (T)(fabs(h) + fabs(v));
00137 }
00138 output = tmp;
00139 }
00140
00141 template< class T >
00142 void EdgeKirsch(Image< T > &input, Image< T > &output)
00143 {
00144 int width = input.getWidth(), height = input.getHeight();
00145
00146 Image< T > tmp;
00147 tmp.makeImage(width, height);
00148
00149 for(int i=1;i<height-1;i++)
00150 for(int j=1;j<width-1;j++) {
00151 T max=-(T)(1E-10); int pos;
00152 for(int orient=KIRSCH_1;orient<=KIRSCH_8;orient++) {
00153 T val=0;
00154 IntKernel kern((INT_KERNEL_TYPE)orient);
00155 for(int k=-1;k<=1;k++)
00156 for(int m=-1;m<=1;m++)
00157 val += (T)kern[k+1][m+1]*input[i+k][j+m];
00158 if(val>max) {
00159 max=val;
00160 pos=orient;
00161 }
00162 }
00163 tmp[i][j] = max;
00164 }
00165 output = tmp;
00166 }
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176