File: ContourMatching.cc

package info (click to toggle)
exactimage 1.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,040 kB
  • sloc: cpp: 35,940; ansic: 1,952; xml: 1,447; makefile: 338; perl: 138; sh: 110; python: 45; php: 37; ruby: 12
file content (476 lines) | stat: -rw-r--r-- 12,904 bytes parent folder | download | duplicates (10)
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#include <cmath>
#include <algorithm>
#include <iostream>

#include "ContourMatching.hh"

const unsigned int logo_trans_before_rot=10000; // TODO: calculate useful value !!

class LengthSorter
{
public:
  const std::vector <Contours::Contour*>& contours;

  LengthSorter(const std::vector <Contours::Contour*>& contour_list)
    : contours(contour_list) {}

  bool operator() (unsigned int i, unsigned int j)
  {
    return contours[i]->size() > contours[j]->size(); 
  }
};

class MatchSorter
{
public:
  bool operator() (const LogoRepresentation::Match* a, const LogoRepresentation::Match* b)
  {
    return a->score > b->score;
  }
};

LogoRepresentation::LogoRepresentation(Contours* logo_contours,
				       unsigned int max_feature_no,
				       unsigned int max_avg_tolerance,
				       unsigned int reduction_shift,
				       double maximum_angle,
				       double angle_step)
{
  source=logo_contours;
  tolerance=max_avg_tolerance;
  shift=reduction_shift;
  rot_max=maximum_angle;
  rot_step=angle_step;
  total_contour_length=0;

  logo_set_count=source->contours.size();
  logo_set_map.resize(logo_set_count);
  for (unsigned int i=0; i<logo_set_count; i++)
    logo_set_map[i]=i;
  
  if (logo_set_count > max_feature_no) {
    std::sort(logo_set_map.begin(), logo_set_map.end(), LengthSorter(source->contours));
    logo_set_count=max_feature_no;
  }

  centerx=.0;
  centery=.0;
  unsigned int count=0;
  for (unsigned int i=0; i<logo_set_count; i++) {
    count+=source->contours[logo_set_map[i]]->size();
    for (unsigned int j=0; j<source->contours[logo_set_map[i]]->size(); j++) {
      centerx += (*(source->contours[logo_set_map[i]]))[j].first;
      centery += (*(source->contours[logo_set_map[i]]))[j].second;
    }
  }
  centerx/=(double)count;
  centery/=(double)count;
    

  double angle=.0;
  rot_max=std::min(359.9, fabs(rot_max));
  rot_step=std::max(rot_step, 0.5);

  do {
    logo_sets.push_back(std::vector <LogoContourData> (logo_set_count));
    for (unsigned int c=0; c<logo_set_count; c++) {
      LogoContourData& data=logo_sets.back()[c];
      data.contour=new Contours::Contour();
      if (angle==0) {
	CenterAndReduce(*(source->contours[logo_set_map[c]]),
			*data.contour,
			shift,
			data.rx,
			data.ry);
	total_contour_length+=source->contours[logo_set_map[c]]->size();
      } else
	RotCenterAndReduce(*(source->contours[logo_set_map[c]]),
			   *data.contour,
			   M_PI*angle / 180.0,
			   logo_trans_before_rot,
			   shift,
			   data.rx,
			   data.ry);
    }
    
    if (angle > 0) {
      angle=-angle;
    } else {
      angle=-angle+rot_step;
    }

  } while (angle <= rot_max);
}

LogoRepresentation::~LogoRepresentation()
{
  for (unsigned int s=0; s<logo_sets.size(); s++)
    for (unsigned int j=0; j<logo_set_count; j++)
      delete logo_sets[s][j].contour;
}

