/normxcorr/trunk

To get this branch, use:
bzr branch http://suren.me/webbzr/normxcorr/trunk

« back to all changes in this revision

Viewing changes to dict_hw/matlab/normxcorr_hw.c

  • Committer: Suren A. Chilingaryan
  • Date: 2010-04-27 09:35:57 UTC
  • Revision ID: csa@dside.dyndns.org-20100427093557-gxvt9b1n68tpig03
Enchanced error checking and timings measurements, Windows-related fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
 
46
46
static DICTContext pstate = NULL;
47
47
static mxArray *coords = NULL;                  // Matlab array with current coordinates
 
48
static int dict_status = 0;
 
49
 
 
50
enum DICTStatus {
 
51
    DICT_STATUS_SET = 1,
 
52
    DICT_STATUS_BASE_IMAGE_LOADED = 2,
 
53
    DICT_STATUS_BASE_POINTS_LOADED = 4,
 
54
    DICT_STATUS_CURRENT_POINTS_LOADED = 8,
 
55
    DICT_STATUS_READY = 15
 
56
};
48
57
 
49
58
#ifndef EXTERN_C
50
59
# ifdef __cplusplus
71
80
        mxDestroyArray(coords);
72
81
        coords = NULL;
73
82
    }
 
83
    
 
84
    dict_status = 0;
74
85
}
75
86
 
76
87
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
141
152
        }
142
153
        
143
154
        if (pstate) {
 
155
            dict_status = 0;
144
156
            mexAtExit(selfClean);
145
157
        } else if (id > 0) {
146
158
            reportError("Context initialization has failed");
178
190
            return;
179
191
        }
180
192
 
 
193
        if ((dict_status&DICT_STATUS_READY)==0) {
 
194
            reportError("normxcorr is not properly initialized, you should load template image and set template and current points first");
 
195
            return;
 
196
        }
 
197
 
181
198
        input = prhs[2];
182
199
 
183
200
        class_id = mxGetClassID(input);
193
210
                    return;
194
211
                }
195
212
 
196
 
                dictLoadImage(ps, (unsigned char*)mxGetData(input));
 
213
                err = dictLoadImage(ps, (unsigned char*)mxGetData(input));
 
214
                if (err) {
 
215
                    reportError("Failed to load specified image");
 
216
                    return;
 
217
                }
197
218
            break;
198
219
            case mxCHAR_CLASS:
199
220
                if ((mxGetNumberOfDimensions(input) != 2)&&(mxGetM(input) != 1)) {
235
256
            return;
236
257
        }
237
258
 
 
259
        if ((dict_status&DICT_STATUS_SET)==0) {
 
260
            reportError("normxcorr is not properly initialized, you should execute setup action first");
 
261
            return;
 
262
        }
 
263
 
238
264
        base = prhs[2];
239
265
        
240
266
        class_id = mxGetClassID(base);
249
275
                width = mxGetN(base);
250
276
                height = mxGetM(base);
251
277
        
252
 
                dictLoadTemplateImage(ps, (unsigned char*)mxGetData(base), width, height);
 
278
                err = dictLoadTemplateImage(ps, (unsigned char*)mxGetData(base), width, height);
 
279
                if (err) {
 
280
                    reportError("Failed to load specified template image");
 
281
                    return;
 
282
                }
253
283
            break;
254
284
            case mxCHAR_CLASS:
255
285
                if ((mxGetNumberOfDimensions(base) != 2)&&(mxGetM(base) != 1)) {
276
306
 
277
307
                err = dictLoadTemplateImageFile(ps, name);
278
308
                if (err) {
279
 
                    reportError("Failed to load image: %s", name);
 
309
                    reportError("Failed to load template image: %s", name);
280
310
                    return;
281
311
                }
 
312
                dict_status |= DICT_STATUS_BASE_IMAGE_LOADED;
282
313
            break;
283
314
            default:
284
315
                reportError("Invalid matrix. The data type (%s) is not supported", mxGetClassName(base));
291
322
            return;
292
323
        }
293
324
 
 
325
        if ((dict_status&DICT_STATUS_SET)==0) {
 
326
            reportError("normxcorr is not properly initialized, you should execute setup action first");
 
327
            return;
 
328
        }
 
329
 
294
330
        x = prhs[2];
295
331
        y = prhs[3];
296
332
        
303
339
            return;
304
340
        }
305
341
        
306
 
        dictSetTemplatePoints(ps,  (float*)mxGetData(x),  (float*)mxGetData(y));
 
342
        err = dictSetTemplatePoints(ps,  (float*)mxGetData(x),  (float*)mxGetData(y));
 
343
        if (err) {
 
344
            reportError("Problem loading template control points, error: %i", err);
 
345
            return;
 
346
        }
 
347
        dict_status |= DICT_STATUS_BASE_POINTS_LOADED;
307
348
     break;
308
349
     case ACTION_SET_POINTS:
309
350
        if (nrhs != 4) {
311
352
            return;
312
353
        }
313
354
 
 
355
        if ((dict_status&DICT_STATUS_SET)==0) {
 
356
            reportError("normxcorr is not properly initialized, you should execute setup action first");
 
357
            return;
 
358
        }
 
359
 
314
360
        x = prhs[2];
315
361
        y = prhs[3];
316
362
        
323
369
            return;
324
370
        }
325
371
        
326
 
        dictSetCurrentPoints(ps,  (float*)mxGetData(x),  (float*)mxGetData(y));
 
372
        err = dictSetCurrentPoints(ps,  (float*)mxGetData(x),  (float*)mxGetData(y));
 
373
        if (err) {
 
374
            reportError("Problem loading current control points, error: %i", err);
 
375
            return;
 
376
        }
 
377
 
 
378
        dict_status |= DICT_STATUS_CURRENT_POINTS_LOADED;
327
379
     break;
328
380
     case ACTION_GET_POINTS:
329
381
        if (nrhs != 2) {
330
382
            reportError("GetPoints action do not expect any arguments");
331
383
            return;
332
384
        }
 
385
 
333
386
        if (nlhs != 1) {
334
387
            reportError("GetPoints action returns a single matrix");
335
388
            return;
339
392
            reportError("normxcorr is not properly initialized, the result matrix is not allocated");
340
393
            return;
341
394
        }
 
395
 
 
396
        if ((dict_status&DICT_STATUS_READY)==0) {
 
397
            reportError("normxcorr is not properly initialized, you should load template image and set template and current points first");
 
398
            return;
 
399
        }
 
400
 
342
401
        
343
402
        dictCompute(ps);
344
403
        
357
416
            return;
358
417
        }
359
418
 
 
419
        dict_status = 0;
 
420
 
360
421
        ncp = (int)mxGetScalar(prhs[2]);
361
422
        corr_size = (int)mxGetScalar(prhs[3]);
362
423
        iprop = (int)mxGetScalar(prhs[5]);
387
448
                errPtr = (int64_t*)mxGetData(idMatrix);
388
449
                errPtr[0] = err;
389
450
                plhs[0] = idMatrix;
 
451
                dict_status = DICT_STATUS_SET;
390
452
            } else {
391
453
                reportError("Initialization of result matrix is failed");
392
454
                return;