summaryrefslogtreecommitdiffstats
path: root/SD-VBS/benchmarks/tracking/src/c/script_tracking.c
blob: bb48acece28fe61de0c0211ba45c540537e03fa0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/********************************
Author: Sravanthi Kota Venkata
********************************/

#include "tracking.h"
#include <malloc.h>
#include "extra.h"
#define TRACKING_MEM 1<<29

int main(int argc, char* argv[])
{
    SET_UP
    mallopt(M_TOP_PAD, TRACKING_MEM);
    mallopt(M_MMAP_MAX, 0);
    int i, j, k, N_FEA, WINSZ, LK_ITER, rows, cols;
    int endR, endC;
    F2D *blurredImage, *previousFrameBlurred_level1, *previousFrameBlurred_level2, *blurred_level1, *blurred_level2;
    F2D *verticalEdgeImage, *horizontalEdgeImage, *verticalEdge_level1, *verticalEdge_level2, *horizontalEdge_level1, *horizontalEdge_level2, *interestPnt;
    F2D *lambda, *lambdaTemp, *features;
    I2D *Ic, *status;
    float SUPPRESION_RADIUS;
    F2D *newpoints;

    int numFind, m, n;
    F2D *np_temp;

    char im1[100];
    int counter=2;
    float accuracy = 0.03;
    int count;
    

    N_FEA = 1600;
    WINSZ = 4;
    SUPPRESION_RADIUS = 10.0;
    LK_ITER = 20;

    #ifdef test
        WINSZ = 2;
        N_FEA = 100;
        LK_ITER = 2;
        counter = 2;
        accuracy = 0.1;
    #endif
    #ifdef sim_fast
        WINSZ = 2;
        N_FEA = 100;
        LK_ITER = 2;
        counter = 4;
    #endif
    #ifdef sim
        WINSZ = 2;
        N_FEA = 200;
        LK_ITER = 2;
        counter = 4;
    #endif
    #ifdef sqcif
        WINSZ = 8;
        N_FEA = 500;
        LK_ITER = 15;
        counter = 2;
    #endif
    #ifdef qcif
        WINSZ = 12;
        N_FEA = 400;
        LK_ITER = 15;
        counter = 4;
    #endif
    #ifdef cif
        WINSZ = 20;
        N_FEA = 500;
        LK_ITER = 20;
        counter = 4;
    #endif
    #ifdef vga
        WINSZ = 32;
        N_FEA = 400;
        LK_ITER = 20;
        counter = 4;
    #endif
    #ifdef wuxga
        WINSZ = 64;
        N_FEA = 500;
        LK_ITER = 20;
        counter = 4;
    #endif
    #ifdef fullhd
        WINSZ = 48;
        N_FEA = 500;
        LK_ITER = 20;
        counter = 4;
    #endif

    I2D* images[counter];
    /** Read input image **/
    for(count=1; count<=counter; count++)
    {
        /** Read image **/
	printf("Input image %d: ", count);
	scanf("%s", im1);
        images[count - 1] = readImage(im1);
	if(count == 1) Ic = readImage(im1);
    }


    rows = Ic->height;
    cols = Ic->width;
    
    printf("start\n");   
    for_each_job{
        
        /** IMAGE PRE-PROCESSING **/
    
        /** Blur the image to remove noise - weighted avergae filter **/
        blurredImage = imageBlur(Ic);
    
        /** Scale down the image to build Image Pyramid. We find features across all scales of the image **/
        blurred_level1 = blurredImage;                   /** Scale 0 **/
        blurred_level2 = imageResize(blurredImage);      /** Scale 1 **/
    
    
        /** Edge Images - From pre-processed images, build gradient images, both horizontal and vertical **/
        verticalEdgeImage = calcSobel_dX(blurredImage);
        horizontalEdgeImage = calcSobel_dY(blurredImage);
    
        /** Edge images are used for feature detection. So, using the verticalEdgeImage and horizontalEdgeImage images, we compute feature strength
            across all pixels. Lambda matrix is the feature strength matrix returned by calcGoodFeature **/
        
        lambda = calcGoodFeature(verticalEdgeImage, horizontalEdgeImage, verticalEdgeImage->width, verticalEdgeImage->height, WINSZ);
        endR = lambda->height;
        endC = lambda->width;
        lambdaTemp = fReshape(lambda, endR*endC, 1);
    
        /** We sort the lambda matrix based on the strengths **/
        /** Fill features matrix with top N_FEA features **/
        fFreeHandle(lambdaTemp);
        lambdaTemp = fillFeatures(lambda, N_FEA, WINSZ);
        features = fTranspose(lambdaTemp);
        
        /** Suppress features that have approximately similar strength and belong to close neighborhood **/
        interestPnt = getANMS(features, SUPPRESION_RADIUS);
        
        /** Refill interestPnt in features matrix **/
        fFreeHandle(features);
        features = fSetArray(2, interestPnt->height, 0);
        for(i=0; i<2; i++) {
            for(j=0; j<interestPnt->height; j++) {
                subsref(features,i,j) = subsref(interestPnt,j,i); 
    		}
        } 
         
    
        fFreeHandle(verticalEdgeImage);
        fFreeHandle(horizontalEdgeImage);
        fFreeHandle(interestPnt);
        fFreeHandle(lambda);
        fFreeHandle(lambdaTemp);
        /** Until now, we processed base frame. The following for loop processes other frames **/
        for(count=1; count<=counter; count++)
        {
            /** Read image **/
            //sprintf(im1, "%s/%d.bmp", argv[1], count);
            //Ic = readImage(im1);
            I2D* Icc = images[count-1];
            rows = Icc->height;
            cols = Icc->width;
            
        
            /** Blur image to remove noise **/
            blurredImage = imageBlur(Icc);
            previousFrameBlurred_level1 = fDeepCopy(blurred_level1);
            previousFrameBlurred_level2 = fDeepCopy(blurred_level2);
            
            fFreeHandle(blurred_level1);
            fFreeHandle(blurred_level2);
        
            /** Image pyramid **/
            blurred_level1 = blurredImage;
            blurred_level2 = imageResize(blurredImage);
        
            /** Gradient image computation, for all scales **/
            verticalEdge_level1 = calcSobel_dX(blurred_level1);   
            horizontalEdge_level1 = calcSobel_dY(blurred_level1); 
            
            verticalEdge_level2 = calcSobel_dX(blurred_level2); 
            horizontalEdge_level2 = calcSobel_dY(blurred_level2);
            
            newpoints = fSetArray(2, features->width, 0);
                
            /** Based on features computed in the previous frame, find correspondence in the current frame. "status" returns the index of corresponding features **/
            status = calcPyrLKTrack(previousFrameBlurred_level1, previousFrameBlurred_level2, verticalEdge_level1, verticalEdge_level2, horizontalEdge_level1, horizontalEdge_level2, blurred_level1, blurred_level2, features, features->width, WINSZ, accuracy, LK_ITER, newpoints);
            fFreeHandle(verticalEdge_level1);
            fFreeHandle(verticalEdge_level2);
            fFreeHandle(horizontalEdge_level1);
            fFreeHandle(horizontalEdge_level2);
            fFreeHandle(previousFrameBlurred_level1);
            fFreeHandle(previousFrameBlurred_level2);
            //printf("height = %d, width = %d, numFind = %d\n", newpoints->height, newpoints->width);
            
            /** Populate newpoints with features that had correspondence with previous frame features **/
            np_temp = fDeepCopy(newpoints);
            if(status->width > 0 )
            {
                k = 0;
                numFind=0;
                for(i=0; i<status->width; i++)
                {
                    if( asubsref(status,i) == 1)
                        numFind++;
                }
                fFreeHandle(newpoints);
                newpoints = fSetArray(2, numFind, 0);
        
                for(i=0; i<status->width; i++)
                {
                    if( asubsref(status,i) == 1)
                    {
                        subsref(newpoints,0,k) = subsref(np_temp,0,i);
                        subsref(newpoints,1,k++) = subsref(np_temp,1,i);
                    }
                }    
            }    
            
            iFreeHandle(status);
            fFreeHandle(np_temp);
            fFreeHandle(features);
        
            /** Populate newpoints into features **/
            features = fDeepCopy(newpoints);
            fFreeHandle(newpoints);
        }
    }
    printf("end..\n");
#ifdef CHECK   
    /* Self checking */
    {
        int ret=0;
        float tol = 2.0;
#ifdef GENERATE_OUTPUT
        fWriteMatrix(features, argv[1]);
#endif
        ret = fSelfCheck(features, "expected_C.txt", tol); 
        if (ret == -1)
            printf("Error in Tracking Map\n");
    }
#endif
    
    fFreeHandle(blurred_level1);
    fFreeHandle(blurred_level2);
    fFreeHandle(features);

    for(count=1; count<=counter; count++)
    {
        free(images[count - 1] );
    }
    iFreeHandle(Ic);
    WRITE_TO_FILE
    return 0;

}