00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef BG_SUBTRACT_H
00011 #define BG_SUBTRACT_H
00012
00013
00014
00015 #include <fstream>
00016 #include <iostream>
00017 #include <list>
00018 #include <cmath>
00019 #include <values.h>
00020 #include "image.h"
00021 #include "image_fileoutput.h"
00022
00023 namespace mimas {
00024
00025 template<typename T>
00026 class bg_subtract
00027 {
00028 private:
00029 image<T> bgtemplate;
00030
00031 protected:
00032 double averageVariation;
00033 bool removeMeans;
00034
00035 public:
00036 void createTemplate(image<T> &imagein, int xroi1, int yroi1, int xroi2, int yroi2);
00037 void createTemplate(image<T> &imagein);
00038 void removeBackground(image<T> &imagein);
00039
00040 bg_subtract()
00041 {
00042 averageVariation=20.0;
00043 removeMeans=false;
00044
00045
00046 }
00047
00048 };
00049
00050 template <typename T>
00051 void bg_subtract<T>::createTemplate(image<T> &imagein, int xroi1, int yroi1, int xroi2, int yroi2)
00052 {
00053 int swap;
00054
00055 if (xroi1>xroi2)
00056 {
00057 swap=xroi2;
00058 xroi2=xroi1;
00059 xroi1=swap;
00060 }
00061 if (yroi1>yroi2)
00062 {
00063 swap=yroi2;
00064 yroi2=yroi1;
00065 yroi1=swap;
00066 }
00067
00068 xroi1=xroi1<0?0:xroi1;
00069 yroi1=yroi1<0?0:yroi1;
00070
00071 xroi2=xroi2>=imagein.getWidth()?imagein.getWidth()-1:xroi2;
00072 yroi2=yroi2>=imagein.getHeight()?imagein.getHeight()-1:yroi2;
00073
00074
00075 bgtemplate.init(xroi2-xroi1+1, yroi2-yroi1+1);
00076
00077 int maxval=INT_MIN, minval=INT_MAX;
00078 for (int y=yroi1, j=0; y<=yroi2; y++, j++)
00079 for (int x=xroi1, i=0; x<=xroi2; x++, i++)
00080 {
00081 const T &value = imagein.pixel(x,y);
00082 if ( value>maxval ) maxval = value;
00083 if ( value<minval ) minval = value;
00084 bgtemplate.pixel(i,j) = value;
00085 }
00086
00087 if ((removeMeans) && (maxval>minval))
00088 {
00089 for (int y=yroi1, j=0; y<=yroi2; y++, j++)
00090 for (int x=xroi1, i=0; x<=xroi2; x++, i++)
00091 bgtemplate.pixel(i,j) = (T)((double)(bgtemplate.pixel(i,j)-minval)/(double)(maxval-minval));
00092 }
00093
00094 std::ofstream outfile( "template.pgm", std::ios::binary );
00095 outfile << bgtemplate;
00096 }
00097
00098 template <typename T>
00099 void bg_subtract<T>::createTemplate(image<T> &imagein)
00100 {
00101 createTemplate(imagein, 0, 0, imagein.getWidth()-1, imagein.getHeight()-1);
00102 }
00103
00104 template <typename T>
00105 void bg_subtract<T>::removeBackground(image<T> &imagein)
00106 {
00107 image<T> tempimage;
00108 double sumerr, err;
00109 int numpix;
00110
00111 tempimage.init(imagein.getWidth(),imagein.getHeight());
00112
00113 for (int y=0; y<imagein.getHeight(); y++)
00114 for (int x=0; x<imagein.getWidth(); x++)
00115 {
00116 sumerr=0.0;
00117 numpix=0;
00118
00119 int maxval=INT_MIN, minval=INT_MAX;
00120
00121 if (removeMeans)
00122 {
00123 for (int yt=-bgtemplate.getHeight()/2; yt<=bgtemplate.getHeight()/2; yt++)
00124 for (int xt=-bgtemplate.getWidth()/2; xt<=bgtemplate.getWidth()/2; xt++)
00125 {
00126 if ((xt+x<0) || (xt+x>=imagein.getWidth())) continue;
00127 if ((yt+y<0) || (yt+y>=imagein.getHeight())) continue;
00128 if (imagein.pixel(xt+x,yt+y)>maxval) maxval=imagein.pixel(xt+x,yt+y);
00129 if (imagein.pixel(xt+x,yt+y)<minval) minval=imagein.pixel(xt+x,yt+y);
00130
00131 }
00132 }
00133
00134 for (int yt=-bgtemplate.getHeight()/2; yt<=bgtemplate.getHeight()/2; yt++)
00135 for (int xt=-bgtemplate.getWidth()/2; xt<=bgtemplate.getWidth()/2; xt++)
00136 {
00137 if ((xt+x<0) || (xt+x>=imagein.getWidth())) continue;
00138 if ((yt+y<0) || (yt+y>=imagein.getHeight())) continue;
00139 numpix++;
00140
00141 if ((removeMeans) && (maxval>minval))
00142 {
00143 err=fabs(((double)(imagein.pixel(xt+x,yt+y)-minval)/(double)(maxval-minval))-
00144 (double)(bgtemplate.pixel(xt+bgtemplate.getWidth()/2,yt+bgtemplate.getHeight()/2)));
00145 }
00146 else
00147 {
00148 err=(double)abs(imagein.pixel(xt+x,yt+y)-
00149 bgtemplate.pixel(xt+bgtemplate.getWidth()/2,yt+bgtemplate.getHeight()/2));
00150 }
00151
00152 sumerr=sumerr+(double)err;
00153
00154 }
00155
00156 if ((sumerr/(double)numpix)<=averageVariation)
00157 tempimage.pixel(x,y)=1;
00158 else
00159 tempimage.pixel(x,y)=0;
00160
00161 }
00162
00163 for (int y=0; y<imagein.getHeight(); y++)
00164 for (int x=0; x<imagein.getWidth(); x++)
00165 if ((tempimage.pixel(x,y))==1)
00166 for (int yt=-bgtemplate.getHeight()/2; yt<=bgtemplate.getHeight()/2; yt++)
00167 for (int xt=-bgtemplate.getWidth()/2; xt<=bgtemplate.getWidth()/2; xt++)
00168 {
00169 if ((xt+x<0) || (xt+x>=imagein.getWidth())) continue;
00170 if ((yt+y<0) || (yt+y>=imagein.getHeight())) continue;
00171
00172 imagein.pixel(x+xt,y+yt)=0;
00173 }
00174 }
00175
00176 }
00177
00178 #endif