summaryrefslogtreecommitdiffstats
path: root/jni/feature_stab/src/dbregtest/dbregtest.cpp
blob: 5087362189668da2c6a0b8a9596e933618dfe50d (plain)
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// $Id: dbregtest.cpp,v 1.24 2011/06/17 14:04:33 mbansal Exp $
#include "stdafx.h"
#include "PgmImage.h"
#include "../dbreg/dbreg.h"
#include "../dbreg/dbstabsmooth.h"
#include <db_utilities_camera.h>

#include <iostream>
#include <iomanip>

#if PROFILE
    #include <sys/time.h>
#endif


using namespace std;

const int DEFAULT_NR_CORNERS=500;
const double DEFAULT_MAX_DISPARITY=0.2;
const int DEFAULT_MOTION_MODEL=DB_HOMOGRAPHY_TYPE_AFFINE;
//const int DEFAULT_MOTION_MODEL=DB_HOMOGRAPHY_TYPE_R_T;
//const int DEFAULT_MOTION_MODEL=DB_HOMOGRAPHY_TYPE_TRANSLATION;
const bool DEFAULT_QUARTER_RESOLUTION=false;
const unsigned int DEFAULT_REFERENCE_UPDATE_PERIOD=3;
const bool DEFAULT_DO_MOTION_SMOOTHING = false;
const double DEFAULT_MOTION_SMOOTHING_GAIN = 0.75;
const bool DEFAULT_LINEAR_POLISH = false;
const int DEFAULT_MAX_ITERATIONS = 10;

void usage(string name) {

  const char *helpmsg[] = {
    "Function: point-based frame to reference registration.",
    "  -m [rt,a,p]  : motion model, rt = rotation+translation, a = affine (default = affine).",
    "  -c <int>   : number of corners (default 1000).",
    "  -d <double>: search disparity as portion of image size (default 0.1).",
    "  -q         : quarter the image resolution (i.e. half of each dimension) (default on)",
    "  -r <int>   : the period (in nr of frames) for reference frame updates (default = 5)",
    "  -s <0/1>   : motion smoothing (1 activates motion smoothing, 0 turns it off - default value = 1)",
    "  -g <double>: motion smoothing gain, only used if smoothing is on (default value =0.75)",
    NULL
  };

  cerr << "Usage: " << name << " [options] image_list.txt" << endl;

  const char **p = helpmsg;

  while (*p)
  {
    cerr << *p++ << endl;
  }
}

void parse_cmd_line(stringstream& cmdline,
            const int argc,
            const string& progname,
            string& image_list_file_name,
            int& nr_corners,
            double& max_disparity,
            int& motion_model_type,
            bool& quarter_resolution,
            unsigned int& reference_update_period,
            bool& do_motion_smoothing,
            double& motion_smoothing_gain
            );

