summaryrefslogtreecommitdiff
path: root/2005/i/robert/src/ovision/see/magnifier.cc
blob: 58113287fb183f85a99b40a523c04262db5bd4e7 (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
// magnifier.cc - Classe Magnifier
// robert - Programme du robot APBteam
// Copyright (C) 2005 Olivier Gaillard

/// @file magnifier.cc Filtre la liste d'objets trouv�es  

#include <iostream>

#include "magnifier.hh"
#include "skittle.hh"

/// Constructeur
Magnifier::Magnifier (Img *img, Segm *segm, Group::ZoneType aim)
: segm_ (segm), img_ (img), aim_ (aim)
{
    oconfig_ = OConfig::getInstance (); 
    itemList_ = new std::vector<Zone>[Group::nbZoneType];
}

/// Destructeur
Magnifier::~Magnifier (void)
{
    delete [] itemList_;
}


// XXX getdelta XXX
/// Retourne le delta utilis� pour la dissociation de 2 balles proches
/// @param type type du group � rechercher GOAL ou BALL
/// @param y coordonnees de la hauteur de la balle 
/*int 
  Group::GetDelta (int type, int y)
  {
  if (type == BALL)
  return (int)(25 + y*0.1);

  else return 3*(int)(25 + y*0.1);
  }
  */

/// Analyse une liste de zones
/// @param zoneList liste des zones extraites par la classe group
bool
Magnifier::analyse (std::vector<Zone> &zoneList) 
{
    // remise � z�ro de toutes les listes
    for (int i=0; i<Group::nbZoneType; ++i)
	itemList_[i].clear ();
    // parcours de toutes les zones extraites
    for(std::vector<Zone>::iterator iter = zoneList.begin (); iter != zoneList.end (); ++iter)
      {
	// V�rifie qu'il n'y a pas de doublon
	if (!checkIsUnique (*iter, itemList_))
	    continue;
	add (*iter, itemList_);
      }
    // parcours des zones jusqu'� ce qu'il n'y ai plus de changement � faire
    while (scan ());
    // analyse des objets trouv�s
    if (itemList_[aim_].size () != 0)
      {
	bool find = false;
	for (std::vector<Zone>::iterator iter = itemList_[aim_].begin ();
	     iter != itemList_[aim_].end (); ++iter)
	  {
	    Skittle *s = isSkittle (*iter);
	    if (s)
	      {
		showZone (*iter);
		s->show ();
		delete s;
		find = true;
	      }
	  }
	return find;
      }
    return false;
}

bool
Magnifier::scan ()
{
    bool hasChanged = false;
    std::vector<Zone> *newList = new std::vector<Zone>[Group::nbZoneType];
    int dist;
    bool find;
    for (int i=0; i <Group::nbZoneType; ++i)
      {
	for (std::vector<Zone>::iterator iter = itemList_[i].begin (); iter != itemList_[i].end (); ++iter)
	  {
	    if (!checkIsUnique (*iter, newList))
	      {
		hasChanged = true;
		continue;
	      }
	    // on regarde s'il une zone noire est situ� en dessous pour virer le cercle sup�rieur des quilles
	    find = false;
	    dist = iter->ymax - iter->ymin;
	    for (std::vector<Zone>::iterator jter = iter+1; jter != itemList_[i].end (); ++jter)
	      {
		if ((iter->ymax + dist > jter->ymin) && (abs(iter->xmin - jter->xmin) < 40) && (abs(iter->xmax - jter->xmax) < 40))
		  {
		    jter->circleUp = true;
		    find = true;
		    break;
		  }
	      }
	    if (!find) add (*iter, newList);
	  }
      }
    delete [] itemList_;
    itemList_ = newList;
    return hasChanged;
}

void
Magnifier::add (Zone &zone, std::vector<Zone> *list)
{
    switch (zone.id)
      {
      case Group::redSkittle:
	if (isRedSkittle (zone)) addItem (zone, list);
	break;
      case Group::greenSkittle:
	if (isGreenSkittle (zone)) addItem (zone, list);
	break;
      case Group::base:
	if (isBase (zone)) addItem (zone, list);
	break;
      case Group::gap:
	if (isGap (zone)) addItem (zone, list);
	break;
      case Group::border:
	if (isBorder (zone)) addItem (zone, list);
	break;
      case Group::reflectBand:
	addItem (zone, list);
	break;
      }
}

bool
isInside (const Zone &z1, const Zone &z2)
{
//    const int maxOut = 300;
//    int out = 0;
    // test si le centre est � l'int�rieur
    if ((z1.centerx <= z2.xmax) && (z1.centerx >= z2.xmin)
	&& (z1.centery <= z2.ymax) && (z1.centery >= z2.ymin))
	{
	  return true;
//	  if (z1.xmax > z2.xmax ) out += z1.xmax - z2.xmax;
//	  if (z1.ymax > z2.ymax ) out += z1.ymax - z2.ymax;
//	  if (z1.xmin < z2.xmin ) out += z2.xmin - z1.xmin;
//	  if (z1.ymin < z2.ymin ) out += z2.ymin - z1.ymin;
//	  if (out < maxOut) return true;
	}
    return false;
}

/// Test si l'object s'agit d'un doublon
bool
Magnifier::checkIsUnique (const Zone &zone, std::vector<Zone> *list)
{
    for (std::vector<Zone>::iterator iter = list[zone.id].begin ();
	 iter != list[zone.id].end (); ++iter)
	if (((abs (zone.centerx - iter->centerx) < oconfig_->magUniqueness)
	    || (abs (zone.centery - iter->centery) < oconfig_->magUniqueness)))
	  {
	    if (isInside (zone, *iter) || isInside (*iter, zone)) 
	//	|| (areBound (zone, *iter)))
	      {
		// on met a jour les donn�es de l'item d�j� trouv�
		if (zone.xmin < iter->xmin) iter->xmin = zone.xmin;
		if (zone.ymin < iter->ymin) iter->ymin = zone.ymin;
		if (zone.xmax > iter->xmax) iter->xmax = zone.xmax;
		if (zone.ymax > iter->ymax) iter->ymax = zone.ymax;
		iter->centerx = (iter->xmax + iter->xmin) / 2;
		iter->centery = (iter->ymax + iter->ymin) / 2;
		return false;
	      }
	  }
    return true;
}


/// Test si l'objet est une quille    
Skittle*
Magnifier::isSkittle (Zone &zone)
{
    Skittle *s = new Skittle (img_, segm_);
    if (s->analyse (zone, itemList_[Group::reflectBand]))
      {
	return s;
      }
    else
	delete s;
    return 0;
}

/// Test si l'objet est une quille rouge
bool 
Magnifier::isRedSkittle (const Zone &zone) const
{
    return true;
}

/// Test si l'objet est une bordure
bool 
Magnifier::isBorder (const Zone &zone) const
{
    // test si la zone touche au moins deux bords de l'image
    /// XXX pas tout � fait vrai pr�s du foss�
    unsigned count = 0;
    if (zone.xmin <= 0) ++count;
    if (zone.ymin <= 0) ++count;
    if (zone.xmax >= img_->width_) ++count;
    if (zone.ymax <= img_->height_) ++count;
    if (count < 2) return false;
    return true;
}


/// Test si l'objet est une quille verte
bool 
Magnifier::isGreenSkittle (const Zone &zone) const
{
    return true;
}

/// Test si l'objet est un socle
bool
Magnifier::isBase (const Zone &zone) const
{
    return true;
}

/// Test si l'objet est un foss�e
bool
Magnifier::isGap (const Zone &zone) const
{
    return true;
}

/// Ajout d'un objet
void
Magnifier::addItem (Zone &zone, std::vector<Zone> *list)
{
    // TODO remplir les flags : partial, ...
    zone.area = (zone.xmax - zone.xmin) * (zone.ymax - zone.ymin);    
    if ((zone.xmax == img_->width_) || (zone.xmin == 0)
	|| (zone.ymax == img_->height_) || (zone.ymin == 0))
	zone.partial = true;
    else zone.partial = false;
    list[zone.id].push_back (zone);
}


/// Affiche les zones trouvees apr�s analyse
void
Magnifier::showItems (const Group::ZoneType type) const
{
    std::cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
    std::cout << Group::translateToColorName (type) << "\n";
    for (std::vector<Zone>::const_iterator iter = itemList_[type].begin (); iter != itemList_[type].end (); ++iter)
	showZone (*iter);
    std::cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
}


/// Test si les zones sont li�es
bool 
Magnifier::areBound (const Zone &z1, const Zone &z2)
{
    /// Ratio demand� pour la liaison
    const double ratioBound = 0.95;
    /// Coeff directeur de la droite reliant les 2 zones
    int yCenterDiff = z1.centery - z2.centery;
    int xCenterDiff = z1.centerx - z2.centerx;
    double a, b;
    if (yCenterDiff != 0)
      {
	a = yCenterDiff / (double)xCenterDiff;
	b = z1.centery - a * z1.centerx;
      }
    else 
      {
	a = 0;
	b = z1.centery;
      }
    int min, max;
    if (xCenterDiff < 0)
      {
	min = z1.centerx;
	max = z2.centerx;
      }
    else
      {
	min = z2.centerx;
	max = z1.centerx;
      }
    /// Parcours des x entre les 2 zones
    int equal = 0;
    int y;
    for (int x = min; x < max; ++x)
      {
	y = img_->maxWithBorder (img_->minWithBorder ((int)(a*x+b), 0), img_->height_);;
	if (segm_->giveColor (img_->tabData_ + ((y*img_->width_+x) * 3), true, true) == z1.color)
	    ++equal;
      }
    /// Retour avec marge d'erreur    
    if (equal >= (max - min)*ratioBound-2)
	return true;
    return false;
}

void
Magnifier::showZone (const Zone &z) const
{
    std::cout << "------------------------------------------------------------------\n";
    std::cout << "<Magnifier::showZone> position : " << z.xmin << ", " << z.xmax << ", " << z.ymin << ", " << z.ymax << "\n";
    std::cout << "<Magnifier::showZone> alone : " << z.alone << "\n"; 
    std::cout << "<Magnifier::showZone> density: " << z.density << "\n"; 
    std::cout << "<Magnifier::showZone> circleUp: " << z.circleUp << "\n"; 
    std::cout << "<Magnifier::showZone> ratio/vertical: " << z.ratio << "  " << (z.vertical?"vertical":"") << "\n"; 
//    std::cout << "<Magnifier::showZone> orientation : " << z.pcX_ << ", " << z.pcY_ << "\n";
    std::cout << "<Magnifier::showZone> partial : " << z.partial << "\n"; 
}