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

/// @file skittle.cc  Reconnaissance des objets quilles

#include <iostream>

#include "skittle.hh"
#include "hotelling.hh"
    
/// Constructeur
Skittle::Skittle (Img *img, Segm *segm)
    : segm_ (segm), img_ (img), pcX_ (0), pcY_ (0)
{
    oconfig_ = OConfig::getInstance (); 
}

/// Destructeur
Skittle::~Skittle (void)
{
}

/// Recherche de la composante principale 
void
Skittle::pca (Zone &zone)
{
    // Agrandissement de la zone de recherche
    /*const int grow = 0;//oconfig_->skittleGrow;
    ymin = img_->minWithBorder (zone.ymin - grow, 0);
    ymax = img_->maxWithBorder (zone.ymax + grow, img_->height_);
    xmin = img_->minWithBorder (zone.xmin - grow, 0);
    xmax = img_->maxWithBorder (zone.xmax + grow, img_->width_);*/
    int ymin, ymax, xmin, xmax;
    ymin = zone.ymin; ymax = zone.ymax;
    xmin = zone.xmin; xmax = zone.xmax;
    /// Initialisation du saut de point
    //int jump = oconfig_->jumpPointDist/oconfig_->skittleDivJump;
    //if (!jump) jump = 1;
    int jump = 5;
    // Parcours d'une partie des pixels de l'image
    // Sauvegarde des points et de la densit�
    int nbPoints = 0;
    std::vector<Hpoint> l;
    for (int y = ymin; y < ymax; y += jump) 
      {
	for (int x = xmin; x < xmax; x += jump) 
	    if (segm_->giveColor (img_->tabData_ + ((img_->tabY_[y] + x) * 3), true, true) == zone.color)
	      {
		//		*f2 << x << " " << y << std::endl;
		Hpoint h (x, y);
		l.push_back (h);
		if ((x >= zone.xmin) && (x <= zone.xmax) && (y >= zone.ymin) && (y <= zone.ymax))
		    ++nbPoints;
	      }
      }
    // Calcul de la densit�
    zone.area = (zone.xmax - zone.xmin) * (zone.ymax - zone.ymin);
    zone.density = nbPoints * jump * jump / (double)zone.area;
    // Calcul de la composante principale
    Hotelling hote (l);
    hote.eigenVectors ();
    hote.get (pcX_, pcY_, pc2X_, pc2Y_);
}

/// Utilis� pour la recherche de la courbure, cherche le point le plus haut
int
Skittle::climb (const int startX, int startY, const int color)
{
    while ((segm_->giveColor (img_->tabData_ + ((startY*img_->width_ + startX) * 3), true, true) == color)
	   && (startY > 0))
      {
	--startY;
//	*f << startX << " " << startY << "\n";
      }
    return startY;
}

/// V�rifie qu'un catadiopre est � proximit�
Skittle::Pos
Skittle::whereIsReflectBand (const Zone &zone, const std::vector<Zone> &listReflectBand)
{
    const double minAreaRatio = 0.1;
    const double maxAreaRatio = 0.4;
    // Test les variables de composantes principales + liste transmise
    if (!pcX_ || !pcY_ || listReflectBand.size ())
	return error;
    // Analyse de la taille
    if (zone.area < minAreaRatio || zone.area > maxAreaRatio) // valeur r�elle 0.2
	// Parcours la liste des catadiopres
	for (std::vector<Zone>::const_iterator iter = listReflectBand.begin ();
	     iter != listReflectBand.end (); ++iter)
	  {
	    // Test la distance cart�sienne
	    if (dist (iter->centerx, iter->centery, zone.centerx, zone.centery))
		// Test la distance orthogonale
		if (orthoDist (iter->centerx - zone.centerx, iter->centery - zone.centery,
			       pcX_, pcY_))
		  {
		    if (iter->centerx > zone.centerx)
			return up;
		    else return down;
		  }

	  }
    return farAway;
}

/// Recherche de du c�t� de la courbure
Skittle::Pos
Skittle::bend (const Zone &zone)
{
    /// Border
    const int border = oconfig_->skittleBorder;
    /// Initialisation du saut de point
    int jump = 1; //oconfig_->jumpPointDist/oconfig_->skittleDivJump;
    if (!jump) jump = 1;
    /// Initialisation du point de d�part
    const int startY = img_->minWithBorder ((int)(zone.ymin + (zone.ymax - zone.ymin) * 0.25), 0);
    /// Point sup�rieur de la quille
    int upperPoint;
    /// Recherche du premier et du dernier point
    int firstPoint = img_->maxWithBorder (zone.xmin + border, img_->width_);
    int lastPoint = img_->minWithBorder (zone.xmax - border, 0);
//    int limitPoint = firstPoint > lastPoint ? firstPoint : lastPoint; 

    int limitPoint = climb (zone.centerx, startY, zone.color);
//    std::cout << "<Skittle::bend> " << firstPoint << " " << lastPoint << " " << limitPoint << std::endl;
    int above = 0; int below = 0;
//    std::cout << "<Skittle::bend> ";
    int last = 0; 
    int trash = 0;
    /// Parcours des autres points
    for (int i = firstPoint + jump; i < lastPoint - jump; i+=jump)
      {
	upperPoint = climb (i, startY, zone.color);
	if (last == upperPoint)
	    ++trash;
	else
	  {
//	    std::cout << upperPoint << " ";
	    if (upperPoint > limitPoint) ++below;
	    else ++above;
	  }
	last = upperPoint;
      }
//    std::cout << std::endl;
    const int score = (int)((lastPoint - firstPoint - trash) / jump * oconfig_->skittleScoreBendRatio) - 2;

//    std::cout << "-- " << above << " " << below << " " << score << std::endl;
    Pos bending = error;
    if (above > score) bending = up;
    else if (below > score) bending = down;
    
    return bending;
}

