summaryrefslogtreecommitdiff
path: root/2005/i/robert/src/ovision/see/imgInterface.cc
blob: d41000d237c7b99f1f8cdcdf269e8a3f22f4de61 (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
/// @file imgInterface.cc Chargement des images, conversion en YUV, HSI, detection des contours, transformation d'une image segmentee en RGB, ecriture de l'image sur le disque

#include "imgInterface.hh"
#include <iostream>
#include <vector>

const unsigned char ImgInterface::tabCol_[9][3] = 
	      {{0, 0, 0},  {255, 255, 255},{0, 0, 255},
	       {0,255,0},  {255, 0, 0},    {0, 150, 60},
               {150,60,0}, {0, 150, 60},   {20, 50, 120}};

/// Constructeur
ImgInterface::ImgInterface ()
    : tabOut_ (0)
{}

/// Destructeur
ImgInterface::~ImgInterface ()
{
    delete [] tabOut_;
}

/// Cree un tableau en RGB pour l'affichage a partir d'une image segmentee
/// @param *tabIn pointeur vers un tableau de donnees segmentees
/// @param *tabOut_ pointeur vers un tableau de donnees RGB
void
ImgInterface::doImg (const unsigned char *tabIn) 
{
    /// Initialise les donn�es
    if (!tabIn) tabIn = tabSegm_;
    if (!tabOut_)
	tabOut_ = new unsigned char [nbPixels_ * 3];
    /// Remplissage de tabOut
    if (tabIn) 
      {
	// Change les couleurs segmentees en valeurs RGB pour l'affichage
	for (int i=0; i<(int)(nbPixels_);i++) 
	  {
	    tabOut_[i*3] = tabCol_[tabIn[i]][0];
	    tabOut_[i*3+1] = tabCol_[tabIn[i]][1];
	    tabOut_[i*3+2] = tabCol_[tabIn[i]][2];
	  }
      }
    else
      {
	// Si la table donnee est vide on renvoie des couleurs noires
	for (int i=0; i<(int)(nbPixels_);i++) 
	  {
	    tabOut_[i*3] = 0;
	    tabOut_[i*3+1] = 0;
	    tabOut_[i*3+2] = 0;
	  }
      }
}

    
/// Dessine les contours autour d'un objet
void
ImgInterface::drawBox (unsigned char *tab, const std::vector<Zone> &list)
{
    unsigned char *pTabMin, *pTabMax;
    // Parcours de la liste des groupes
    for (std::vector<Zone>::const_iterator iter = list.begin (); iter != list.end (); ++iter)
      {
	// Affiche les coutours horizontaux
	pTabMin = &tab[iter->ymin*width_+iter->xmin];
	pTabMax = &tab[iter->ymax*width_+iter->xmin];
	for (int x=iter->xmin; x<iter->xmax; x++)
	  {
	    *pTabMin = 1;
	    *pTabMax = 1;
	    ++pTabMin;
	    ++pTabMax;
	  }
	// Affiche les courtours verticaux
	pTabMin = &tab[iter->ymin*width_+iter->xmin];
	pTabMax = &tab[iter->ymin*width_+iter->xmax];
	for (int x=iter->ymin; x<iter->ymax; x++)
	  {
	    *pTabMin = 1;
	    *pTabMax = 1;
	    pTabMin += width_;
	    pTabMax += width_;
	  }

	// Affiche une croix au centre
	pTabMin = &tab[iter->centery*width_+iter->centerx-5];
	pTabMax = &tab[(iter->centery-5)*width_+iter->centerx];
	for (int x=0; x<10; x++)
	  {
	    *pTabMin = 1;
	    *pTabMax = 1;
	    ++pTabMin;
	    pTabMax += width_;
	  }
      }
}

/// Ajoute des coutours autour des objets trouv�es
void
ImgInterface::addGroupToDisplay (unsigned char *tab, const  std::vector<Zone> &list)
{
    if (!tab)
      {
	std::cerr << "ImgInterface::AddGroupToDisplay tab empty" << std::endl; 
	return;
      }
    drawBox (tab, list);
}

    
/// Mirroir l'image
void 
ImgInterface::mirror ()
{
    const int totalWidth = width_*3;
    unsigned char buf[totalWidth];
    /// Inverse l'image
    for (int i=0; i<height_; ++i)
      {
	memcpy (buf, &tabData_[i*totalWidth], totalWidth);
	for (int j=0; j<width_; ++j)
	  {
	    tabData_[i*totalWidth+j*3+2] = buf[totalWidth-j*3+2];
	    tabData_[i*totalWidth+j*3+1] = buf[totalWidth-j*3+1];
	    tabData_[i*totalWidth+j*3] =   buf[totalWidth-j*3];
	  }
      }
}

/// Creation du tableau de RGB pour faire une image 
void 
ImgInterface::doGroupImg (const std::vector<Zone> &zoneList, bool init) 
{
    // On verifie que des groupes ont ete trouve
    if (zoneList.empty ()) {
	std::cerr << "ImgInterface::doGroupImg: No group defined" << std::endl;
	init = 1;
	return;
    }
    if (init)
      {
	// Allocation de la memoire
	if (!tabSegm_)
	    tabSegm_ = new unsigned char[nbPixels_];
	// On initialise le tableau pour une image noire
	for (unsigned int i=0; i<nbPixels_; i++)
	    tabSegm_[i] = 0;
      }
    else
      {
	// Allocation de la memoire
	if (!tabSegm_)
	    tabSegm_ = new unsigned char[nbPixels_];
      }
    // Parcours de la liste des zones trouvees
    for (std::vector<Zone>::const_iterator iter = zoneList.begin ();
	 iter < zoneList.end (); ++iter)
	// Remplissage de la zone avec une couleur
	for(int i=iter->xmin; i<iter->xmax; i++)
	    for (int j=iter->ymin; j<iter->ymax; j++)
		tabSegm_[j*width_+i] = iter->id+1;
    std::cout << std::endl;
}