vector<double> toVector(const ImageFeature &image) { vector<double> result(image.size()); for(uint i=0;i<image.size();++i) { result[i]=image[i]; } return result; }
void histogramNormalization(ImageFeature &img) { const uint bins=256; vector<int> H(bins); vector<double> T(bins); for(uint c=0;c<img.zsize();++c) { //DBG(10) << "c=" << c << endl; for(uint x=0;x<img.xsize();++x) { for(uint y=0;y<img.ysize();++y) { ++H[int(img(x,y,c)*255)]; } } for(uint p=1;p<bins;++p) { H[p]+=H[p-1]; } for(uint p=0;p<bins;++p) { T[p]=(1.0/double(img.size()))*double(H[p]); } for(uint x=0;x<img.xsize();++x) { for(uint y=0;y<img.ysize();++y) { img(x,y,c)=T[int(img(x,y,c)*255)]; } } } }
int main(int argc , char **argv) { GetPot cl(argc,argv); if(cl.search("-h")) {USAGE(); exit(0);} string line; if(cl.search("-E")) { cout << "Estimating PCA" << endl; ifstream filelist(cl.follow("filelist","-E")); ImageFeature img; getline(filelist,line); img.load(line,true); cout << line << endl; PCA pca(img.size()); cout << img.size() << endl; pca.putData(img.layerVector(0)); while(getline(filelist,line)) { cout << line << endl; img.load(line,true); pca.putData(img.layerVector(0)); } pca.dataEnd(); pca.save(cl.follow("covariance.pca","-c")); pca.calcPCA(); pca.save(cl.follow("transformation.pca","-t")); filelist.close(); } else if(cl.search("-T")) { PCA pca; pca.load(cl.follow("transformation.pca","-t")); int dim=cl.follow(20,"-d"); ifstream filelist(cl.follow("filelist","-T")); vector<double> tmp; ImageFeature img; while(getline(filelist,line)) { cout << line << endl; img.load(line,true); tmp=pca.transform(img.layerVector(0),img.size()); tmp.resize(dim); VectorFeature tmpvec(tmp); tmpvec.save(line+".pca.vec.gz"); } filelist.close(); } else if(cl.search("-B")) { PCA pca; pca.load(cl.follow("transformation.pca","-t")); int x=cl.follow(16,"-x"); int y=cl.follow(16,"-y"); ifstream filelist(cl.follow("filelist","-B")); vector<double> tmp; VectorFeature tmpvec; while(getline(filelist,line)) { cout << line << endl; tmpvec.load(line); vector<double> tmp; tmp=pca.backTransform(tmpvec.data()); ImageFeature img(tmp,x,y); cutoff(img); img.save(line+".backpca.png"); } filelist.close(); } else if(cl.search("-M")) { ImageFeature img; img.load(cl.follow("image.png","-M"),true); PCA pca; vector<double> backproj,vec; pca.load(cl.follow("transformation.pca","-t")); int w=cl.follow(16,"-x"); int h=cl.follow(16,"-y"); double scalefac=cl.follow(0.8333,"-s"); int dim=cl.follow(20,"-d"); ImageFeature scimg(img); ImageFeature patch; uint minX=100000, minY=100000; uint maxX=100000, maxY=100000; while(int(scimg.xsize())>=w and int(scimg.ysize())>=h) { DBG(10) << VAR(scimg.xsize()) << " x " << VAR(scimg.ysize()) << endl; ImageFeature faceprobmap(scimg.xsize(),scimg.ysize(),1); double maxDist=0.0; double minDist=numeric_limits<double>::max(); vector<double> tmpvec; for(uint x=0;x<scimg.xsize();++x) { DBG(10) << VAR(x) << endl; for(uint y=0;y<scimg.ysize();++y) { patch=getPatch(scimg,x,y,x+w,y+h); vec=patch.layerVector(0); vector<double> imgMinMean=vec; for(uint i=0;i<imgMinMean.size();++i) { imgMinMean[i]-=pca.mean()[i]; } double energyImg=getEnergy(imgMinMean); tmpvec=pca.transform(vec,dim); double energyTrans=getEnergy(tmpvec); backproj=pca.backTransform(tmpvec); double energyBack=getEnergy(backproj); double d=0; double tmp; for(uint i=0;i<backproj.size();++i) { tmp=backproj[i]-vec[i]; d+=tmp*tmp; } faceprobmap(x,y,0)=d; DBG(10) << VAR(energyImg) << " " << VAR(energyTrans) << " " << VAR(energyBack) << " " << VAR(energyImg-energyTrans) << " " << VAR(d) << endl; if(minDist>d) { minDist=d; minX=x; minY=y; } if(maxDist<d) { maxDist=d; maxX=x; maxY=y; } } } DBG(10) << VAR(scimg.xsize()) << " " << VAR(scimg.ysize()) << endl; DBG(10) << VAR(minDist) << " " << VAR(minX) << " " << VAR(minY) << endl; DBG(10) << VAR(maxDist) << " " << VAR(maxX) << " " << VAR(maxY) << endl << endl; normalize(faceprobmap); ostringstream filenamestream; filenamestream << cl.follow("image.png","-M") << ".fpm." << (scimg.xsize()) <<".png"; faceprobmap.save(filenamestream.str()); uint newW=int(scimg.xsize()*scalefac); uint newH=int(scimg.ysize()*scalefac); scimg=scale(scimg,newW,newH); } } else if(cl.search("-F")) { #ifdef HAVE_FFT_LIBRARY ImageFeature img; img.load(cl.follow("image.png","-F"),true); PCA pca; pca.load(cl.follow("transformation.pca","-t")); int w=cl.follow(16,"-x"); int h=cl.follow(16,"-y"); double scalefac=cl.follow(0.8333,"-s"); uint dim=cl.follow(20,"-d"); DBG(10) << "Eigenfaces loaded" << endl; ImageFeature scimg=img; while(int(scimg.xsize())>w and int(scimg.ysize())>h) { ImageFeature fpm=detect(scimg,pca,dim,w,h); pair<uint,uint> p; p=argmax(fpm); DBG(10) << scimg.xsize() << "x" << scimg.ysize() << " (" <<p.first<<", "<< p.second << ") " << VAR(maximum(fpm)) ; p=argmin(fpm); BLINK(10) << " (" <<p.first<<", "<< p.second << ") " << VAR(minimum(fpm)) << endl; normalize(fpm); ostringstream filenamestream; filenamestream << cl.follow("image.png","-F") << ".fpm." << (scimg.xsize()) <<".png"; fpm.save(filenamestream.str()); scimg=scale(scimg,int(scimg.xsize()*scalefac),int(scimg.ysize()*scalefac)); } #else DBG(10) << "compiled without FFT lib. this does not work. use -M option" << endl; #endif } else { USAGE(); exit(20); } }