double LogoRepresentation::Score(Contours* image)
{
  unsigned int image_set_count=image -> contours.size();

  if (image_set_count==0 || logo_set_count==0) {
    std::cerr << "Warning: nothing to match..." << std::endl;
    return 0.0;
  }

  // build image set
  image_set.resize(image_set_count);
  for (unsigned int c=0; c<image_set_count; c++) {
    ImageContourData& data=image_set[c];
    data.contour=new Contours::Contour();
    CenterAndReduce(*(image->contours[c]),
		      *data.contour,
		      shift,
		      data.rx,
		      data.ry);
  }

  // calculate 1 to 1 matching scores

  for (unsigned int s=0; s<logo_sets.size(); s++)
    for (unsigned int j=0; j<logo_set_count; j++) {
      logo_sets[s][j].matches.resize(image_set_count);
      for (unsigned int i=0; i<image_set_count; i++)
	logo_sets[s][j].matches[i]=new Match(image_set[i], logo_sets[s][j], tolerance, shift,
					     source->contours[logo_set_map[j]]->size(), image->contours[i]);
    }

  // calculate heuristic n to m matching

  double score=.0;
  unsigned int best_set=0;
  unsigned int best_pivot=0;
  for (unsigned int s=0; s<logo_sets.size(); s++) {
    unsigned int pivot=0;
    double current=N_M_Match(s, pivot);
    if (current > score) {
      score=current;
      best_set=s;
      best_pivot=pivot;
    }
  }

  // starting parameters optained from heuristic
  score=(score/ (double) total_contour_length) / (double) tolerance;
  std::cout << "heuristic score: " <<  score << std::endl;
  const LogoContourData& result=logo_sets[best_set][best_pivot];
  logo_translation.first=(int)result.matches[result.n_to_n_match_index]->transx;
  logo_translation.second=(int)result.matches[result.n_to_n_match_index]->transy;
  if (best_set==0)
    rot_angle=.0;
  else {
    logo_translation.first+=logo_trans_before_rot;
    logo_translation.second+=logo_trans_before_rot;
    rot_angle=rot_step*(double)((best_set+1)/2);
    if (best_set%2==0)
      rot_angle=-rot_angle;
  }

  mapping.resize(logo_set_count);
  for (unsigned int i=0; i<logo_set_count; i++) {
    mapping[i]=std::pair <Contours::Contour*, Contours::Contour*>
      (source->contours[logo_set_map[i]],
       logo_sets[best_set][i].matches[logo_sets[best_set][i].n_to_n_match_index]->cimg);
  }

  // adjust translation
  // (this is a quick workaround to the shifted-centroid phenomenon)

  Contours::Contour tmp;
  double rax=.0;
  double ray=.0;
  double rbx=.0;
  double rby=.0;
  RotCenterAndReduce(*(mapping[best_pivot].first), tmp, M_PI*rot_angle/180.0,
		     logo_trans_before_rot, 0, rax, ray);
  rax-=(double)logo_trans_before_rot;
  ray-=(double)logo_trans_before_rot;
  tmp.clear();
  const Contours::Contour& second=*(mapping[best_pivot].second);
  for (unsigned int i=0; i<second.size(); i++) {
    rbx+=second[i].first;
    rby+=second[i].second;
  }
  rbx/=(double)second.size();
  rby/=(double)second.size();
  int newtx=(int)(rbx-rax);
  int newty=(int)(rby-ray);
  std::cout << "adjusting translation: "
	    << logo_translation.first << "\t"
	    << logo_translation.second << "\t->\t"
	    << newtx << "\t" << newty << std::endl;
  logo_translation.first=newtx;
  logo_translation.second=newty;


  // optimize

  score=PrecisionScore();

  //#if false
  /*
  double oldrx=.0;
  double oldry=.0;
  double newrx=.0;
  double newry=.0;
  RotatedCentroidPosition(oldrx, oldry);
  rot_angle-=10;
  RotatedCentroidPosition(newrx, newry);
  logo_translation.first+=(int)(oldrx-newrx);
  logo_translation.second+=(int)(oldry-newry);
  */

  bool improved=true;
  unsigned int precision_iterations=4;
  //if (false)
  for (unsigned int run=0; run <= precision_iterations && improved ; run++) {
     std::cout << score
	       << "\t" << logo_translation.first
	       << "\t" << logo_translation.second
	       << "\t" << rot_angle << std::endl;
     improved=false;

     if (run < precision_iterations) {
       improved=Optimize(score);
     }
  }
  //#endif 

  // clean up
  for (unsigned int s=0; s<logo_sets.size(); s++)
    for (unsigned int j=0; j<logo_set_count; j++) {
      for (unsigned int i=0; i<image_set_count; i++)
	delete logo_sets[s][j].matches[i];
      logo_sets[s][j].matches.clear();
    }
 
  for (unsigned int j=0; j<image_set_count; j++)
    delete image_set[j].contour;
  image_set.clear();

  return score;
}

const std::pair<int, int>& LogoRepresentation::CalculateInverseTranslation(int rx, int ry)
{
  double x=(double)logo_translation.first - rx;
  double y=(double)logo_translation.second - ry;
  double c=cos(-M_PI*rot_angle/180.0);
  double s=sin(-M_PI*rot_angle/180.0);
  double xx=c*x - s*y;
  double yy=s*x + c*y;
  inverse_translation.first=rx+(int)xx;
  inverse_translation.second=ry+(int)yy;
  return inverse_translation;
}


double LogoRepresentation::N_M_Match(unsigned int set, unsigned int& pivot)
{
  std::vector <LogoContourData>& data=logo_sets[set];
  for (unsigned int i=0; i<logo_set_count; i++) {
    std::sort(data[i].matches.begin(), data[i].matches.end(), MatchSorter());
    //std::cout << "BEST\t" << data[i].matches[0]->score << std::endl;
  }

  unsigned int image_set_count=data[0].matches.size();

  const unsigned int depth=5;
  const unsigned int counterdepth=1000;
  int mdepth=std::min(image_set_count, depth);
  int ndepth=std::min(image_set_count, counterdepth);

  double bestsum=.0;
  pivot=0;
  unsigned int tmpbest[logo_set_count];

  for (unsigned int base=0; base < logo_set_count; base++)
    for (int m=0; m < mdepth; m++) {
      
      double sum=data[base].matches[m]->score;
      double tx=data[base].matches[m]->transx;
      double ty=data[base].matches[m]->transy;
      tmpbest[base]=m;

      for (unsigned int counter=0; counter < logo_set_count; counter++)
	if (counter != base) {
	  double best=.0;
	  tmpbest[counter]=0;
	  for (int n=0; n < ndepth; n++){
	    double current=data[counter].matches[n]->TransScore(tx, ty);
	    if (current > best) {
	      best=current;
	      tmpbest[counter]=n;
	    }
	  }
	  sum+=best;
	}

      if (sum > bestsum) {
	bestsum=sum;
	pivot=base;
	for (unsigned int i=0; i<logo_set_count; i++)
	  data[i].n_to_n_match_index=tmpbest[i];
      }
    }
  
  //std::cout << pivot << std::endl;

  return bestsum;
}


