void OpticalFlow::estLaplacianNoise(const DImage& Im1,const DImage& Im2,Vector<double>& para) { int nChannels = Im1.nchannels(); if(para.dim()!=nChannels) para.allocate(nChannels); else para.reset(); double temp; Vector<double> total(nChannels); for(int k = 0;k<nChannels;k++) total[k] = 0; for(int i =0;i<Im1.npixels();i++) for(int k = 0;k<nChannels;k++) { int offset = i*nChannels+k; temp= abs(Im1.data()[offset]-Im2.data()[offset]); if(temp>0 && temp<1000000) { para[k] += temp; total[k]++; } } for(int k = 0;k<nChannels;k++) { if(total[k]==0) { cout<<"All the pixels are invalid in estimation Laplacian noise!!!"<<endl; cout<<"Something severely wrong happened!!!"<<endl; para[k] = 0.001; } else para[k]/=total[k]; } }
bool OpticalFlow::SaveOpticalFlow(const DImage& flow,ofstream& myfile) { Image<unsigned short int> foo; foo.allocate(flow); for(int i =0;i<flow.npixels();i++) { foo.data()[i*2] = (__min(__max(flow.data()[i*2],-200),200)+200)*160; foo.data()[i*2+1] = (__min(__max(flow.data()[i*2+1],-200),200)+200)*160; } return foo.saveImage(myfile); }
bool OpticalFlow::LoadOpticalFlow(ifstream& myfile,DImage& flow) { Image<unsigned short int> foo; if(foo.loadImage(myfile) == false) return false; if(!flow.matchDimension(foo)) flow.allocate(foo); for(int i = 0;i<flow.npixels();i++) { flow.data()[i*2] = (double)foo.data()[i*2]/160-200; flow.data()[i*2+1] = (double)foo.data()[i*2+1]/160-200; } return true; }
bool OpticalFlow::showFlow(const DImage& flow,const char* filename) { if(flow.nchannels()!=1) { cout<<"The flow must be a single channel image!"<<endl; return false; } Image<unsigned char> foo; foo.allocate(flow.width(),flow.height()); double Max = flow.max(); double Min = flow.min(); for(int i = 0;i<flow.npixels(); i++) foo[i] = (flow[i]-Min)/(Max-Min)*255; foo.imwrite(filename); }