// group.cc - Classe G // robert - Programme du robot APBteam // Copyright (C) 2005 Olivier Gaillard /// @file group.cc Cree une liste chainee de zones correspondant aux balles #include #include #include #include "group.hh" /// Constructeur /// @param *img classe image /// @param *segm classe segm Group::Group (Img *img, Segm *segm) : segm_ (segm), img_ (img) { oconfig_ = OConfig::getInstance (); } /// Ajoute une balle ou un poteau à la liste de groupes /// @param idColor numéro de la couleur du group /// @param xmin,xmax,ymin,ymax borne du group /// @param centerx, centery centre du group void Group::addZone (const int idColor, const int xmin, const int xmax, const int ymin, const int ymax) { Zone zone; zone.xmin = xmin; zone.xmax = xmax; zone.ymin = ymin; zone.ymax = ymax; zone.centerx = (xmax+xmin)/2; zone.centery = (ymax+ymin)/2; zone.color = idColor; for (std::vector::iterator iter = oconfig_->groupColor.begin (); iter != oconfig_->groupColor.end (); ++iter) if (idColor == iter->color) { if (iter->label == "redSkittle") zone.id = redSkittle; else if (iter->label == "greenSkittle") zone.id = greenSkittle; else if (iter->label == "border") zone.id = border; else if (iter->label == "base") zone.id = base; else if (iter->label == "gap") zone.id = gap; else if (iter->label == "reflectBand") zone.id = reflectBand; break; } zoneList_.push_back (zone); } /// Cherche l'objet complet a partir d'un pixel donne /// @param type type du group à rechercher GOAL ou BALL /// @param numColor numero de la couleur a chercher /// @param x,y coordonnees de depart pour la recherche void Group::plague (const unsigned char numColor, const int x, const int y) { int xmax = x; int xmin = x; int ymax = y; int ymin = y; // TODO ajouter une inertie ? int tmpY = y*img_->width_; // Parcours de l'objet trouve de haut en bas et de gauche à droite while ((xmax < img_->width_-1)&& (segm_->giveColor(img_->tabData_ + ((++xmax)+tmpY)*3, true, true) == numColor)) {} while ((xmin > 0) && (segm_->giveColor(img_->tabData_ + ((--xmin)+tmpY)*3, true, true) == numColor)) {} while ((ymax < img_->height_-1) && (segm_->giveColor(img_->tabData_ + (x+(++ymax)* img_->width_)*3, true, true) == numColor)) {} while ((ymin > 0) && (segm_->giveColor(img_->tabData_ + (x+(--ymin)* img_->width_)*3, true, true) == numColor)) {} // Ajout de la zone addZone (numColor, xmin, xmax, ymin, ymax); } /// Affiche les zones trouvees void Group::showZones () const { std::cout << "Groupes :\n"; if (!zoneList_.size ()) return; for (std::vector::const_iterator iter = zoneList_.begin (); iter != zoneList_.end (); ++iter) std::cout << (int)iter->id << " " << iter->xmin << " " << iter->xmax << " " << iter->ymin << " " << iter->ymax << "\n"; std::cout << std::endl; } /// Selectionne les points a tester dans l'image /// @param numColor numero de la couleur a trouver void Group::jumpPoints (int color) { std::vector list; ObjectColor obj; obj.label = "undefined"; obj.color = color; list.push_back (obj); jumpPoints (list); } void Group::jumpPoints (const std::vector &colorList) { // Initialisation zoneList_.clear (); segm_->clearLum (); segm_->setMode (img_->colorMode_); int tmpY; unsigned char curColor; std::vector::const_iterator iter; // Parcours d'une partie des pixels de l'image for (int y=0; yheight_; y+=oconfig_->jumpPointDist) { tmpY = y*img_->width_; for (int x=0; xwidth_; x+=oconfig_->jumpPointDist) { curColor = segm_->giveColor (img_->tabData_ + ((tmpY + x) * 3), true, true, true); for (iter = colorList.begin (); iter != colorList.end (); ++iter) if ((iter->color >= 0) && (iter->color == curColor)) { plague (curColor, x, y); break; } } } std::cout << " Luminosité : " << segm_->getLum () << std::endl; } /// Convertit le label de couleur en nombre std::string Group::translateToColorName (const int color) { std::string label; if (color == Group::redSkittle) label = "redSkittle"; else if (color == Group::greenSkittle) label = "greenSkittle"; else if (color == Group::gap) label = "gap"; else if (color == Group::border) label = "border"; else if (color == Group::base) label = "base"; else if (color == Group::reflectBand) label = "reflectBand"; else label = "undefined"; return label; }