-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogo_util.cpp
More file actions
646 lines (550 loc) · 16.1 KB
/
Logo_util.cpp
File metadata and controls
646 lines (550 loc) · 16.1 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
#include "stdafx.h"
#include "Logo_base.h"
#include "Logo_util.h"
#include "Logo_log.h"
namespace LogRec
{
/*
*/
bool GetTriCode(std::vector<LogRec::LogoPoint> &vlp, LogRec::LogoTriangle &tris)
{
bool bp1 = false;
bool bp2 = false;
bool bp3 = false;
for (std::vector<LogRec::LogoPoint>::iterator itr = vlp.begin(); itr != vlp.end(); itr++)
{
if (*itr == tris.m_A)
{
tris.m_A.m_uVisualWord = itr->m_uVisualWord;
bp1 = true;
}
if (*itr == tris.m_B)
{
tris.m_B.m_uVisualWord = itr->m_uVisualWord;
bp2 = true;
}
if (*itr == tris.m_C)
{
tris.m_C.m_uVisualWord = itr->m_uVisualWord;
bp3 = true;
}
if (bp1 && bp2 && bp3)
{
return true;
}
}
return false;
}
//point wrapper
LogoSLRPoint logoSLRPoint(CvSURFPoint &surfp,unsigned short visualWord)
{
LogoSLRPoint slrp;
slrp = surfp;
slrp.m_uVisualWord = visualWord;
return slrp;
}
LogoSLRPoint logoSLRPoint(KeyPoint &keypt,unsigned short visualWord)
{
LogoSLRPoint slrp;
slrp = keypt;
slrp.m_uVisualWord = visualWord;
return slrp;
}
CvSURFPoint KeyPointToSurfPoint(KeyPoint &keypt)
{
CvSURFPoint cvSurfpt;
cvSurfpt.pt.x = keypt.pt.x;
cvSurfpt.pt.y = keypt.pt.y;
cvSurfpt.dir = keypt.angle;
cvSurfpt.size = keypt.size;
cvSurfpt.hessian = 0;
cvSurfpt.laplacian = 0;
return cvSurfpt;
}
bool IsAlreadyInTriangles(std::vector<LogRec::LogoTriangle> &vLtri, LogRec::LogoTriangle &tris)
{
for (int i = 0; i < vLtri.size (); i ++)
{
if (vLtri[i] == tris)
{
return true;
}
}
return false;
}
bool TriangleIsInRect(CvRect &rect,CvPoint *tri)
{
for (int i = 0; i < 3; i++)
{
if ((tri[i].x < 0) || (tri[i].x > rect.width) || (tri[i].y < 0) || (tri[i].y > rect.height))
{
return false;
}
}
return true;
}
void LogoReadTrainFileList(std::string filePath, std::string fileName, std::vector<LogRec::LogoTrainFileListFormat> &vTrainList)
{
ifstream fin((filePath+fileName).c_str());//("flickr_logos_27_dataset_training_set_annotation.txt");
if (!fin)
{
cout << "file open error!" << endl;//use log in the future!
return;
}
string line;
string lastLine;
while (getline(fin,line))
{
LogRec::LogoTrainFileListFormat ltf;
istringstream eleExtractor;
eleExtractor.str(line.c_str());
eleExtractor >> ltf.m_sFileName;//
eleExtractor >> ltf.m_sClasName;
eleExtractor >> ltf.m_iSubClassType;
eleExtractor >> ltf.m_rBoundBox.m_iLeft;
eleExtractor >> ltf.m_rBoundBox.m_iTop;
eleExtractor >> ltf.m_rBoundBox.m_iRight;
eleExtractor >> ltf.m_rBoundBox.m_iBottom;
vTrainList.push_back(ltf);
}
}
bool LogoLoadCodeBook(std::string filePath, std::string fileName, std::vector<LogRec::LogoCodeBookFormat> &vCodeBook)
{//读取visual words vocabulary
ifstream fin((filePath+fileName).c_str());//("flickr_logos_27_dataset_training_set_annotation.txt");
if (!fin)
{
cout << "file open error!" << __FILE__ << __LINE__ << endl;//use log in the future!
return false;
}
string line;
int iSig = 1;
while (getline(fin,line))
{
LogRec::LogoCodeBookFormat lcb;
istringstream eleExtractor;
eleExtractor.str(line.c_str());
for(int i = 0; i < 128; i++)
{
eleExtractor >> lcb.m_fDescriptor[i];
}
lcb.m_iSig = iSig;
iSig++;
vCodeBook.push_back(lcb);
}
return true;
}
bool LogoEdgeIsRepeated(std::vector<unsigned int> &edges, unsigned int e)
{
for (std::vector<unsigned int>::iterator first = edges.begin(); first != edges.end(); first++)
{
if (*first == e)
{
return false;
}
}
return true;
}
bool LogoVectorIsNotInFormerTriangles(const vector<vector<int> >& one_tri,
const vector<vector<vector<int> > > &tris)
{
set<vector<int> > tTriangle;
set<vector<int> > sTriangle;
for (int i = 0; i < tris.size (); i ++)
{
tTriangle.clear();
sTriangle.clear();
for (int j = 0; j < 3; j++ )
{
tTriangle.insert(tris[i][j]);
sTriangle.insert(one_tri[j]);
}
if (tTriangle == sTriangle)
{
return false;
}
}
return true;
}
void Logo_draw_subdiv_facet(IplImage* img,CvSubdiv2DEdge edge)
{
CvSubdiv2DEdge t = edge;
int i, count = 0;
CvPoint * buf = 0;
do
{
count++;
t = cvSubdiv2DGetEdge(t,CV_NEXT_AROUND_LEFT);
}while ( t != edge );
buf = (CvPoint*)malloc(count * sizeof(buf[0]));
t = edge;
for (i = 0; i < count; i++)
{
CvSubdiv2DPoint* pt = cvSubdiv2DEdgeOrg( t );
if ( !pt )
{
break;
}
buf[i] = cvPoint( cvRound(pt->pt.x), cvRound(pt->pt.y) );
t = cvSubdiv2DGetEdge( t, CV_NEXT_AROUND_LEFT );
}
if ( i == count )
{
CvSubdiv2DPoint* pt = cvSubdiv2DEdgeDst( cvSubdiv2DRotateEdge(edge,1));
cvFillConvexPoly( img, buf, count, CV_RGB(255,255,255), CV_AA, 0 );
cvPolyLine( img, &buf, &count, 1, 1, CV_RGB(0,0,0), 1, CV_AA, 0);
//draw_subdiv_point( img, pt->pt, CV_RGB(0,0,0));
}
free( buf );
}
void Logo_paint_voronoi(CvSubdiv2D* subdiv, IplImage* img)
{
CvSeqReader reader;
int i, total = subdiv->edges->total;
int elem_size = subdiv->edges->elem_size;
cvCalcSubdivVoronoi2D( subdiv );
cvStartReadSeq( (CvSeq*)(subdiv->edges), &reader, 0 );
for ( i = 0; i < total; i++)
{
CvQuadEdge2D* edge = (CvQuadEdge2D*)(reader.ptr);
if ( CV_IS_SET_ELEM( edge ))
{
CvSubdiv2DEdge e = (CvSubdiv2DEdge)edge;
Logo_draw_subdiv_facet( img, cvSubdiv2DRotateEdge( e, 0 ) );
//draw_subdiv_facet( img, cvSubdiv2DRotateEdge( e, 3 ) );
}
CV_NEXT_SEQ_ELEM( elem_size, reader );
}
}
void Logo_draw_subdiv_point( IplImage* img, CvPoint2D32f fp, CvScalar color )
{
cvCircle( img, cvPoint(cvRound(fp.x), cvRound(fp.y)), 3, color, CV_FILLED, 8, 0 );
}
int Logo_get_visual_vocabulary(std::string dstVocabFile, int thresh_size, int k, std::string filePath, std::string fileList)
{
ifstream fin((filePath+fileList).c_str());
if (!fin)
{
cerr << "open file failed!" << endl;
return -1;
}
string path = filePath;
//read images from given path
string line;
//用来聚类求visual vocabulary 的矩阵
cv::Mat m_Features(thresh_size, 128, CV_32F);
int picNum = 0;
int i_totalF = 0;
float* p_imgPtr = m_Features.ptr<float>(0);
while (getline(fin,line))
{
istringstream filenameExtractor;
//string str;
filenameExtractor.str(line);
filenameExtractor >> line;// >> str;
string fullNamePath = path+line;
//根据路径抽取图片特征点
const char* image_filename = fullNamePath.c_str();
CvMemStorage* storage = cvCreateMemStorage(0);
IplImage* image = cvLoadImage( image_filename, CV_LOAD_IMAGE_GRAYSCALE );//CV_LOAD_IMAGE_GRAYSCALE
if(!image )
{
fprintf( stderr, "Can not load %s \n",image_filename );
continue;
//goto l1;
//exit(-1);
}
CvSeq *imageKeypoints = 0, *imageDescriptors = 0;
CvSURFParams params = cvSURFParams(500, 1);
cvExtractSURF( image, 0, &imageKeypoints, &imageDescriptors, storage, params );
int length = (int)(imageDescriptors->elem_size/sizeof(float));
//cv::Mat m_image(imageDescriptors->total, length, CV_32F);
//提取到矩阵里
CvSeqReader img_reader;
//float* img_ptr = m_image.ptr<float>(0);
cvStartReadSeq( imageDescriptors, &img_reader );
for(int i = 0; i < imageDescriptors->total; i++ )
{
const float* descriptor = (const float*)img_reader.ptr;
CV_NEXT_SEQ_ELEM( img_reader.seq->elem_size, img_reader );
memcpy(p_imgPtr, descriptor, length*sizeof(float));
p_imgPtr += length;
i_totalF++;
if (i_totalF > thresh_size)
{
break;
}
}
if (i_totalF > thresh_size)
{
break;
}
picNum++;
cout << "name:" << line << " itotalf:" << i_totalF << " picNum:" << picNum << endl;
cvReleaseImage(&image);
//add by 2011.12.14
cvReleaseMemStorage(&storage);
image = NULL;
}
cout << "toal features:" << i_totalF << endl;
BOWKMeansTrainer bowKms(k);
//bowKms.add(m_Features);
double tCount = (double)cvGetTickCount();
cout << "begin kmeans------------>>>>>>>>>>>>>>" << endl;
cv::Mat m_vocab = bowKms.cluster(m_Features);
cout << "kmeans over ---------->>>>>>>>>>>>" << endl;
tCount = (double)cvGetTickCount() - tCount;
ofstream ofin((filePath+dstVocabFile).c_str());
//float farr[2][3] = {{4.233,0.987,4.32},{0.44,0.666,0.34}};
printf("The Clustering result!\n");
for (int j = 0; j < m_vocab.rows; j++)
{
string sline;
for (int k = 0; k < m_vocab.cols; k++)
{
string tstr;
ostringstream ostr;
ostr << m_vocab.at<float>(j,k) << " ";
sline += ostr.str();
//istringstream istr(ostr.str());
//istr >> tstr;
}
ofin << sline << endl;
//printf("\n");
}
ostringstream lline;
lline << "cost time:" << tCount;
ofin << lline.str() << endl;
}
int Logo_get_visual_vocabularyRWI(std::string dstVocabFile, int thresh_size, int k, std::string filePath, std::string fileList)
{
ifstream fin((filePath+fileList).c_str());
if (!fin)
{
//cerr << "open file failed!" << endl;
LogRec::LOG_LEVEL_ERROR;
LogRec::PRINT_LOG_MESSAGE("failed to open file!");
return -1;
}
string path = filePath;
//read images from given path
string line;
//用来聚类求visual vocabulary 的矩阵
cv::Mat m_Features(thresh_size, 128, CV_32F);
int picNum = 0;
int i_totalF = 0;
float* p_imgPtr = m_Features.ptr<float>(0);
LogRec::LOG_SET_OPTION(LogRec::LOG_OPTION_INFO);
while (getline(fin,line))
{
istringstream filenameExtractor;
string fileDir;
filenameExtractor.str(line.c_str());
filenameExtractor >> fileDir;// >> str;
string fileName;
filenameExtractor >> fileName;
string fullNamePath = path+fileDir+"\\"+fileName;
//根据路径抽取图片特征点
const char* image_filename = fullNamePath.c_str();
CvMemStorage* storage = cvCreateMemStorage(0);
IplImage* image = cvLoadImage( image_filename, CV_LOAD_IMAGE_GRAYSCALE );//CV_LOAD_IMAGE_GRAYSCALE
if(!image )
{
//fprintf( stderr, "Can not load %s \n",image_filename );
LogRec::LOG_LEVEL_WARN;
LogRec::PRINT_LOG_MESSAGE("failed to open fiel!");
continue;
//goto l1;
//exit(-1);
}
//cvExtractSIFT();
CvSeq *imageKeypoints = 0, *imageDescriptors = 0;
//cvGetMat()
CvSURFParams params = cvSURFParams(500, 0);
cvExtractSURF( image, 0, &imageKeypoints, &imageDescriptors, storage, params );
int length = (int)(imageDescriptors->elem_size/sizeof(float));
//cv::Mat m_image(imageDescriptors->total, length, CV_32F);
//提取到矩阵里
CvSeqReader img_reader;
//float* img_ptr = m_image.ptr<float>(0);
cvStartReadSeq( imageDescriptors, &img_reader );
for(int i = 0; i < imageDescriptors->total; i++ )
{
const float* descriptor = (const float*)img_reader.ptr;
CV_NEXT_SEQ_ELEM( img_reader.seq->elem_size, img_reader );
memcpy(p_imgPtr, descriptor, length*sizeof(float));
p_imgPtr += length;
i_totalF++;
if (i_totalF > thresh_size)
{
break;
}
}
if (i_totalF > thresh_size)
{
break;
}
picNum++;
LogRec::LOG_LEVEL_INFO;
LogRec::PRINT_LOG_MESSAGE("PIC_NAME: %s TOTAL_FEATURES: %d PIC_NUM: %d \n",
line.c_str(),i_totalF,picNum);
//cout << "name:" << line << " itotalf:" << i_totalF << " picNum:" << picNum << endl;
cvReleaseImage(&image);
////add by 2011.12.14
cvReleaseMemStorage(&storage);
image = NULL;
}
//cout << "toal features:" << i_totalF << endl;
LogRec::LOG_LEVEL_INFO;
LogRec::PRINT_LOG_MESSAGE("TOTAL_FEATURES: %d",i_totalF);
BOWKMeansTrainer bowKms(k);
//bowKms.add(m_Features);
double tCount = (double)cvGetTickCount();
LogRec::LOG_LEVEL_INFO;
LogRec::PRINT_LOG_MESSAGE("<-----------------------KMEANS BEGIN----------------------->");
//cout << "begin kmeans------------>>>>>>>>>>>>>>" << endl;
cv::Mat m_vocab = bowKms.cluster(m_Features);
LogRec::LOG_LEVEL_INFO;
LogRec::PRINT_LOG_MESSAGE("<-----------------------KMEANS OVER----------------------->");
tCount = (double)cvGetTickCount() - tCount;
ofstream ofin((filePath+dstVocabFile).c_str());
//float farr[2][3] = {{4.233,0.987,4.32},{0.44,0.666,0.34}};
//printf("The Clustering result!\n");
LogRec::LOG_LEVEL_INFO;
LogRec::PRINT_LOG_MESSAGE("<-----------------------CLUSTERING RESULT----------------------->");
for (int j = 0; j < m_vocab.rows; j++)
{
string sline;
for (int k = 0; k < m_vocab.cols; k++)
{
string tstr;
ostringstream ostr;
ostr << m_vocab.at<float>(j,k) << " ";
sline += ostr.str();
//istringstream istr(ostr.str());
//istr >> tstr;
}
ofin << sline << endl;
//printf("\n");
}
ostringstream lline;
lline << "cost time:" << tCount;
ofin << lline.str() << endl;
LogRec::LOG_SET_OPTION(LogRec::LOG_OPTION_ALL);
}
int Logo_get_visual_vocabularySIFT(std::string dstVocabFile, int thresh_size, int k, std::string filePath, std::string fileList)
{
ifstream fin((filePath+fileList).c_str());
if (!fin)
{
//cerr << "open file failed!" << endl;
LogRec::LOG_LEVEL_ERROR;
LogRec::PRINT_LOG_MESSAGE("--failed to open file!--");
return -1;
}
string path = filePath;
//read images from given path
string line;
//用来聚类求visual vocabulary 的矩阵
cv::Mat m_Features;
int length = 128;
m_Features.create(thresh_size, 128, CV_32FC1);
int picNum = 0;
int i_totalF = 0;
float* p_imgPtr = m_Features.ptr<float>(0);
LogRec::LOG_SET_OPTION(LogRec::LOG_OPTION_INFO);
while ( getline(fin,line) )
{
istringstream filenameExtractor;
string fileDir;
filenameExtractor.str(line.c_str());
filenameExtractor >> fileDir;// >> str;
string fileName;
filenameExtractor >> fileName;
string fullNamePath = path+fileDir+"\\"+fileName;
//根据路径抽取图片特征点
cv::Mat image = cv::imread(fullNamePath,CV_LOAD_IMAGE_COLOR);
if( !image.data )
{
LogRec::LOG_LEVEL_WARN;
LogRec::PRINT_LOG_MESSAGE("---failed to open fiel!---");
continue;
}
//#define _SURF_
#ifdef _SURF_
int hessian = 500;
SurfFeatureDetector detector( hessian );
SurfDescriptorExtractor extractor(4,2,1);
Mat descriptors;
LogRec::LOG_LEVEL_INFO;
LogRec::PRINT_LOG_MESSAGE("--Use Surf");
#else
SiftFeatureDetector detector;
SiftDescriptorExtractor extractor;
Mat descriptors;
LogRec::LOG_LEVEL_INFO;
LogRec::PRINT_LOG_MESSAGE("--Use Sift");
#endif
std::vector<cv::KeyPoint> keypoints;
detector.detect(image,keypoints);
extractor.compute(image,keypoints,descriptors);
for(int i = 0; i < descriptors.rows; i++ )
{
//const float* descriptor = (const float*)descriptors.ptr<float>(i);
const float* descriptor = (const float*)(descriptors.data + length*sizeof(float)*i);
//descriptors
memcpy(p_imgPtr, descriptor, length*sizeof(float));
p_imgPtr += length;
i_totalF++;
/**/if (i_totalF > thresh_size)
{
break;
}
}
if (i_totalF > thresh_size)
{
break;
}
picNum++;
LogRec::LOG_LEVEL_INFO;
LogRec::PRINT_LOG_MESSAGE("PIC_NAME: %s TOTAL_FEATURES: %d PIC_NUM: %d \n",
line.c_str(),i_totalF,picNum);
image = NULL;
}
LogRec::LOG_LEVEL_INFO;
LogRec::PRINT_LOG_MESSAGE("TOTAL_FEATURES: %d",i_totalF);
BOWKMeansTrainer bowKms(k);
//bowKms.add(m_Features);
double tCount = (double)cvGetTickCount();
LogRec::LOG_LEVEL_INFO;
LogRec::PRINT_LOG_MESSAGE("\n<-----------------------KMEANS BEGIN----------------------->\n");
//cout << "begin kmeans------------>>>>>>>>>>>>>>" << endl;
cv::Mat m_vocab = bowKms.cluster(m_Features);
LogRec::LOG_LEVEL_INFO;
LogRec::PRINT_LOG_MESSAGE("<-----------------------KMEANS OVER----------------------->");
tCount = (double)cvGetTickCount() - tCount;
ofstream ofin((filePath+dstVocabFile).c_str());
//float farr[2][3] = {{4.233,0.987,4.32},{0.44,0.666,0.34}};
//printf("The Clustering result!\n");
LogRec::LOG_LEVEL_INFO;
LogRec::PRINT_LOG_MESSAGE("<-----------------------CLUSTERING RESULT----------------------->");
for (int j = 0; j < m_vocab.rows; j++)
{
string sline;
for (int k = 0; k < m_vocab.cols; k++)
{
string tstr;
ostringstream ostr;
ostr << m_vocab.at<float>(j,k) << " ";
sline += ostr.str();
//istringstream istr(ostr.str());
//istr >> tstr;
}
ofin << sline << endl;
//printf("\n");
}
ostringstream lline;
lline << "cost time:" << tCount;
ofin << lline.str() << endl;
LogRec::LOG_SET_OPTION(LogRec::LOG_OPTION_ALL);
}
}