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; } }
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); }
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]; } }
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; }
//-------------------------------------------------------------------------------------------------------- // 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; }
//--------------------------------------------------------------------------------------- // function to convert image to feature image //--------------------------------------------------------------------------------------- void OpticalFlow::im2feature(DImage &imfeature, const DImage &im) { int width=im.width(); int height=im.height(); int nchannels=im.nchannels(); if(nchannels==1) { imfeature.allocate(im.width(),im.height(),3); DImage imdx,imdy; im.dx(imdx,true); im.dy(imdy,true); _FlowPrecision* data=imfeature.data(); for(int i=0;i<height;i++) for(int j=0;j<width;j++) { int offset=i*width+j; data[offset*3]=im.data()[offset]; data[offset*3+1]=imdx.data()[offset]; data[offset*3+2]=imdy.data()[offset]; } } else if(nchannels==3) { DImage grayImage; im.desaturate(grayImage); imfeature.allocate(im.width(),im.height(),5); DImage imdx,imdy; grayImage.dx(imdx,true); grayImage.dy(imdy,true); _FlowPrecision* data=imfeature.data(); for(int i=0;i<height;i++) for(int j=0;j<width;j++) { int offset=i*width+j; data[offset*5]=grayImage.data()[offset]; data[offset*5+1]=imdx.data()[offset]; data[offset*5+2]=imdy.data()[offset]; data[offset*5+3]=im.data()[offset*3+1]-im.data()[offset*3]; data[offset*5+4]=im.data()[offset*3+1]-im.data()[offset*3+2]; } } else imfeature.copyData(im); }
cv::Mat ParImageToIplImage(DImage& img) { int width = img.width(); int height = img.height(); int nChannels = img.nchannels(); if(width <= 0 || height <= 0 || nChannels != 1) return cv::Mat(); BaseType*& pData = img.data(); cv::Mat image = cv::Mat(height, width, CV_MAKETYPE(8, 1)); for(int i = 0;i < height;i++) { for(int j = 0;j < width;j++) { image.ptr<uchar>(i)[j] = pData[i*width + j] * 255; } } return image; }
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()); }