summaryrefslogtreecommitdiff
path: root/2004/i/nono/src/ovision/group.cc
diff options
context:
space:
mode:
authorgaillaro2004-05-01 11:52:08 +0000
committergaillaro2004-05-01 11:52:08 +0000
commit109db2b0cce1b89c75e32f3705a1c5975391b3c5 (patch)
tree28da709aee1cbab296b99378e42a1e737403addf /2004/i/nono/src/ovision/group.cc
parentf95f1fb2bc439178a9e7ca6102b57c02c791f3f0 (diff)
Ajout de ovision.
Diffstat (limited to '2004/i/nono/src/ovision/group.cc')
-rw-r--r--2004/i/nono/src/ovision/group.cc235
1 files changed, 235 insertions, 0 deletions
diff --git a/2004/i/nono/src/ovision/group.cc b/2004/i/nono/src/ovision/group.cc
new file mode 100644
index 0000000..603dd70
--- /dev/null
+++ b/2004/i/nono/src/ovision/group.cc
@@ -0,0 +1,235 @@
+// group.cc - Classe Group
+// nono - Programme du robot Efrei Robotique I1-I2 2004
+// Copyright (C) 2004 Olivier Gaillard
+
+/// @file group.cc Cree une liste chainee de zones correspondant aux balles
+
+#include "group.h"
+#include <iostream>
+#include <stdlib.h>
+#include <math.h>
+using namespace std;
+
+#define BALL 1
+#define GOAL 2
+
+
+/// Constructeur
+
+/// @param *img classe image
+/// @param *segm classe segmNN
+Group::Group(Img *img, SegmNN *segm) {
+ // Sauvegarde des pointeurs
+ Group::img = img;
+ Group::segm = segm;
+
+ width = img->width;
+ height = img->height;
+
+ tabOut = NULL;
+ zoneListBall = NULL;
+ zoneListGoal = NULL;
+}
+
+
+/// Destructeur
+Group::~Group() {
+ ZONE *pCur = zoneListBall;
+ ZONE *pPrev;
+
+ while (pCur)
+ {
+ pPrev = pCur;
+ pCur = pCur->next;
+
+ free(pPrev);
+ }
+
+ pCur = zoneListGoal;
+ while (pCur)
+ {
+ pPrev = pCur;
+ pCur = pCur->next;
+
+ free(pPrev);
+ }
+}
+
+
+/// Cherche l'objet complet a partir d'un pixel donne
+
+/// @param numColor numero de la couleur a chercher
+/// @param x,y coordonnees de depart pour la recherche
+void Group::Plague(int type, unsigned char numColor, int x, int y) {
+ unsigned int xmax = x;
+ unsigned int xmin = x;
+ unsigned int ymax = y;
+ unsigned int ymin = y;
+
+ // TODO ajouter une inertie ?
+
+ // Parcours de l'objet trouve de haut en bas
+ while ((xmax < width-1)&& (tabSegm[(++xmax)+y*width] == numColor)) {}
+ while ((xmin > 0) && (tabSegm[(--xmin)+y*width] == numColor)) {}
+ while ((ymax < height-1) && (tabSegm[x+(++ymax)*width] == numColor)) {}
+ while ((ymin > 0) && (tabSegm[x+(--ymin)*width] == numColor)) {}
+
+ // Verification la validite des entrees
+/* if ((xmin < 0) || (xmin > width))
+ xmin = 0;
+ if ((xmax > img->width) || (xmax < 0))
+ xmax = img->width;
+ if ((ymin < 0) || (ymin > height))
+ ymin = 0;
+ if ((ymax > img->height) || (ymax < 0))
+ ymax = img->height;
+*/
+ // Calcul du centre de l'image
+ int centerx, centery;
+ centerx = (xmax+xmin)/2;
+ centery = (ymax+ymin)/2;
+
+ ZONE *pCur;
+
+ if (type == BALL)
+ pCur = zoneListBall;
+ else if (type == GOAL)
+ {
+ if (!((pLast->ymin <= 0) && (pLast->ymax >= config->width)))
+ return;
+ pCur = zoneListGoal;
+ }
+
+ ZONE *pLast=NULL;
+ while (pCur) {
+ // si on a deja ce groupe on actualise les donnees du groupe
+ if ((numColor == pCur->idColor) && (abs(pCur->centerx - centerx) <= DELTA) && (abs(pCur->centery - centery) <= DELTA)) {
+ if (xmin < pCur->xmin) pCur->xmin = xmin;
+ if (xmax > pCur->xmax) pCur->xmax = xmax;
+ if (ymin < pCur->ymin) pCur->ymin = ymin;
+ if (ymax > pCur->ymax) pCur->ymax = ymax;
+
+ pCur->centerx = (pCur->xmax+pCur->xmin)/2;
+ pCur->centery = (pCur->ymax+pCur->ymin)/2;
+
+ return;
+ }
+
+ pLast = pCur;
+ pCur = pCur->next;
+ }
+
+
+ // Si il n'est pas presente on l'ajoute
+ if (!pCur) {
+ if (!pLast)
+ {
+ if (type == BALL)
+ {
+ zoneListBall = new ZONE;
+ pLast = zoneListBall;
+ }
+ else if (type == GOAL)
+ {
+ zoneListGoal = new ZONE;
+ pLast = zoneListGoal;
+ }
+ }
+ else
+ {
+ pLast->next = new ZONE;
+ pLast = pLast->next;
+ }
+
+ pLast->xmin = xmin;
+ pLast->xmax = xmax;
+ pLast->ymin = ymin;
+ pLast->ymax = ymax;
+
+ pLast->centerx = centerx;
+ pLast->centery = centery;
+ pLast->idColor = numColor;
+
+ // test si la balle est vu partiellement ou completement
+ if ((pLast->xmin <= 0) || (pLast->ymin <= 0) || (pLast->xmax >= config->width) || (pLast->ymax >= config->height))
+ pLast->partial = 1;
+ else pLast->partial = 0;
+
+
+
+
+ pLast->next = NULL;
+ }
+}
+
+
+/// Affiche les zones trouvees
+void Group::ShowZones() {
+
+ ZONE *pCur = zoneListBall;
+ cout << "Groupes balles:" << endl;
+ while(pCur) {
+ printf("%u %i %i %i %i\n", pCur->idColor, pCur->xmin, pCur->xmax, pCur->ymin, pCur->ymax);
+ pCur = pCur->next;
+ }
+
+ ZONE *pCur = zoneListGoal;
+ cout << "Groupes balles:" << endl;
+ while(pCur) {
+ printf("%u %i %i %i %i\n", pCur->idColor, pCur->xmin, pCur->xmax, pCur->ymin, pCur->ymax);
+ pCur = pCur->next;
+ }
+}
+
+
+/// Selectionne les points a tester dans l'image
+/// @param numColor numero de la couleur a trouver
+void Group::JumpPoints(unsigned char numColor) {
+
+ tabSegm = segm->tabSegm;
+
+ // Initialisation de la couleur a chercher
+ numColor = segm->index[numColor];
+
+ // Parcours d'une partie des pixels de l'image
+ for (unsigned int x=0; x<width; x+=10) {
+ for (unsigned int y=0; y<height; y+=10) {
+ if (tabSegm[y*width+x] == numColor)
+ Plague(BALL, numColor, x, y);
+ }
+ }
+
+}
+
+
+/// Creation du tableau de RGB pour faire une image
+void Group::TabOut() {
+ ZONE *pCur = zoneListBall;
+
+ // On verifie que des groupes ont ete trouve
+ if (!pCur) {
+ cerr << "Group::TabOut : No group defined" << endl;
+ if (tabOut) delete [] tabOut;
+ return;
+ }
+
+ // Allocation de la memoire
+ if (tabOut) delete [] tabOut;
+ tabOut = new unsigned char[img->nbPixels];
+
+ // On initialise le tableau pour une image noire
+ for (unsigned int i=0; i<img->nbPixels; i++)
+ tabOut[i] = 0;
+
+ // Parcours de la liste des zones trouvees
+ while (pCur) {
+ // Remplissage de la zone avec une couleur
+ for(unsigned int i=pCur->xmin; i<pCur->xmax; i++)
+ for (unsigned int j=pCur->ymin; j<pCur->ymax; j++)
+ tabOut[j*img->width+i] = pCur->idColor+1;
+
+ pCur = pCur->next;
+ }
+}
+
+