-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.C
344 lines (284 loc) · 6.78 KB
/
Utils.C
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
#include "Utils.H"
#include "_cxcore.h"
int ShowImage(const char* zFile, const char* zWnd)
{
IplImage* pImg = cvLoadImage(zFile);
int iRet = ShowImage(pImg, zWnd);
cvReleaseImage(&pImg);
return iRet;
}
int ShowImage(IplImage* pImg, const char* zWnd)
{
cvNamedWindow(zWnd, 0);
cvShowImage(zWnd, pImg);
int iRet = cvWaitKey(); // after a key pressed, release data
cvDestroyWindow(zWnd);
return iRet;
}
/*
* safely assume zBuf is 0 terminated
* zOut has at least zBuf
*/
void RemoveBlanks(char* zBuf, char* zOut)
{
int iLen = 0;
char* zStart, *zEnd;
zStart = zBuf;
while(*zStart == ' ' || *zStart == '\t' )
{
zStart++;
}
zEnd = zBuf+strlen(zBuf)-1;
while(*zEnd == ' ' || *zEnd == '\t' )
{
zEnd--;
}
iLen = zEnd - zStart + 1;
strncpy(zOut, zStart, iLen);
zOut[iLen] = 0;
}
void NormalizeImg(IplImage* pSrc, IplImage* pDest, float _fMin, float _fMax)
{
float p=0.0, q=0.0, fMin = 1000000.0 , fMax = 0.0;
for(int y=0; y<pSrc->height; y++)
{
for(int x=0;x<pSrc->width; x++)
{
p = *((float*)(pSrc->imageData + pSrc->widthStep*y) + x);
if(p <= fMin) fMin = p;
if(p >= fMax) fMax = p;
}
}
// y = mx + c
float m,c;
m = (_fMax - _fMin)/(fMax - fMin);
c = _fMin - (m*fMin);
for(int y=0; y<pDest->height; y++)
{
for(int x=0; x<pDest->width; x++)
{
p = *((float *)(pSrc->imageData + pSrc->widthStep*y) + x);
q = m*p + c;
((uchar *)(pDest->imageData + pDest->widthStep*y))[x] = (uchar)nearbyintf(q);
}
}
}
void PrintMat(CvMat* pMat, const char* zFile)
{
if (pMat == NULL) return;
int iType = cvGetElemType(pMat);
if ( IS_MUL_CHANNEL(iType))
{
/* no intension to use multi channel images !! */
LOG(1, "Dont print multi channel mat %d\n", iType);
return;
}
FILE *fp = fopen(zFile, "w");
if (fp == NULL)
{
LOG(1, "Error opening %s\n", zFile);
return;
}
int iRows, iCols;
iRows = pMat->rows;
iCols = pMat->cols;
unsigned char* c;
short* s;
int* i;
float *f;
double *d;
for (int k=0; k<iRows; k++)
{
for (int j=0; j<iCols; j++)
{
/* ugly ??*/
if ( iType == CV_8U || iType == CV_8S )
{
c = pMat->data.ptr;
fprintf(fp, "%d, ", *(c + k*iCols + j));
}
else if (iType == CV_16U || iType == CV_16S )
{
s = pMat->data.s;
fprintf(fp, "%d, ", *(s + k*iCols + j));
}
else if ( iType == CV_32S )
{
i = pMat->data.i;
fprintf(fp, "%d, ", *(i + k*iCols + j));
}
else if ( iType == CV_32F )
{
f = pMat->data.fl;
fprintf(fp, "%f, ", *(f + k*iCols + j));
}
else if ( iType == CV_64F )
{
d = pMat->data.db;
fprintf(fp, "%f, ", *(d + k*iCols + j));
}
else
{
LOG(1, "Unknown mat type in PrintMat() %d\n", iType);
return;
}
}
fprintf(fp, "\n");
}
fclose(fp);
}
//
// Convert Image to Mat
//
CvMat* ConvertToMat(IplImage* pImg, int iMode)
{
if (pImg->nChannels != 1)
{
LOG(1, "Image channels=%d, not converting to mat\n", pImg->nChannels);
return NULL;
}
CvMat *pMat = NULL;
int iType = GetType(pImg);
int iHeight, iWidth, iWidthStep;
if (iMode == CONV_MODE_ROW)
{
iHeight = 1;
iWidth = pImg->height * pImg->width;
iWidthStep = pImg->widthStep * pImg->height;
}
else if ( iMode == CONV_MODE_COL)
{
iHeight = pImg->height * pImg->width;
iWidth = 1;
iWidthStep = pImg->widthStep * pImg->height;
}
else
{
iHeight = pImg->height;
iWidth = pImg->width;
iWidthStep = pImg->widthStep;
}
pMat = cvCreateMatHeader(iHeight, iWidth, iType);
cvSetData(pMat, pImg->imageData, iWidthStep);
return pMat;
}
//CvMat* GetMat(IplImage* pImg, int iMode)
//{
// if (pImg->nChannels != 1)
// {
// LOG(1, "Image channels=%d, not converting to mat\n", pImg->nChannels);
// return NULL;
// }
//
//
// //if (iMode == CONV_MODE_ROW)
// // iRows = 1;
// //else if ( iMode == CONV_MODE_COL)
// // iRows = pImg->height * pImg->width;
// //else
// // iRows = 0;
//
// //cvReshape(&oMat, pMat, 0, iRows);
// PrintMat(&oMat, "./temp.mat");
// return pMat;
//}
int GetType(IplImage* pImg)
{
int iDepth = icvIplToCvDepth( pImg->depth );
return CV_MAKETYPE( iDepth, pImg->nChannels );
}
// fill iRowNum of pDest with pSrc
// mats should match of course
void FillMat(CvMat* pDstMat, int iRowNum, CvMat* pSrcMat)
{
unsigned char *pSrc, *pDst;
float *pPos;
int iSrcStep, iDstStep;
CvSize oSrcSize, oDstSize;
cvGetRawData(pSrcMat, &pSrc, &iSrcStep, &oSrcSize );
cvGetRawData(pDstMat, &pDst, &iDstStep, &oDstSize );
pPos = (float*)(pDst + iDstStep*iRowNum);
for( int y=0; y<oSrcSize.height; y++ )
{
for( int x=0; x<oSrcSize.width; x++ )
{
*pPos = (float)(pSrc + y*iSrcStep)[x];
pPos++;
}
}
}
void System(char* zFormat, ...)
{
char zBuf[1000];
va_list ap;
va_start(ap, zFormat);
vsnprintf(zBuf, 1000, zFormat, ap);
va_end(ap);
LOG(1, "Executing \"%s\"\n", zBuf);
system(zBuf);
}
int WritePlot(float** ppArray , int iCount, int iDim, const char* zDataFile, const char* zPlotFile)
{
FILE *pData = fopen(zDataFile, "w");
if (!pData)
{
LOG(1, "fopen() failed on %s\n", zDataFile);
return 0;
}
FILE *pPlot = fopen(zPlotFile, "w");
if (!pPlot)
{
LOG(1, "fopen() failed on %s\n", zPlotFile);
fclose(pData);
return 0;
}
fprintf(pPlot, "plot \\\n");
bool bPlot = false;
for (int i=0; i<iDim; i++)
{
for (int j=0; j<iCount; j++)
{
if (!bPlot)
{
if (j>0) fprintf(pPlot, ", \\\n");
fprintf(pPlot, "\"%s\" using 0:%d with linespoints", zDataFile, j+1);
}
float f = *(*(ppArray+j) + i);
fprintf(pData, "%f\t", f);
}
bPlot = true;
fprintf(pData, "\n");
}
fclose(pData);
fclose(pPlot);
return 1;
}
double CalcMahalanobis(float* pArr1, float* pArr2, int iSize)
{
CvMat* pMat1 = cvCreateMatHeader(1, iSize, CV_32FC1);
cvSetData(pMat1, pArr1, sizeof(float)*iSize);
CvMat* pMat2 = cvCreateMatHeader(1, iSize, CV_32FC1);
cvSetData(pMat2, pArr2, sizeof(float)*iSize);
CvMat* pCovMat, *pInvCovMat, *pAvgMat;
CvMat** ppMat = new CvMat*[2];
ppMat[0] = pMat1;
ppMat[1] = pMat2;
pAvgMat = cvCreateMat(1, iSize, CV_32FC1);
pCovMat = cvCreateMat(iSize, iSize, CV_32FC1);
pInvCovMat = cvCreateMat(iSize, iSize, CV_32FC1);
cvCalcCovarMatrix((const CvArr**)ppMat, 2, pCovMat, pAvgMat, CV_COVAR_NORMAL);
cvInvert(pCovMat, pInvCovMat, CV_SVD);
double dDist = cvMahalonobis(pMat1, pMat2, pInvCovMat);
cvReleaseMat(&pMat1);
cvReleaseMat(&pMat2);
cvReleaseMat(&pAvgMat);
cvReleaseMat(&pAvgMat);
cvReleaseMat(&pCovMat);
cvReleaseMat(&pInvCovMat);
delete [] ppMat;
return dDist;
}
void ResetImage(IplImage* pImg, int iVal)
{
memset(pImg->imageData, iVal, pImg->imageSize);
}