int main(int argc, char* argv[])
{
  int    nr_corners = DEFAULT_NR_CORNERS;
  double max_disparity = DEFAULT_MAX_DISPARITY;
  int    motion_model_type = DEFAULT_MOTION_MODEL;
  bool   quarter_resolution = DEFAULT_QUARTER_RESOLUTION;

  unsigned int reference_update_period = DEFAULT_REFERENCE_UPDATE_PERIOD;

  bool   do_motion_smoothing = DEFAULT_DO_MOTION_SMOOTHING;
  double motion_smoothing_gain = DEFAULT_MOTION_SMOOTHING_GAIN;
  const bool DEFAULT_USE_SMALLER_MATCHING_WINDOW = true;

  int default_nr_samples = DB_DEFAULT_NR_SAMPLES/5;

  bool   use_smaller_matching_window = DEFAULT_USE_SMALLER_MATCHING_WINDOW;


  bool   linear_polish = DEFAULT_LINEAR_POLISH;

  if (argc < 2) {
    usage(argv[0]);
    exit(1);
  }

  stringstream cmdline;
  string progname(argv[0]);
  string image_list_file_name;

#if PROFILE
  timeval ts1, ts2, ts3, ts4;
#endif

  // put the options and image list file name into the cmdline stringstream
  for (int c = 1; c < argc; c++)
  {
    cmdline << argv[c] << " ";
  }

  parse_cmd_line(cmdline, argc, progname, image_list_file_name, nr_corners, max_disparity, motion_model_type,quarter_resolution,reference_update_period,do_motion_smoothing,motion_smoothing_gain);

  ifstream in(image_list_file_name.c_str(),ios::in);

  if ( !in.is_open() )
  {
    cerr << "Could not open file " << image_list_file_name << ".  Exiting" << endl;

    return false;
  }

  // feature-based image registration class:
  db_FrameToReferenceRegistration reg;
//  db_StabilizationSmoother stab_smoother;

  // input file name:
  string file_name;

  // look-up tables for image warping:
  float ** lut_x = NULL, **lut_y = NULL;

  // if the images are color, the input is saved in color_ref:
  PgmImage color_ref(0,0);

  // image width, height:
  int w,h;

  int frame_number = 0;

  while ( !in.eof() )
  {
    getline(in,file_name);

    PgmImage ref(file_name);

    if ( ref.GetDataPointer() == NULL )
    {
      cerr << "Could not open image" << file_name << ". Exiting." << endl;
      return -1;
    }

    cout << ref << endl;

    // color format:
    int format = ref.GetFormat();

    // is the input image color?:
    bool color = format == PgmImage::PGM_BINARY_PIXMAP;

    w = ref.GetWidth();
    h = ref.GetHeight();

    if ( !reg.Initialized() )
    {
      reg.Init(w,h,motion_model_type,DEFAULT_MAX_ITERATIONS,linear_polish,quarter_resolution,DB_POINT_STANDARDDEV,reference_update_period,do_motion_smoothing,motion_smoothing_gain,default_nr_samples,DB_DEFAULT_CHUNK_SIZE,nr_corners,max_disparity,use_smaller_matching_window);
      lut_x = db_AllocImage_f(w,h);
      lut_y = db_AllocImage_f(w,h);

    }

    if ( color )
    {
      // save the color image:
      color_ref = ref;
    }

    // make a grayscale image:
    ref.ConvertToGray();

    // compute the homography:
    double H[9],Hinv[9];
    db_Identity3x3(Hinv);
    db_Identity3x3(H);

    bool force_reference = false;

#if PROFILE
    gettimeofday(&ts1, NULL);
#endif

    reg.AddFrame(ref.GetRowPointers(),H,false,false);
    cout << reg.profile_string << std::endl;

#if PROFILE
    gettimeofday(&ts2, NULL);

    double elapsedTime = (ts2.tv_sec - ts1.tv_sec)*1000.0; // sec to ms
    elapsedTime += (ts2.tv_usec - ts1.tv_usec)/1000.0; // us to ms
    cout <<"\nelapsedTime for Reg<< "<<elapsedTime<<" ms >>>>>>>>>>>>>\n";
#endif

    if (frame_number == 0)
    {
      reg.UpdateReference(ref.GetRowPointers());
    }


    //std::vector<int> &inlier_indices = reg.GetInliers();
    int *inlier_indices = reg.GetInliers();
    int num_inlier_indices = reg.GetNrInliers();
    printf("[%d] #Inliers = %d\n",frame_number,num_inlier_indices);

    reg.Get_H_dref_to_ins(H);

    db_GenerateHomographyLut(lut_x,lut_y,w,h,H);

    // create a new image and warp:
    PgmImage warped(w,h,format);

#if PROFILE
    gettimeofday(&ts3, NULL);
#endif

    if ( color )
      db_WarpImageLutBilinear_rgb(color_ref.GetRowPointers(),warped.GetRowPointers(),w,h,lut_x,lut_y);
    else
      db_WarpImageLut_u(ref.GetRowPointers(),warped.GetRowPointers(),w,h,lut_x,lut_y,DB_WARP_FAST);

#if PROFILE
    gettimeofday(&ts4, NULL);
    elapsedTime = (ts4.tv_sec - ts3.tv_sec)*1000.0; // sec to ms
    elapsedTime += (ts4.tv_usec - ts3.tv_usec)/1000.0;     // us to ms
    cout <<"\nelapsedTime for Warp <<"<<elapsedTime<<" ms >>>>>>>>>>>>>\n";
#endif

    // write aligned image: name is aligned_<corresponding input file name>
    stringstream s;
    s << "aligned_" << file_name;
    warped.WritePGM(s.str());

    /*
    // Get the reference and inspection corners to write to file
    double *ref_corners = reg.GetRefCorners();
    double *ins_corners = reg.GetInsCorners();

    // get the image file name (without extension), so we
    // can generate the corresponding filenames for matches
    // and inliers
    string file_name_root(file_name.substr(0,file_name.rfind(".")));

    // write matches to file
    s.str(string(""));
    s << "Matches_" << file_name_root << ".txt";

    ofstream  match_file(s.str().c_str());

    for (int i = 0; i < reg.GetNrMatches(); i++)
    {
      match_file << ref_corners[3*i] << " " << ref_corners[3*i+1] << " " << ins_corners[3*i] << " " << ins_corners[3*i+1] << endl;
    }

    match_file.close();

    // write the inlier matches to file
    s.str(string(""));
    s << "InlierMatches_" << file_name_root << ".txt";

    ofstream inlier_match_file(s.str().c_str());

    for(int i=0; i<num_inlier_indices; i++)
    {
      int k = inlier_indices[i];
      inlier_match_file << ref_corners[3*k] << " "
            << ref_corners[3*k+1] << " "
            << ins_corners[3*k] << " "
            << ins_corners[3*k+1] << endl;
    }
    inlier_match_file.close();
    */

    frame_number++;
  }

  if ( reg.Initialized() )
  {
    db_FreeImage_f(lut_x,h);
    db_FreeImage_f(lut_y,h);
  }

  return 0;
}

