diff options
Diffstat (limited to 'SD-VBS/benchmarks/tracking/src/c/calcPyrLKTrack.c')
| -rw-r--r-- | SD-VBS/benchmarks/tracking/src/c/calcPyrLKTrack.c | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/SD-VBS/benchmarks/tracking/src/c/calcPyrLKTrack.c b/SD-VBS/benchmarks/tracking/src/c/calcPyrLKTrack.c new file mode 100644 index 0000000..3718673 --- /dev/null +++ b/SD-VBS/benchmarks/tracking/src/c/calcPyrLKTrack.c | |||
| @@ -0,0 +1,179 @@ | |||
| 1 | /******************************** | ||
| 2 | Author: Sravanthi Kota Venkata | ||
| 3 | ********************************/ | ||
| 4 | |||
| 5 | #include "tracking.h" | ||
| 6 | |||
| 7 | /** calcPyrLKTrack tries to find the displacement (translation in x and y) of features computed in the previous frame. | ||
| 8 | To compute the displacement, we interpolate the existing feature position around the pixel neighborhood. | ||
| 9 | For better results, we perform interpolatations across all pyramid levels. | ||
| 10 | |||
| 11 | Input: Previous frame blurred images, vertical and horizontal | ||
| 12 | Current frame edge images, vertical and horizontal | ||
| 13 | Current frame blurred images, for level 0 and 1 | ||
| 14 | Number of features from previous frame | ||
| 15 | Window size | ||
| 16 | Accuracy | ||
| 17 | Number of iterations to compute motion vector | ||
| 18 | Features from the previous frame | ||
| 19 | currentFrameFeatures, Populate the current frame features' displacements | ||
| 20 | Output: Number of valid features | ||
| 21 | |||
| 22 | **/ | ||
| 23 | |||
| 24 | I2D* calcPyrLKTrack(F2D* previousImageBlur_level1, F2D* previousImageBlur_level2, F2D* vertEdge_level1, F2D* vertEdge_level2, F2D* horzEdge_level1, F2D* horzEdge_level2, F2D* currentImageBlur_level1, F2D* currentImageBlur_level2, F2D* previousFrameFeatures, int nFeatures, int winSize, float accuracy, int max_iter, F2D* currentFrameFeatures) | ||
| 25 | { | ||
| 26 | int idx, level, pLevel, i, j, k, winSizeSq; | ||
| 27 | I2D *valid; | ||
| 28 | F2D *rate, *iPatch, *jPatch, *iDxPatch; | ||
| 29 | F2D *iDyPatch; | ||
| 30 | float tr, x, y, dX, dY, c_xx, c_yy, c_xy; | ||
| 31 | int imgSize_1, imgSize_2; | ||
| 32 | float mX, mY, dIt, eX, eY, c_det; | ||
| 33 | I2D *imgDims; | ||
| 34 | |||
| 35 | imgDims = iMallocHandle(2, 2); | ||
| 36 | subsref(imgDims,0,0) = previousImageBlur_level1->height; | ||
| 37 | subsref(imgDims,0,1) = previousImageBlur_level1->width; | ||
| 38 | subsref(imgDims,1,0) = previousImageBlur_level2->height; | ||
| 39 | subsref(imgDims,1,1) = previousImageBlur_level2->width; | ||
| 40 | |||
| 41 | pLevel = 2; | ||
| 42 | rate = fMallocHandle(1, 6); | ||
| 43 | |||
| 44 | asubsref(rate,0) = 1; | ||
| 45 | asubsref(rate,1) = 0.5; | ||
| 46 | asubsref(rate,2) = 0.25; | ||
| 47 | asubsref(rate,3) = 0.125; | ||
| 48 | asubsref(rate,4) = 0.0625; | ||
| 49 | asubsref(rate,5) = 0.03125; | ||
| 50 | |||
| 51 | winSizeSq = 4*winSize*winSize; | ||
| 52 | valid = iSetArray(1,nFeatures, 1); | ||
| 53 | |||
| 54 | /** For each feature passed from previous frame, compute the dx and dy, the displacements **/ | ||
| 55 | for(i=0; i<nFeatures; i++) | ||
| 56 | { | ||
| 57 | dX = 0; | ||
| 58 | dY = 0; | ||
| 59 | |||
| 60 | /** Compute the x and y co-ordinate values at "pLevel" **/ | ||
| 61 | x = subsref(previousFrameFeatures,0,i) * asubsref(rate,pLevel); | ||
| 62 | y = subsref(previousFrameFeatures,1,i) * asubsref(rate,pLevel); | ||
| 63 | c_det = 0; | ||
| 64 | |||
| 65 | /** For each pyramid level, try to find correspondence. | ||
| 66 | We look for the correspondence in a given window size | ||
| 67 | , (winSize x winSize) neighborhood **/ | ||
| 68 | for(level = pLevel-1; level>=0; level--) | ||
| 69 | { | ||
| 70 | x = x+x; | ||
| 71 | y = y+y; | ||
| 72 | dX = dX + dX; | ||
| 73 | dY = dY + dY; | ||
| 74 | imgSize_1 = subsref(imgDims,level,0); | ||
| 75 | imgSize_2 = subsref(imgDims,level,1); | ||
| 76 | |||
| 77 | c_xx = 0; | ||
| 78 | c_xy = 0; | ||
| 79 | c_yy = 0; | ||
| 80 | |||
| 81 | if((x-winSize)<0 || (y-winSize)<0 || (y+winSize+1)>=imgSize_1 || (x+winSize+1)>=imgSize_2) | ||
| 82 | { | ||
| 83 | asubsref(valid,i) = 0; | ||
| 84 | break; | ||
| 85 | } | ||
| 86 | |||
| 87 | /** Perform interpolation. Use co-ord from previous | ||
| 88 | frame and use the images from current frame **/ | ||
| 89 | |||
| 90 | if(level ==0) | ||
| 91 | { | ||
| 92 | iPatch = getInterpolatePatch(previousImageBlur_level1, imgSize_2, x, y, winSize); | ||
| 93 | iDxPatch = getInterpolatePatch(vertEdge_level1, imgSize_2, x, y, winSize); | ||
| 94 | iDyPatch = getInterpolatePatch(horzEdge_level1, imgSize_2, x, y, winSize); | ||
| 95 | } | ||
| 96 | if(level ==1) | ||
| 97 | { | ||
| 98 | iPatch = getInterpolatePatch(previousImageBlur_level2, imgSize_2, x, y, winSize); | ||
| 99 | iDxPatch = getInterpolatePatch(vertEdge_level2, imgSize_2, x, y, winSize); | ||
| 100 | iDyPatch = getInterpolatePatch(horzEdge_level2, imgSize_2, x, y, winSize); | ||
| 101 | } | ||
| 102 | |||
| 103 | /** Compute feature strength in similar way as calcGoodFeature **/ | ||
| 104 | for(idx=0; idx<winSizeSq; idx++) | ||
| 105 | { | ||
| 106 | c_xx += asubsref(iDxPatch,idx) * asubsref(iDxPatch,idx); | ||
| 107 | c_xy += asubsref(iDxPatch,idx) * asubsref(iDyPatch,idx); | ||
| 108 | c_yy += asubsref(iDyPatch,idx) * asubsref(iDyPatch,idx); | ||
| 109 | } | ||
| 110 | |||
| 111 | c_det = (c_xx * c_yy -c_xy * c_xy); | ||
| 112 | tr = c_xx + c_yy; | ||
| 113 | |||
| 114 | if((c_det/(tr+0.00001)) < accuracy) | ||
| 115 | { | ||
| 116 | asubsref(valid,i) = 0; | ||
| 117 | fFreeHandle(iPatch); | ||
| 118 | fFreeHandle(iDxPatch); | ||
| 119 | fFreeHandle(iDyPatch); | ||
| 120 | break; | ||
| 121 | } | ||
| 122 | c_det = 1/c_det; | ||
| 123 | |||
| 124 | /** We compute dX and dY using previous frame and current | ||
| 125 | frame images. For this, the strength is computed at each | ||
| 126 | pixel in the new image. **/ | ||
| 127 | for(k=0; k<max_iter; k++) | ||
| 128 | { | ||
| 129 | if( (x+dX-winSize)<0 || (y+dY-winSize)<0 || (y+dY+winSize+1)>=imgSize_1 || (x+dX+winSize+1)>=imgSize_2) | ||
| 130 | { | ||
| 131 | asubsref(valid,i) = 0; | ||
| 132 | break; | ||
| 133 | } | ||
| 134 | |||
| 135 | if(level == 0) | ||
| 136 | jPatch = getInterpolatePatch(currentImageBlur_level1, imgSize_2, x+dX, y+dY, winSize); | ||
| 137 | if(level == 1) | ||
| 138 | jPatch = getInterpolatePatch(currentImageBlur_level2, imgSize_2, x+dX, y+dY, winSize); | ||
| 139 | |||
| 140 | eX = 0; | ||
| 141 | eY = 0; | ||
| 142 | for(idx=0; idx<winSizeSq; idx++) | ||
| 143 | { | ||
| 144 | dIt = asubsref(iPatch,idx) - asubsref(jPatch,idx); | ||
| 145 | eX += dIt * asubsref(iDxPatch,idx); | ||
| 146 | eY += dIt * asubsref(iDyPatch,idx); | ||
| 147 | } | ||
| 148 | |||
| 149 | mX = c_det * (eX * c_yy - eY * c_xy); | ||
| 150 | mY = c_det * (-eX * c_xy + eY * c_xx); | ||
| 151 | dX = dX + mX; | ||
| 152 | dY = dY + mY; | ||
| 153 | |||
| 154 | if( (mX*mX+mY*mY) < accuracy) | ||
| 155 | { | ||
| 156 | fFreeHandle(jPatch); | ||
| 157 | break; | ||
| 158 | } | ||
| 159 | |||
| 160 | fFreeHandle(jPatch); | ||
| 161 | } | ||
| 162 | |||
| 163 | fFreeHandle(iPatch); | ||
| 164 | fFreeHandle(iDxPatch); | ||
| 165 | fFreeHandle(iDyPatch); | ||
| 166 | } | ||
| 167 | |||
| 168 | subsref(currentFrameFeatures,0,i) = subsref(previousFrameFeatures,0,i) + dX; | ||
| 169 | subsref(currentFrameFeatures,1,i) = subsref(previousFrameFeatures,1,i) + dY; | ||
| 170 | |||
| 171 | } | ||
| 172 | |||
| 173 | fFreeHandle(rate); | ||
| 174 | iFreeHandle(imgDims); | ||
| 175 | return valid; | ||
| 176 | } | ||
| 177 | |||
| 178 | |||
| 179 | |||
