#include "lagrcv.h" /* void calcImagePyr(char *src, int sizeY, int sizeX, int level, char *pyr){ pyr=(char*) malloc(sizeof(char)*(int)sizeY*sizeX*4/3); int i, startPnt, nX, nY; memcpy(pyr,src, sizeY*sizeX); nY=sizeY; nX=sizeX; for(i=0; i> 1; nX = (nX + 1) >> 1; calcSubsample(pyr, sizeY, sizeX, pyr+sizeY*sizeX) } free(pyr); } */ void calcSubSampleAvg(double *src, int sizeY, int sizeX, double *dest, int destSizeY, int destSizeX){ int i, j, idx, destI, destJ, idxDest; for(i=0, destI=0; destI0)?sum[idx-1]:0) + ((i>0)?sum[idx-sizeY]:0) - ((i>0&&j>0)?sum[idx-sizeY-1]:0); } } */ idxDelta[0]=halfSize*sizeY+halfSize; idxDelta[1]=-halfSize*sizeY+halfSize; idxDelta[2]=halfSize*sizeY-halfSize; idxDelta[3]=-halfSize*sizeY-halfSize; for(i=halfSize; i=0; level--){ x+=x; y+=y; dX+=dX; dY+=dY; imgSize[0]=imgDims[level*2+0]; //y,x imgSize[1]=imgDims[level*2+1]; //y,x c_xx=c_xy=c_yy=0; //when feature goes out to the boundary. if((x-winSize-1)<0 || (y-winSize-1)<0 || (y+winSize+1+1)>=imgSize[0] || (x+winSize+1+1)>=imgSize[1]){ //winSize+1due to interpolation //error or skip the level?? valid[i]=0; break; } getInterpolatePatch(iP[level], imgSize, x, y, winSize+1, iPatch); //to calculate iDx, iDy calcSobel(iPatch, (winSize+1)*2, (winSize+1)*2, iDxPatch, iDyPatch); for(k=0; k <(winSize*2); k++ ){ memcpy( iPatch + k*winSize*2, iPatch + (k+1)*(winSize+1)*2 + 1, winSize*2 ); memcpy( iDxPatch + k*winSize*2, iDxPatch + (k+1)*(winSize+1)*2 + 1, winSize*2 ); memcpy( iDyPatch + k*winSize*2, iDyPatch + (k+1)*(winSize+1)*2 + 1, winSize*2 ); } for(idx=0; idx=imgSize[0] || (x+dX+winSize+1)>=imgSize[1]){ //winSize+1due to interpolation //error or skip the level?? valid[i]=0; break; } getInterpolatePatch(jP[level], imgSize, x+dX, y+dY, winSize, jPatch); eX=eY=0; for(idx=0;idx0 && (mX + prevMX) < 0.01 && (mX+prevMX) > -0.01 && (mY + prevMY) < 0.01 && (mY+prevMY) > -0.01) { dX -= mX*0.5f; dY -= mY*0.5f; break; } prevMX=mX; prevMY=mY; } if(k==max_iter){ valid[i]=0; } } newFPnt[i*2+0]=fPnt[i*2+0]+dX; newFPnt[i*2+1]=fPnt[i*2+1]+dY; /* if(valid[i] || (x+dX-winSize)<0 || (y+dY-winSize)<0 || (y+dY+winSize+1)>=imgSize[0] || (x+dX+winSize+1)>=imgSize[1]){ newFPnt[i*2+0]=fPnt[i*2+0]+dX; newFPnt[i*2+1]=fPnt[i*2+1]+dY; getInterpolatePatch(jP[0], imgSize, newFPnt[i*2+0], newFPnt[i*2+0], winSize, jPatch); dIt=0; for(idx=0;idxwinSizeSq*50000){ valid[i]=0; } }else{ newFPnt[i*2+0]=0; newFPnt[i*2+1]=0; } */ } free(iPatch); free(jPatch); free(iDxPatch); free(iDyPatch); } void calcPyrLKTrackWInit(double** iP, double** iDxP, double** iDyP, double** jP, const int* imgDims, int pLevel, double* fPnt, int nFeatures, int winSize, double* newFPnt, double* initFPnt, char* valid, double accuracy_th, int max_iter){ double x, y, eX, eY, dX, dY, mX, mY, prevMX, prevMY, c_xx, c_xy, c_yy, c_det, dIt; double* iPatch, *jPatch, *iDxPatch, *iDyPatch; int level, winSizeSq; int i, k, idx; int imgSize[2]; static const double rate[]={1, 0.5, 0.25, 0.125, 0.0625, 0.03125}; winSizeSq=4*winSize*winSize; iPatch=(double*) malloc(sizeof(double)*winSizeSq); jPatch=(double*) malloc(sizeof(double)*winSizeSq); iDxPatch=(double*) malloc(sizeof(double)*winSizeSq); iDyPatch=(double*) malloc(sizeof(double)*winSizeSq); for(i=0; i=0; level--){ x+=x; y+=y; dX+=dX; dY+=dY; imgSize[0]=imgDims[level*2+0]; //y,x imgSize[1]=imgDims[level*2+1]; //y,x c_xx=c_xy=c_yy=0; //when feature goes out to the boundary. if((x-winSize)<0 || (y-winSize)<0 || (y+winSize+1)>=imgSize[0] || (x+winSize+1)>=imgSize[1]){ //winSize+1due to interpolation //error or skip the level?? valid[i]=0; break; } getInterpolatePatch(iP[level], imgSize, x, y, winSize, iPatch); getInterpolatePatch(iDxP[level], imgSize, x, y, winSize, iDxPatch); getInterpolatePatch(iDyP[level], imgSize, x, y, winSize, iDyPatch); for(idx=0; idx=imgSize[0] || (x+dX+winSize+1)>=imgSize[1]){ //winSize+1due to interpolation //error or skip the level?? valid[i]=0; break; } getInterpolatePatch(jP[level], imgSize, x+dX, y+dY, winSize, jPatch); eX=eY=0; for(idx=0;idx0 && (mX + prevMX) < 0.01 && (mX+prevMX) > -0.01 && (mY + prevMY) < 0.01 && (mY+prevMY) > -0.01) { dX -= mX*0.5f; dY -= mY*0.5f; break; } prevMX=mX; prevMY=mY; } if(k==max_iter){ valid[i]=0; } } newFPnt[i*2+0]=fPnt[i*2+0]+dX; newFPnt[i*2+1]=fPnt[i*2+1]+dY; /* if(valid[i] || (x+dX-winSize)<0 || (y+dY-winSize)<0 || (y+dY+winSize+1)>=imgSize[0] || (x+dX+winSize+1)>=imgSize[1]){ newFPnt[i*2+0]=fPnt[i*2+0]+dX; newFPnt[i*2+1]=fPnt[i*2+1]+dY; getInterpolatePatch(jP[0], imgSize, newFPnt[i*2+0], newFPnt[i*2+0], winSize, jPatch); dIt=0; for(idx=0;idxwinSizeSq*50000){ valid[i]=0; } }else{ newFPnt[i*2+0]=0; newFPnt[i*2+1]=0; } */ } free(iPatch); free(jPatch); free(iDxPatch); free(iDyPatch); } void calcPyrLKTrack(double** iP, double** iDxP, double** iDyP, double** jP, const int* imgDims, int pLevel, double* fPnt, int nFeatures, int winSize, double* newFPnt, char* valid, double accuracy_th, int max_iter){ double x, y, eX, eY, dX, dY, mX, mY, c_xx, c_xy, c_yy, c_det, dIt; double* iPatch, *jPatch, *iDxPatch, *iDyPatch; int level, winSizeSq; int i, k, idx; int imgSize[2]; static const double rate[]={1, 0.5, 0.25, 0.125, 0.0625, 0.03125}; winSizeSq=4*winSize*winSize; iPatch=(double*) malloc(sizeof(double)*winSizeSq); jPatch=(double*) malloc(sizeof(double)*winSizeSq); iDxPatch=(double*) malloc(sizeof(double)*winSizeSq); iDyPatch=(double*) malloc(sizeof(double)*winSizeSq); for(i=0; i=0; level--){ x+=x; y+=y; dX+=dX; dY+=dY; imgSize[0]=imgDims[level*2+0]; //y,x imgSize[1]=imgDims[level*2+1]; //y,x c_xx=c_xy=c_yy=0; //when feature goes out to the boundary. if((x-winSize)<0 || (y-winSize)<0 || (y+winSize+1)>=imgSize[0] || (x+winSize+1)>=imgSize[1]){ //winSize+1due to interpolation //error or skip the level?? valid[i]=0; break; } getInterpolatePatch(iP[level], imgSize, x, y, winSize, iPatch); getInterpolatePatch(iDxP[level], imgSize, x, y, winSize, iDxPatch); getInterpolatePatch(iDyP[level], imgSize, x, y, winSize, iDyPatch); for(idx=0; idx=imgSize[0] || (x+dX+winSize+1)>=imgSize[1]){ //winSize+1due to interpolation //error or skip the level?? valid[i]=0; break; } getInterpolatePatch(jP[level], imgSize, x+dX, y+dY, winSize, jPatch); eX=eY=0; for(idx=0;idx=imgSize[0] || (x+dX+winSize+1)>=imgSize[1]){ newFPnt[i*2+0]=fPnt[i*2+0]+dX; newFPnt[i*2+1]=fPnt[i*2+1]+dY; getInterpolatePatch(jP[0], imgSize, newFPnt[i*2+0], newFPnt[i*2+0], winSize, jPatch); dIt=0; for(idx=0;idxwinSizeSq*50000){ valid[i]=0; } }else{ newFPnt[i*2+0]=0; newFPnt[i*2+1]=0; } */ } free(iPatch); free(jPatch); free(iDxPatch); free(iDyPatch); } void getPatch(double* srcImg, const int* srcDims, double centerX, double centerY, int winSize, double** dstImg){ int srcIdxX, srcIdxY, dstIdxX; srcIdxY=(int)centerY-winSize; for(int i=-winSize; i=imdims[0] || (x+winSize)>=imdims[1]){ //error or skip the level?? valid[i]=0; continue; } getPatch(imgI, imdims, x, y, winSize, iPatch); getPatch(iDx, imdims, x, y, winSize, iDxPatch); getPatch(iDy, imdims, x, y, winSize, iDyPatch); idx=(int)x*imdims[0]+(int)y; c_det=c_xx[i]*c_yy[i]-c_xy[i]*c_xy[i]; if(c_det/(c_xx[i]+c_yy[i]+0.00000001)=imdims[0] || (x+dX+winSize+1)>=imdims[1]){ //winSize+1due to interpolation //error or skip the level?? valid[i]=0; break; } getInterpolatePatch(imgJ, imgSize, x+dX, y+dY, winSize, jPatch); eX=eY=0; for(idxCol=0;idxCol<2*winSize;idxCol++){ for(idxRow=0;idxRow<2*winSize;idxRow++){ dIt=iPatch[idxCol][idxRow]-jPatch[idxCol*winSize*2+idxRow]; eX+=dIt*iDxPatch[idxCol][idxRow]; eY+=dIt*iDyPatch[idxCol][idxRow]; } } mX=c_det*(eX*c_yy[i]-eY*c_xy[i]); mY=c_det*(-eX*c_xy[i]+eY*c_xx[i]); dX+=mX; dY+=mY; if((mX*mX+mY*mY)