#include "ovision.hh" #include #include #include /// Constructeur OVision::OVision (bool useSocket) : oconfig_ (0), img_ (0), colorTab_ (0), segm_ (0), eraser_ (0), group_ (0), mag_ (0), space_ (0), v4l_ (0), step_ (0), aim_ (Group::undefined) //map_ (0), { // socket_ = 0; } /// Destructeur OVision::~OVision () { delete oconfig_; delete img_; delete v4l_; delete segm_; delete group_; delete mag_; delete space_; delete colorTab_; } void OVision::readColorSkittle (ObjectColor &o) { if (aim_ == Group::redSkittle) { colorTab_ = new ColorTable ("../runtime/rc/colortableRed.z"); o.label = "redSkittle"; } else { colorTab_ = new ColorTable ("../runtime/rc/colortableGreen.z"); o.label = "greenSkittle"; std::cout << " color " << o.label << "\n"; } o.type = aim_; // reading color value std::ifstream file ("../runtime/rc/poids"); if (!file.is_open ()) throw std::string (" file poids not found\n"); o.color = -1; static const int bufferSize = 50; char buffer[bufferSize]; while (!file.eof ()) { file.getline (buffer, bufferSize); std::string line (buffer); if (line.find (o.label) != std::string::npos) { std::istringstream ss (line); ss >> line; ss >> o.color; break; } } if (o.color == -1) throw std::string (" error in file poids\n"); oconfig_->groupColor.clear (); oconfig_->groupColor.push_back (o); } /// Iniialisatoin de toutes les classes void OVision::init (const Group::ZoneType aim) { aim_ = aim; oconfig_ = new OConfig; // Initialisation des classes ObjectColor tmp; readColorSkittle (tmp); img_ = new Img; // Initialisation caméra v4l_ = new Video4Linux ("/dev/video", oconfig_->width, oconfig_->height, oconfig_->inputColor, oconfig_->brightness); v4l_->calibrate (); // Prendre une image pour que la taille de l'image soit configuré img_->load (*v4l_); // segm_ = new SegmTable (*colorTab_); // segm_->setMode (oconfig_->inputColor); segm_ = new SegmLearn; segm_->setMode (oconfig_->inputColor); segm_->buildNN (oconfig_->nnNbColor, Segm::loadFromFile); eraser_ = new Eraser (oconfig_->width, 10); eraser_->init (); group_ = new Group (img_, segm_, eraser_); mag_ = new Magnifier (img_, segm_, eraser_, aim_); // Mode de couleur utilisé pour les la segmentation segm_->setMode (oconfig_->inputColor); space_ = new Space (img_->width_, img_->height_); // Calibration des longueurs space_->addSetupPoint (56, 155, 50, 315); space_->addSetupPoint (65, 51, 50, 465); space_->addSetupPoint (164, 46, -50, 465); space_->setup (0.003, -2.106, 561.391); } /// Prends une image avec la caméra void OVision::takeShoot () { img_->load (*v4l_); } /// Analyse une image void OVision::update () { // Compteur du nombre d'image traité step_++; // Cherche les balles // group_->jumpPoints (oconfig_->groupColor); //group_->jumpPoints (0); // Analyse et tri la liste de zones trouvées //mag_->analyse (group_->getZoneList ()); // Parcours la liste de balles trouvées /* if (group_->zoneListBall) { int x,y; x = group_->zoneListBall->centerx; y = img_->height - group_->zoneListBall->centery; space->GetLoc (x, y, x, y); } */ // Création des groupes Group group (img_, segm_, eraser_); std::vector list; group.jumpPoints(oconfig_->groupColor); mag_->analyse (group.getZoneList ()); std::vector &l = mag_->getItemList (Group::greenSkittle); for (std::vector::iterator iter = l.begin (); iter != l.end (); ++iter) { mag_->showZone (*iter); } std::ostringstream ss; ss << "vis" << step_ << ".yuv"; std::string name = ss.str (); img_->writeRaw (name.c_str ()); } /// Affiche d'info sur l'update void OVision::showInfo (const Zone &zone) const { mag_->showZone (zone); } /// Renvoie la position de la quille dans le référentiel robot void OVision::getLoc (Zone &zone, int &x, int &y, double &angle, int &dist) { space_->getRealLoc (zone, x, y, angle, dist); } /// Renvoie la liste des quilles std::vector& OVision::getSkittles () const { return mag_->getItemList(aim_); } /// Renvoie le pointeur sur Video4Linux Video4Linux & OVision::getVideo4Linux () const { return *v4l_; } /// Envoie l'image par socket_ void OVision::sendImg () { // socket__->Send (img_->tabData); }