summaryrefslogtreecommitdiff
path: root/2004/i/nono/src/ovision/segmTable.cc
blob: aa252c2d86cec275300715d4ca3c4313ebbf0a3e (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
// segmTable.cc - Classe Segmentation
// nono - Programme du robot Efrei Robotique I1-I2 2004
// Copyright (C) 2004 Olivier Gaillard

/// @file segmTable.cc Segmente l'image et cree un tableau contenant des valeurs segmentees, creation du reseau de neurones
#include "segmTable.h"
#include "convertImg.h"
#include <fstream>
#include <iostream>

static const int COLOR_TAB_SIZE  = 256; 
static const int COLOR_TAB_SIZE_2  = COLOR_TAB_SIZE*COLOR_TAB_SIZE; 
static const int COLOR_TAB_SIZE_TOTAL  = COLOR_TAB_SIZE_2*COLOR_TAB_SIZE; 

/// Constructor SegmTable

/// @param img classe img permettant d'acceder au donnees de l'image a traiter
/// @param oconfig classe oconfig permettant d'acceder aux variables de oconfiguration
SegmTable::SegmTable (Img *img, bool loadFromFile) : SegmNN (img) 
{
    if (loadFromFile)
	LoadColorTableFile ();
    else
	colorTable = NULL;
}


/// Destructor SegmTable
SegmTable::~SegmTable ()
{
    delete [] colorTable;
}


/// Cree un tableau des couleurs segment�es pour ne plus faire de calcul
/// et augmenter la rapidit�
/// @param testOutputMax choix de l'utilisation d'un sueil maxi pour la sortie pour �viter qu'une couleur trop diff�rente soit attribuer � une autre couleur
void
SegmTable::DoColorTable (bool testOutputMax, char colorMode)
{
    unsigned char x[3];

    delete [] colorTable;
    colorTable = new unsigned char[COLOR_TAB_SIZE_TOTAL];

    ConvertImg conv;

    if (colorMode == YUV)
	conv.RGBtoYUV (*img);
    if (colorMode == HSI)
	conv.RGBtoHSI (*img);
    
    
    // Parcours de toutes les valeurs possibles
    for (int i=0; i<COLOR_TAB_SIZE; i++)
	for (int j=0; j<COLOR_TAB_SIZE; j++)
	    for (int k=0; k<COLOR_TAB_SIZE; k++)
	      {
		// Remplissage de la table
		x[0]=i; x[1]=j; x[2]=k;	
		colorTable[ i*COLOR_TAB_SIZE_2 + j*COLOR_TAB_SIZE +k ] = FindColorNN (x , testOutputMax);
	      }
}


/// Donne la couleur � partir du tableau
/// @param *x pointeur vers un tableau contenant une valeur RGB
unsigned char
SegmTable::GiveColor (unsigned char *x, bool testOutputMax, bool indexProcess)
{
//    if (indexProcess)
	return index [colorTable[ x[0]*COLOR_TAB_SIZE_2 + x[1]*COLOR_TAB_SIZE + x[2] ]];
//    else
//	return colorTable[ x[0]*COLOR_TAB_SIZE_2 + x[1]*COLOR_TAB_SIZE + x[2] ];
}


/// Cree un fichier de ColorTable
void 
SegmTable::CreateColorTableFile (char *filename)
{
    std::ofstream file;

    file.open (filename);
    
    if (file)
	file.write ((char*)colorTable, COLOR_TAB_SIZE_TOTAL);
    else
	std::cerr << "SegmTable::CreateColorTableFile Erreur d'ouverture de fichier" << std::endl;
    
    file.close ();
}




/// Charge un fichier de ColorTable
bool
SegmTable::LoadColorTableFile (char *filename)
{
    delete [] colorTable;
    colorTable = new unsigned char[COLOR_TAB_SIZE_TOTAL];

    std::ifstream file;
    file.open (filename);
    
    if (file)
	file.read ((char*)colorTable, COLOR_TAB_SIZE_TOTAL);
    else
      {
	std::cerr << "SegmTable::LoadColorTableFile Erreur d'ouverture de fichier" << std::endl;
	return 0;
      }
    
    file.close ();
    return 1;
}