double LogoRepresentation::PrecisionScore()
{
  Contours::Contour tmp;
  double trash;
  //double sum=(double)tolerance*(double)total_contour_length;
  double sum=.0;
  unsigned int length=0;

  double tx=(double)logo_translation.first-(double)logo_trans_before_rot;
  double ty=(double)logo_translation.second-(double)logo_trans_before_rot;

  for (unsigned int i=0; i<logo_set_count; i++) {
    tmp.clear();
    RotCenterAndReduce(*mapping[i].first, tmp, M_PI*rot_angle/180.0,
		       logo_trans_before_rot, 0, trash, trash);

    double current=(double)tolerance*(double)tmp.size();
    length+=tmp.size();
    current-=L1Dist(tmp, *mapping[i].second,
		0.0, 0.0, tx, ty, 0, trash, trash);

    //std::cout << "match " << i << "\t" << current << std::endl;
    sum+=std::max(0.0, current);
  }

  sum=(sum/ (double)length) / (double) tolerance;
  return sum;
};


void LogoRepresentation::RotatedCentroidPosition(double& rx, double& ry)
{
  double c=cos(M_PI*rot_angle/180.0);
  double s=sin(M_PI*rot_angle/180.0);
  rx=c*centerx - s*centery;
  ry=s*centerx + c*centery;
  //  std::cout << centerx << "\t" << centery << "\t\t" << rx << "\t" << ry <<std::endl;
}


bool LogoRepresentation::OptimizeAngle(double& score, double delta)
{
       double oldrx=.0;
       double oldry=.0;
       double newrx=.0;
       double newry=.0;
       std::pair<int, int> o_translation=logo_translation;
       double o_angle=rot_angle;
       RotatedCentroidPosition(oldrx, oldry);
       rot_angle+=delta;
       RotatedCentroidPosition(newrx, newry);
       logo_translation.first+=(int)(oldrx-newrx);
       logo_translation.second+=(int)(oldry-newry);

       double new_score=PrecisionScore();
       if (new_score > score) {
	 score=new_score;
	 return true;
       }

       logo_translation=o_translation;
       rot_angle=o_angle;
       return false;
}

bool LogoRepresentation::OptimizeHTranslation(double& score, int delta)
{
  logo_translation.first+=delta;
  double new_score=PrecisionScore();
  if (new_score > score) {
    score=new_score;
    return true;
  }
  
  logo_translation.first-=delta;
  return false;
}

bool LogoRepresentation::OptimizeVTranslation(double& score, int delta)
{
  logo_translation.second+=delta;
  double new_score=PrecisionScore();
  if (new_score > score) {
    score=new_score;
    return true;
  }
  
  logo_translation.second-=delta;
  return false;
}

bool LogoRepresentation::Optimize(double& score)
{
  bool improvement=false;

  const double start_angle_delta=2.0;
  const double end_angle_delta=0.01; // was 0.1 -ReneR 2007-08-06
  double delta=start_angle_delta;
  do {
    bool success=false;
    while (OptimizeAngle(score, delta))
      success=true;
    if (!success)
    while (OptimizeAngle(score, -delta))
      success=true;

    improvement |= success;
    delta /= 2.0;
  } while (delta >= end_angle_delta );

  bool successh=false;
  while (OptimizeHTranslation(score, +1))
    successh=true;
  if (!successh)
  while (OptimizeHTranslation(score, -1))
    successh=true;
  improvement |= successh;

  bool successv=false;
  while (OptimizeVTranslation(score, +1))
    successv=true;
  if (!successv)
  while (OptimizeVTranslation(score, -1))
    successv=true;
  improvement |= successv;

  return improvement;
};


LogoRepresentation::Match::Match(const ImageContourData& image,
	  const LogoContourData& logo,
	  int tolerance,
	  int shift,
	  unsigned int original_logo_length,
	  Contours::Contour* icimg)
{
  length=original_logo_length;
  cimg=icimg;
  score=(double)tolerance*(double)length;
  score-=L1Dist(*logo.contour, *image.contour, logo.rx, logo.ry, image.rx, image.ry, shift, transx, transy);
  if (score < 0.0)
    score=.0;
}

double LogoRepresentation::Match::TransScore(double tx, double ty)
{
  return std::max(.0, score - 0.5*((double)length*(fabs(tx-transx)+fabs(ty-transy))));
}