/// Analyse la zone
bool
Skittle::analyse (Zone &zone, const std::vector<Zone> &listReflectBand)
{
    //    delete f; delete f2;
//    f = new std::ofstream ("data", std::ios_base::trunc);
//    f2 = new std::ofstream ("data2", std::ios_base::trunc);
    // group est partiel ? 
    // if (zone.partial)
    // quille verticale sur l'image ?
    pca (zone);
    zone.ratio = (zone.ymax - zone.ymin)/(double)(zone.xmax - zone.xmin);
    if ((pcY_ > 0.95) || (pcY_ < -0.95))
	zone.vertical = true;
    else zone.vertical = false;
    zone.pcX = pcX_;
    zone.pcY = pcY_;
//    if ((zone.centery > 140) && (pcX_ > 0.98) && (zone.density > 0.7) 
//	&& (zone.density < 0.85) && (zone.ratio > 0.6) && (zone.ratio < 0.8))
//	return false;
//    if (zone.vertical)
//      {
//	// debout ou couch� ?
	//bend_ = bend (zone);
//	whereIsReflectBand (zone, listReflectBand);
//      }
//    else
//      {
//	bend_ = down;
	// calcul de l'orientation
//	pca (zone);
//      }

//    bend (zone);
//    feet_ = whereIsBottom (zone);
//    if (checkArea (zone) == 1) 
//	zone.alone = true;
//    else zone.alone = false;
    

//    f->close ();
//    f2->close ();
    return true;
}
    
/// Test si la zone contient les vis
double
Skittle::isBottom (const Zone &zone, const Pos pos)
{
    int newCenterx, newCentery;
    // Recherche de la zone � analyser
    int shiftx = (int)(pcX_*(zone.xmax-zone.centerx));
    int shifty = (int)(1.2*pcY_*(zone.ymax-zone.centery));
    if (((pos == down) && (pcY_ > 0)) || ((pos == up) && (pcY_ <0)))
      {
	shiftx *= -1;
	shifty *= -1;
      }
    newCenterx = (int)(zone.centerx - shiftx);
    newCentery = (int)(zone.centery - shifty);
    //std::cout << "--- " << (zone.xmax - zone.xmin) * (zone.ymax - zone.ymin);
    //std::cout << "%%%% " << newCenterx << " " << newCentery << std::endl;
    //std::cout << "%%% " << zone.centerx << " " << zone.centery << std::endl;
    //std::cout << "%% " << zone.xmin << " " << zone.xmax << " " << zone.ymin << " " << zone.ymax << std::endl;
    // Recherche des extr�mit�s
    int startx = (int)(newCenterx - (zone.xmax - zone.xmin)*pc2X_);
    int endx = (int)(newCenterx + (zone.xmax - zone.xmin)*pc2X_);
    //std::cout << "% " << startx << " " << endx << std::endl;
    // Test si la zone est valide
    if ((startx < 0) || (endx > img_->width_))
	return -1;
    // Parcours de la zone
    int last = -1;
    int diff = 0;
    int y, color;
    for (int x=startx; x < endx; ++x)
      {
	y = (int)(newCentery + (x - newCenterx)/2*pc2Y_);
	if ((y < 0) || (y > img_->height_))
	    return -1;
	color = segm_->giveColor (img_->tabData_ + ((y*img_->width_ + x) * 3), true, true); 
	if (color != last)
	  {
	    ++diff;
	    last = color;
	  }
      }
    //std::cout << diff << std::endl;
    return diff;
}

/// Cherche les vis de la quille
Skittle::Pos
Skittle::whereIsBottom (const Zone &zone)
{
    double isUp, isDown;
    isUp = isBottom (zone, up);
    isDown = isBottom (zone, down);
    if ((isUp == -1) || (isDown == -1))
	return error;
    if (isUp > isDown)
	return up;
    else if (isDown> isUp)
	return down;
    return error;
}

/// Verifie la coh�rence de la taille de la quille
int
Skittle::checkArea (const Zone &zone)
{
    if (zone.ymax < 150)
	if ((zone.xmax - zone.xmin) * (zone.ymax - zone.ymin) > 14000)
	    return 2;
    else if (zone.ymax >= 150)
	if ((zone.xmax - zone.xmin) * (zone.ymax - zone.ymin) > 19000)
	    return 2;
    return 1;
}


/// Affiche les infos sur la quille
void 
Skittle::show () const
{
    std::cout << "<Skittle::show> position : " << (bend_ == up ? "debout"
					       : (bend_ == down ? "couch�" : "error")) << "\n";
    std::cout << "<Skittle::show> orientation : " << pcX_ << ", " << pcY_ << "\n";
    std::cout << "<Skittle::show> catadiopre: " << (reflectBand_ == up ? "up"
					       : (reflectBand_ == down ? "down" 
					       : (reflectBand_ == farAway ? "farAway" : "error"))) << std::endl;
    std::cout << "<Skittle::show> vis : " << (feet_ == up ? "up"
					       : (feet_ == down ? "down" : "error")) << std::endl;
}