summaryrefslogtreecommitdiff
path: root/2004/i/nono/src/ovision/segmTh.cc
blob: dea549105ad2fe4f1aa7086cbcae2b7dd5d5683a (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
// segmTh.cc - Classe Segmentation
// nono - Programme du robot Efrei Robotique I1-I2 2004
// Copyright (C) 2004 Olivier Gaillard
#include "segmTh.h"
#include <iostream>
#include <stdlib.h>
using namespace std;

SegmTh::SegmTh(Img *img, OConfig *config) {
    SegmTh::img = img;
    SegmTh::config = config;
    nbOutput = config->nn_NbCouleurs;

    tabSegm = NULL;
}


SegmTh::~SegmTh()
{
}


void SegmTh::CreateThresholds(char *filename, unsigned char *node) {

    int weight[nbOutput][3];
    int seuil[nbOutput][3][2];
    int tmp;
   
    if (!node) {
	cerr << "SegmTh::CreateThresholds : NN non initialis�\n";
	return;
    }
   
    // Classement des seuils par ordre croissant
    for (int comp=0; comp<3; comp++) {

	// Tri par insertion pour les 3 composantes avec un tableau d'index
	for(int i=0; i<nbOutput; i++) {
	    weight[i][comp] = i;
	    
	    int j=i;
	    while ((j>0) && (node[weight[j][comp]*3+comp] < node[weight[j-1][comp]*3+comp])) {
		tmp = weight[j-1][comp];
		weight[j-1][comp]=weight[j][comp];
		weight[j][comp]=tmp;
		j--;
		
	    }
	}
    }

    // Formation des seuils
    for (int comp=0; comp<3  ; comp++) {
	for (int i=0; i<nbOutput; i++) {
	    if (i==0) {
		seuil[weight[i][comp]][comp][MIN] = 0; 
		seuil[weight[i][comp]][comp][MAX] =  
		    node[weight[i][comp]*3+comp]-(node[weight[i][comp]*3+comp]-node[weight[i+1][comp]*3+comp])/2;
		
	    }
	    else if (i==nbOutput-1) {
		seuil[weight[i][comp]][comp][MIN] = 
		    node[weight[i][comp]*3+comp]-(node[weight[i][comp]*3+comp]-node[weight[i-1][comp]*3+comp])/2;
		seuil[weight[i][comp]][comp][MAX] = 255;
	    }
	    else {
		seuil[weight[i][comp]][comp][MIN] =
		    node[weight[i][comp]*3+comp]-(node[weight[i][comp]*3+comp]-node[weight[i-1][comp]*3+comp])/2;
		seuil[weight[i][comp]][comp][MAX] =
		    node[weight[i][comp]*3+comp]-(node[weight[i][comp]*3+comp]-node[weight[i+1][comp]*3+comp])/2;
	    }
	}
    }



    // Ecriture dans un fichier
    FILE *file;
    file = fopen(filename, "w+");
   
    fprintf(file, "#x1Min\tx1Max\tx2Min\tx2Max\tx3Min\tx3Max\n");
    for (int i=0; i<nbOutput; i++) {
	for (int j=0; j<3; j++)
	    fprintf(file, "%u\t%u\t", seuil[i][j][MIN], seuil[i][j][MAX]);
	fprintf(file, "\n");

    }

    fclose(file);
}


unsigned char SegmTh::FindzoneTh (unsigned char *x) {
    unsigned char *pColor;

    // Parcours de toutes les couleurs possibles
    for (int i=0; i<nbOutput; i++) {
	pColor = &config->color[i*6];

	// On test pour savoir si la couleur correspond au seuils
	if (x[0] >= pColor[0] && x[0] <= pColor[1] && 
	    x[1] >= pColor[2] && x[1] <= pColor[3] &&
	    x[2] >= pColor[4] && x[2] <= pColor[5])
		return i;
    }

    // Si aucune couleur ne correspond
    return nbOutput;
}


void SegmTh::Segm () {
    if (tabSegm) delete [] tabSegm;
    tabSegm = new unsigned char[img->nbPixels];
    if (config->nbCouleurMax < nbOutput) {
	cerr << "SegmTh::Segm : Nb de couleurs dans le fichier threshold insuffisante\n";
	return;
    }

    
    // Parcours de l'image et d�termination de la couleur
    for (int i=0; i<(int)(img->nbPixels); i++) {
	tabSegm[i] = FindzoneTh(img->tabData+i*3);
    }
}