void OpticalFlow::Laplacian(DImage &output, const DImage &input, const DImage& weight) { if(output.matchDimension(input)==false) output.allocate(input); output.reset(); if(input.matchDimension(weight)==false) { cout<<"Error in image dimension matching OpticalFlow::Laplacian()!"<<endl; return; } const _FlowPrecision *inputData=input.data(),*weightData=weight.data(); int width=input.width(),height=input.height(); DImage foo(width,height); _FlowPrecision *fooData=foo.data(),*outputData=output.data(); // horizontal filtering for(int i=0;i<height;i++) for(int j=0;j<width-1;j++) { int offset=i*width+j; fooData[offset]=(inputData[offset+1]-inputData[offset])*weightData[offset]; } for(int i=0;i<height;i++) for(int j=0;j<width;j++) { int offset=i*width+j; if(j<width-1) outputData[offset]-=fooData[offset]; if(j>0) outputData[offset]+=fooData[offset-1]; } foo.reset(); // vertical filtering for(int i=0;i<height-1;i++) for(int j=0;j<width;j++) { int offset=i*width+j; fooData[offset]=(inputData[offset+width]-inputData[offset])*weightData[offset]; } for(int i=0;i<height;i++) for(int j=0;j<width;j++) { int offset=i*width+j; if(i<height-1) outputData[offset]-=fooData[offset]; if(i>0) outputData[offset]+=fooData[offset-width]; } }
void OpticalFlow::genInImageMask(DImage &mask, const DImage &flow,int interval) { int imWidth,imHeight; imWidth=flow.width(); imHeight=flow.height(); if(mask.matchDimension(flow.width(),flow.height(),1)==false) mask.allocate(imWidth,imHeight); else mask.reset(); const _FlowPrecision *pFlow; _FlowPrecision *pMask; pFlow = flow.data();; pMask=mask.data(); double x,y; for(int i=0;i<imHeight;i++) for(int j=0;j<imWidth;j++) { int offset=i*imWidth+j; y=i+pFlow[offset*2+1]; x=j+pFlow[offset*2]; if(x<interval || x>imWidth-1-interval || y<interval || y>imHeight-1-interval) continue; pMask[offset]=1; } }
//-------------------------------------------------------------------------------------------------------- // function to generate mask of the pixels that move inside the image boundary //-------------------------------------------------------------------------------------------------------- void OpticalFlow::genInImageMask(DImage &mask, const DImage &vx, const DImage &vy,int interval) { int imWidth,imHeight; imWidth=vx.width(); imHeight=vx.height(); if(mask.matchDimension(vx)==false) mask.allocate(imWidth,imHeight); const _FlowPrecision *pVx,*pVy; _FlowPrecision *pMask; pVx=vx.data(); pVy=vy.data(); mask.reset(); pMask=mask.data(); double x,y; for(int i=0;i<imHeight;i++) for(int j=0;j<imWidth;j++) { int offset=i*imWidth+j; y=i+pVx[offset]; x=j+pVy[offset]; if(x<interval || x>imWidth-1-interval || y<interval || y>imHeight-1-interval) continue; pMask[offset]=1; } }
//-------------------------------------------------------------------------------------------------------- // function to do sanity check: imdx*du+imdy*dy+imdt=0 //-------------------------------------------------------------------------------------------------------- void OpticalFlow::SanityCheck(const DImage &imdx, const DImage &imdy, const DImage &imdt, double du, double dv) { if(imdx.matchDimension(imdy)==false || imdx.matchDimension(imdt)==false) { cout<<"The dimensions of the derivatives don't match!"<<endl; return; } const _FlowPrecision* pImDx,*pImDy,*pImDt; pImDx=imdx.data(); pImDy=imdy.data(); pImDt=imdt.data(); double error=0; for(int i=0;i<imdx.height();i++) for(int j=0;j<imdx.width();j++) for(int k=0;k<imdx.nchannels();k++) { int offset=(i*imdx.width()+j)*imdx.nchannels()+k; double temp=pImDx[offset]*du+pImDy[offset]*dv+pImDt[offset]; error+=fabs(temp); } error/=imdx.nelements(); cout<<"The mean error of |dx*u+dy*v+dt| is "<<error<<endl; }
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; }
void OpticalFlow::warpFL(DImage &warpIm2, const DImage &Im1, const DImage &Im2, const DImage &Flow) { if(warpIm2.matchDimension(Im2)==false) warpIm2.allocate(Im2.width(),Im2.height(),Im2.nchannels()); ImageProcessing::warpImageFlow(warpIm2.data(),Im1.data(),Im2.data(),Flow.data(),Im2.width(),Im2.height(),Im2.nchannels()); }