-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcargadorDeImagenes.cpp
More file actions
192 lines (141 loc) · 6 KB
/
Copy pathcargadorDeImagenes.cpp
File metadata and controls
192 lines (141 loc) · 6 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
// Created by Gonzalo on 17/05/2018.
//
#include <iostream>
#include <fstream>
#include <sstream>
#include "cargadorDeImagenes.h"
//REVISAR ESTAS FUNCIONES
//Copiadas del ppmloader
double obtenerPixel(uchar* datos, int i, int j, int alto, int ancho){
if(i > alto)
throw std::runtime_error("LA COORDENADA i (la primera) SUPERA LA ALTURA VALIDA");
if(j > ancho)
throw std::runtime_error("LA COORDENADA j (la segunda) SUPERA EL ANCHO VALIDA");
// unsigned int red = (unsigned int)(datos[i*ancho*3 + j*3 + 0]);
// unsigned int green = (unsigned int)(datos[i*ancho*3 + j*3 + 1]);
// unsigned int blue = (unsigned int)(datos[i*ancho*3 + j*3 + 2]);
//
// //std::cout << "COLORES EN RGB: (" << red << ", " << green << ", " << blue << ")" << std::endl;
//
// return (unsigned int)((red+green+blue) / 3);
// uchar pixel = (datos[i*ancho + j]);
double pixel = (double) (datos[i*ancho + j]);
return pixel;
}
void leerImagen(std::string filename, uchar** datos, int* ancho, int* alto){
*datos = NULL;
*ancho = 0;
*alto = 0;
PPM_LOADER_PIXEL_TYPE pt = PPM_LOADER_PIXEL_TYPE_INVALID;
//std::cout << "ARCHIVO A CARGAR: " << filename.c_str() << std::endl;
bool ret = LoadPPMFile(datos, ancho, alto, &pt, filename.c_str());
if (!ret || ancho == 0|| alto == 0|| pt == PPM_LOADER_PIXEL_TYPE_INVALID){
//Predicado anterior: (!ret || ancho == 0|| alto == 0|| pt!=PPM_LOADER_PIXEL_TYPE_RGB_8B)
throw std::runtime_error("Fallo al leer la imagen.");
}
}
cargadorDeImagenes::cargadorDeImagenes(const char *archivo) {
std::ifstream listaDeNombres(archivo);
if(listaDeNombres.eof()){
std::cout << "EL ARCHIVO ESTA VACIO O NO EXISTE" << std::endl;
return;
}
std::string nombreArchivoDeImagen;
int IDPersona;
//HAY QUE VER SI ESTO FUNCA
//std::cout << "CONSTRUCTOR ANTES DE CICLO" << std::endl;
std::stringstream conversor;
std::string delimiter = ",";
for(std::string linea; std::getline(listaDeNombres, linea); ){
//std::cout << linea << std::endl;
//Vacio el conversor
conversor.clear();
conversor.str("");
//Separamos la linea y copio la direccion del archivo
conversor << linea.substr(0, linea.find(delimiter));
conversor >> nombreArchivoDeImagen;
//Borro el nombre del archivo de la line actual
linea.erase(0, linea.find(delimiter) + delimiter.length());
//Vacio el conversor
conversor.clear();
conversor.str("");
//Pongo la ID de la persona en el conversor
conversor << linea.substr(0, linea.find(delimiter));
//Convierto el ID a INT
conversor >> IDPersona;
//std::cout << "ARCHIVO: " << nombreArchivoDeImagen << std::endl;
//std::cout << "ID DE PERSONA: " << IDPersona << std::endl;
//Cargo la Imagen
cargarImagen(nombreArchivoDeImagen, IDPersona);
_rutasImagenes.push_back(nombreArchivoDeImagen);
}
listaDeNombres.close();
}
void cargadorDeImagenes::cargarImagen(std::string rutaArchivo, int IDPersona) {
//std::cout << "CARGANDO IMAGEN: " << rutaArchivo << "; ID: " << IDPersona << std::endl;
//UNA CUENTA SE DEFASA Y CARGA MAL LAS COSAS, POR EJEMPLO SI CARGAS EL MISMO ARCHIVO DOS VECES (PARA VER
// SI EL VECTOR ES EL MISMO) ENTOCES EN LA POSICION 3437 HAY US DEFASAJE Y A PARTIR DE AHI SE ANULA EL VECTOR
//Uso el ppmloader para cargar el archivo dado
uchar *datos = NULL;
int ancho = 0, alto = 0;
//rutaArchivo = "../" + rutaArchivo;
leerImagen(rutaArchivo, &datos, &ancho, &alto);
//std::cout << "ANCHO DE LA IMAGEN: " << ancho << std::endl;
//std::cout << "ALTO DE LA IMAGEN: " << alto << std::endl;
std::vector<double> vectorPixeles(alto*ancho, 0);
// int numPixel = 0;
// //Obtengo la info de cada pixel
// for(int h = 0; h < alto; ++h){
// for(int w = 0; w < ancho; ++w){
//
// vectorPixeles.push_back(obtenerMediaDePixel(datos, h, w, alto, ancho));
// numPixel++;
// }
// }
for(int p = 0; p < alto*ancho; p++) {
//vectorPixeles[p] = datos[p];
vectorPixeles[p] = (double) datos[p];
}
//std::cout << "TAMAÑO DEL VECTOR DE PIXELES: " << vectorPixeles.size() << std::endl;
// std::cout << "CONTENIDO DE PIXELES DEL VECTOR: " << vectorPixeles.size() << std::endl;
//
// for(int i = 0; i < vectorPixeles.size(); i++){
// std::cout << "PIXEL: " << i << ", VALOR: " << vectorPixeles[i] << std::endl;
// }
// std::cout << "MUESTRO EL VECTOR PARA LA PRUEBA CHICA: " << std::endl;
// std::cout << "[";
//
// for(int i = 0; i < vectorPixeles.size(); i++){
// std::cout << "(" << vectorPixeles[i] << ", PIXEL: " << i << ")" << ((i + 1 == vectorPixeles.size())? (""): (", "));
// }
//
// std::cout << "]" << std::endl;
//Ahora el vector esta cargado con los pixeles de la imagen (en escala de grises)
//entonces lo pongo en el vector de imagenes
_imagenes.push_back(std::make_pair(vectorPixeles, IDPersona));
//Borro lo contenido en datos
delete [] datos;
}
std::vector< std::pair<std::vector<double>, int> > cargadorDeImagenes::conjuntoDeImagenes() {
return _imagenes;
}
std::vector<int> cargadorDeImagenes::clases() {
std::vector<int> _clases(_imagenes.size());
//Consiguo el arreglo de clases
for(int i = 0; i < _clases.size(); i++){
_clases[i] = _imagenes[i].second;
}
return _clases;
}
std::vector<std::vector<double>> cargadorDeImagenes::vectoresDeImagenes() {
std::vector<std::vector<double>> _vectores(_imagenes.size());
//Consiguo el arreglo de clases
for(int i = 0; i < _vectores.size(); i++){
_vectores[i] = _imagenes[i].first;
}
return _vectores;
}
std::vector<std::string> cargadorDeImagenes::rutas() {
return _rutasImagenes;
}