void parse_cmd_line(stringstream& cmdline,
            const int argc,
            const string& progname,
            string& image_list_file_name,
            int& nr_corners,
            double& max_disparity,
            int& motion_model_type,
            bool& quarter_resolution,
            unsigned int& reference_update_period,
            bool& do_motion_smoothing,
            double& motion_smoothing_gain)
{
  // for counting down the parsed arguments.
  int c = argc;

  // a holder
  string token;

  while (cmdline >> token)
  {
    --c;

    int pos = token.find("-");

    if (pos == 0)
    {
      switch (token[1])
      {
      case 'm':
    --c; cmdline >> token;
    if (token.compare("rt") == 0)
    {
      motion_model_type = DB_HOMOGRAPHY_TYPE_R_T;
    }
    else if (token.compare("a") == 0)
    {
      motion_model_type = DB_HOMOGRAPHY_TYPE_AFFINE;
    }
    else if (token.compare("p") == 0)
    {
      motion_model_type = DB_HOMOGRAPHY_TYPE_PROJECTIVE;
    }
    else
    {
      usage(progname);
      exit(1);
    }
    break;
      case 'c':
    --c; cmdline >> nr_corners;
    break;
      case 'd':
    --c; cmdline >> max_disparity;
    break;
      case 'q':
    quarter_resolution = true;
    break;
      case 'r':
    --c; cmdline >> reference_update_period;
    break;
      case 's':
    --c; cmdline >> do_motion_smoothing;
    break;
      case 'g':
    --c; cmdline >> motion_smoothing_gain;
    break;
      default:
    cerr << progname << "illegal option " << token << endl;
      case 'h':
    usage(progname);
    exit(1);
    break;
      }
    }
    else
    {
      if (c != 1)
      {
    usage(progname);
    exit(1);
      }
      else
      {
    --c;
    image_list_file_name = token;
      }
    }
  }

  if (c != 0)
  {
    usage(progname);
    exit(1);
  }
}