forked from einsnull/DeepLearningToolBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataAndImage.h
More file actions
357 lines (330 loc) · 7.19 KB
/
dataAndImage.h
File metadata and controls
357 lines (330 loc) · 7.19 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#pragma once
#include <opencv2/opencv.hpp>
#include <Eigen/Dense>
#include <iostream>
#include <fstream>
using namespace std;
using namespace Eigen;
#pragma comment(lib,"opencv_core243.lib")
#pragma comment(lib,"opencv_highgui243.lib")
#pragma comment(lib,"opencv_imgproc243.lib")
bool loadMnistData(MatrixXd &data,char *szFileName)
{
FILE *fp;
fopen_s(&fp,szFileName,"rb");
if(!fp)
{
cout << "Could not Open " << szFileName << endl;
return false;
}
unsigned int magic = 0;
unsigned char temp;
unsigned int numOfImages = 0;
unsigned int rows;
unsigned int cols;
for(int i = 0;i < 4;i++)
{
if(feof(fp))
{
fclose(fp);
return false;
}
fread(&temp,sizeof(char),1,fp);
magic = magic << 8 | temp;
}
//printf("magic: %d\n",magic);
for(int i = 0;i < 4;i++)
{
if(feof(fp))
{
fclose(fp);
return false;
}
fread(&temp,sizeof(char),1,fp);
numOfImages = numOfImages << 8 | temp;
}
//printf("numOfImages: %d\n",numOfImages);
for(int i = 0;i < 4;i++)
{
if(feof(fp))
{
fclose(fp);
return false;
}
fread(&temp,sizeof(char),1,fp);
rows = rows << 8 | temp;
}
//printf("rows: %d\n",rows);
for(int i = 0;i < 4;i++)
{
if(feof(fp))
{
fclose(fp);
return false;
}
fread(&temp,sizeof(char),1,fp);
cols = cols << 8 | temp;
}
//printf("cols: %d\n",cols);
data.resize(rows*cols,numOfImages);
for(int i = 0;i < (int)numOfImages;i++)
{
for(int j = 0; j < (int)(rows * cols); j++)
{
if(feof(fp))
{
cout << "Error reading file" << endl;
fclose(fp);
return false;
}
fread(&temp,sizeof(char),1,fp);
data(j,i) = (temp / 255.0);
}
}
fclose(fp);
return true;
}
bool loadMnistLabels(MatrixXi &labels,char *szFileName)
{
FILE *fp;
fopen_s(&fp,szFileName,"rb");
if(!fp)
{
cout << "Could not Open " << szFileName << endl;
return false;
}
unsigned int magic = 0;
unsigned char temp;
unsigned int numOfLabels = 0;
for(int i = 0;i < 4;i++)
{
if(feof(fp))
{
fclose(fp);
return false;
}
fread(&temp,sizeof(char),1,fp);
magic = magic << 8 | temp;
}
for(int i = 0;i < 4;i++)
{
if(feof(fp))
{
fclose(fp);
return false;
}
fread(&temp,sizeof(char),1,fp);
numOfLabels = numOfLabels << 8 | temp;
}
//printf("numOfLabels: %d\n",numOfLabels);
labels.resize(numOfLabels,1);
for(int i = 0;i < (int)numOfLabels;i++)
{
if(feof(fp))
{
fclose(fp);
return false;
}
fread(&temp,sizeof(char),1,fp);
labels(i,0) = temp;
}
fclose(fp);
return true;
}
void buildImage(MatrixXd &theta,int imgWidth,char* szFileName,bool showFlag = false,double ratio = 1)
{
int margin = 1;
int rows = theta.rows();
int cols = theta.cols();
if(rows <= 0 || cols <= 0)
{
return ;
}
double pr = sqrt((double)rows);
int perRow = (int)pr + (pr - (int)pr > 0);
double tc = (double)rows / (double)perRow;
int tCols = (int)tc + (tc - (int)tc > 0);
/*cout << "perRow: " << perRow << endl;
cout << "tCols: " << tCols << endl;*/
MatrixXd max = theta.rowwise().maxCoeff();
MatrixXd min = theta.rowwise().minCoeff();
int imgHeight = cols/imgWidth;
IplImage* iplImage = cvCreateImage(
cvSize(imgWidth * perRow + margin * (perRow+1),imgHeight * tCols + margin * (tCols + 1)),
IPL_DEPTH_8U,1);
int step = iplImage->widthStep;
uchar *data = (uchar *)iplImage->imageData;
int h = iplImage->height;
int w = iplImage->width;
for(int x = 0; x < w; x++)
{
for(int y = 0; y < h; y++)
{
data[y * step + x] = 0;
}
}
for(int i = 0;i < rows;i++)
{
int n = 0;
int hIdx = i / perRow;
int wIdx = i % perRow;
for(int j = 0;j < imgHeight;j++)
{
for(int k = 0;k < imgWidth; k++)
{
double per = (theta(i,n) - min(i,0) ) / (max(i,0) - min(i,0));
//data[j * step + k] = 255;
int val = (hIdx * imgHeight+j + margin * (hIdx+1)) * step
+ (wIdx * imgWidth + k + margin * (wIdx+1));
if(val > step * (imgHeight * tCols + margin * (tCols + 1)))
{
cout << "error" << endl;
}
data[val] = (uchar)(int)(per * 230.0);
n ++;
}
}
}
cvSaveImage(szFileName,iplImage);
if(showFlag)
{
cvNamedWindow(szFileName,CV_WINDOW_AUTOSIZE);
IplImage* iplImageShow = cvCreateImage(
cvSize((int)(iplImage->width * ratio),(int)(iplImage->height * ratio)),IPL_DEPTH_8U,1);
cvResize(iplImage,iplImageShow,CV_INTER_CUBIC);
cvShowImage(szFileName,iplImageShow);
cvWaitKey(100000);
cvDestroyWindow(szFileName);
cvReleaseImage(&iplImageShow);
}
cvReleaseImage(&iplImage);
}
bool saveMatrix(MatrixXd &m,char *szFileName)
{
ofstream ofs(szFileName);
if(!ofs)
{
return false;
}
ofs << m.rows() << " " << m.cols() << endl;
for(int i = 0; i < m.rows(); i++)
{
for(int j = 0; j < m.cols(); j++)
{
ofs << m(i,j) << " ";
}
}
ofs.close();
return true;
}
bool LoadMatrix(MatrixXd &m,char *szFileName)
{
ifstream ifs(szFileName);
if(!ifs)
{
return false;
}
int rows;
int cols;
ifs >> rows >> cols;
m.resize(rows,cols);
for(int i = 0; i < m.rows(); i++)
{
for(int j = 0; j < m.cols(); j++)
{
if(ifs.eof())
{
return false;
}
ifs >> m(i,j);
}
}
ifs.close();
return true;
}
//data dim * numOfExamples
bool loadDataSet(MatrixXd &data,char *szFileName)
{
ifstream ifs(szFileName);
if(!ifs)
{
return false;
}
cout << "Loading data..." << endl;
int inputLayerSize;
ifs >> inputLayerSize;
int dataSetSize;
ifs >> dataSetSize;
data.resize(dataSetSize,inputLayerSize);
for(int i = 0; i < dataSetSize; i++)
{
for(int j = 0; j < inputLayerSize; j++)
{
ifs >> data(i,j);
}
}
ifs.close();
return true;
}
void showImage(MatrixXd &theta,int imgWidth,char* szWindowName,double ratio = 1)
{
int margin = 1;
int rows = theta.rows();
int cols = theta.cols();
if(rows <= 0 || cols <= 0)
{
return ;
}
double pr = sqrt((double)rows);
int perRow = (int)pr + (pr - (int)pr > 0);
double tc = (double)rows / (double)perRow;
int tCols = (int)tc + (tc - (int)tc > 0);
/*cout << "perRow: " << perRow << endl;
cout << "tCols: " << tCols << endl;*/
MatrixXd max = theta.rowwise().maxCoeff();
MatrixXd min = theta.rowwise().minCoeff();
int imgHeight = cols/imgWidth;
IplImage* iplImage = cvCreateImage(
cvSize(imgWidth * perRow + margin * (perRow+1),imgHeight * tCols + margin * (tCols + 1)),
IPL_DEPTH_8U,1);
int step = iplImage->widthStep;
uchar *data = (uchar *)iplImage->imageData;
int h = iplImage->height;
int w = iplImage->width;
for(int x = 0; x < w; x++)
{
for(int y = 0; y < h; y++)
{
data[y * step + x] = 0;
}
}
for(int i = 0;i < rows;i++)
{
int n = 0;
int hIdx = i / perRow;
int wIdx = i % perRow;
for(int j = 0;j < imgHeight;j++)
{
for(int k = 0;k < imgWidth; k++)
{
double per = (theta(i,n) - min(i,0) ) / (max(i,0) - min(i,0));
//data[j * step + k] = 255;
int val = (hIdx * imgHeight+j + margin * (hIdx+1)) * step
+ (wIdx * imgWidth + k + margin * (wIdx+1));
if(val > step * (imgHeight * tCols + margin * (tCols + 1)))
{
cout << "error" << endl;
}
data[val] = (uchar)(int)(per * 230.0);
n ++;
}
}
}
IplImage* iplImageShow = cvCreateImage(
cvSize((int)(iplImage->width * ratio),(int)(iplImage->height * ratio)),IPL_DEPTH_8U,1);
cvResize(iplImage,iplImageShow,CV_INTER_CUBIC);
cvShowImage(szWindowName,iplImageShow);
cvWaitKey(3);
cvReleaseImage(&iplImageShow);
cvReleaseImage(&iplImage);
}