forked from bushuhui/pi-slic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_slic.cpp
586 lines (427 loc) · 14.7 KB
/
test_slic.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdint.h>
#include <set>
#include <vector>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv2/legacy/legacy.hpp>
#ifdef ENABLE_GPU
#include <cuda_runtime_api.h>
#include <cuda.h>
#include <helper_functions.h> // helper for shared functions common to CUDA SDK samples
#include <helper_cuda.h> // helper functions for CUDA error checking and initialization
#include <helper_cuda_drvapi.h> // helper functions for drivers
#endif
#include <base/time/Time.h>
#include <base/debug/debug_config.h>
#include <base/utils/utils.h>
#include "src/vl/slic.h"
#include "src/gSLIC/FastImgSeg.h"
#include "src/PI_SLIC.h"
using namespace std;
using namespace pi;
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
int test_cslic(CParamArray *pa)
{
int testNum = svar.GetInt("testNum", 1);
int useGPU = svar.GetInt("useGPU", 0);
// load image
string imgFN = svar.GetString("image", "./data/test640.png");
cv::Mat img = cv::imread(imgFN);
int imgW, imgH, imgC, block;
imgW = img.cols;
imgH = img.rows;
imgC = img.channels();
block = imgW * imgH;
dbg_pt("input img W, H, C = %3d, %3d, %3d\n", imgW, imgH, imgC);
cv::Mat img2;
uint32_t *seg;
float *imgF;
if( 1 ) {
if( imgC == 3 )
cv::cvtColor(img, img2, CV_BGR2GRAY);
else
img2 = img;
// do SLIC segmentation
imgF = new float[imgW*imgH];
seg = new uint32_t[imgW*imgH];
uint8_t *pix = img2.data;
for(int i=0; i<imgW*imgH; i++) imgF[i] = pix[i];
} else {
if( imgC == 1 )
cv::cvtColor(img, img2, CV_GRAY2BGR);
else
img2 = img;
// do SLIC segmentation
imgF = new float[imgW*imgH*3];
seg = new uint32_t[imgW*imgH];
uint8_t *pix = img2.data;
for(int iy=0; iy<imgH; iy++) {
for(int ix=0; ix<imgW; ix++) {
for(int ic=0; ic<3; ic++) {
imgF[ic*imgW*imgH + iy*imgW + ix] = *pix;
pix++;
}
}
}
}
int regionSize = 10;
float regularization = 0.1;
int minRegionSize = 16;
regionSize = svar.GetInt("regionSize", 10);
regularization = svar.GetDouble("regularization", 0.1);
minRegionSize = svar.GetInt("minRegionSize", 16);
for(int i=0; i<testNum; i++) {
timer.enter("vl_slic_segment");
vl_slic_segment(seg, imgF,
imgW, imgH, 1,
regionSize, regularization, minRegionSize);
timer.leave("vl_slic_segment");
}
// draw segmentation results
uint32_t segIMax = 0;
set<uint32_t> segSet;
for(int i=0; i<imgW*imgH; i++) {
uint32_t si = seg[i];
if( si > segIMax ) segIMax = si;
set<uint32_t>::iterator it = segSet.find(si);
if( it == segSet.end() ) segSet.insert(si);
}
int segN = segSet.size();
printf("segNum = %d, segIMax = %d\n", segN, segIMax);
uint8_t *colorLUT = new uint8_t[segIMax*3];
for(int i=0; i<segIMax; i++) {
colorLUT[i*3 + 0] = rand() % 256;
colorLUT[i*3 + 1] = rand() % 256;
colorLUT[i*3 + 2] = rand() % 256;
}
cv::Mat imgSeg;
imgSeg.create(imgH, imgW, CV_8UC3);
uint8_t *pImg = imgSeg.data;
for(int i=0; i<imgW*imgH; i++) {
uint32_t si = seg[i];
pImg[i*3 + 0] = colorLUT[si*3 + 0];
pImg[i*3 + 1] = colorLUT[si*3 + 1];
pImg[i*3 + 2] = colorLUT[si*3 + 2];
}
string fnImgOut = fmt::sprintf("%s_out.png", imgFN);
cv::imwrite(fnImgOut, imgSeg);
delete [] imgF;
delete [] seg;
delete [] colorLUT;
return 0;
}
// Copy cvImage to memory buffer
void CvImgToBuffer(IplImage* frame, unsigned char* imgBuffer)
{
for (int i=0;i<frame->height;i++) {
for (int j=0;j<frame->width;j++) {
int bufIdx=(i*frame->width+j)*4;
imgBuffer[bufIdx]=CV_IMAGE_ELEM(frame,unsigned char,i,j*3);
imgBuffer[bufIdx+1]=CV_IMAGE_ELEM(frame,unsigned char,i,j*3+1);
imgBuffer[bufIdx+2]=CV_IMAGE_ELEM(frame,unsigned char,i,j*3+2);
}
}
}
// Copy memory buffer to cvImage
void OutputImgToCvImg(unsigned char* markedImg, IplImage* frame)
{
for (int i=0;i<frame->height;i++) {
for (int j=0;j<frame->width;j++) {
int bufIdx=(i*frame->width+j)*4;
CV_IMAGE_ELEM(frame,unsigned char,i,j*3)=markedImg[bufIdx];
CV_IMAGE_ELEM(frame,unsigned char,i,j*3+1)=markedImg[bufIdx+1];
CV_IMAGE_ELEM(frame,unsigned char,i,j*3+2)=markedImg[bufIdx+2];
}
}
}
CvSubdiv2D* InitSubdivision(CvMemStorage* storage, CvRect rect)
{
CvSubdiv2D* subdiv;
subdiv = cvCreateSubdiv2D( CV_SEQ_KIND_SUBDIV2D, sizeof(*subdiv), sizeof(CvSubdiv2DPoint), sizeof(CvQuadEdge2D), storage);
cvInitSubdivDelaunay2D(subdiv, rect);
return subdiv;
}
void LocatePoint(CvSubdiv2D* subdiv, CvPoint2D32f fp)
{
CvSubdiv2DEdge e;
CvSubdiv2DEdge e0 = 0;
CvSubdiv2DPoint* p = 0;
cvSubdiv2DLocate(subdiv, fp, &e0, &p);
if(e0) {
e = e0;
do {
e = cvSubdiv2DGetEdge(e, CV_NEXT_AROUND_LEFT);
}
while( e != e0 );
}
}
CvSeq* ExtractSeq(CvSubdiv2DEdge edge, CvMemStorage* storage)
{
CvSeq* pSeq = NULL;
CvSubdiv2DEdge egTemp = edge;
int i, nCount = 0;
CvPoint* buf = 0;
// count number of edges in facet
do {
nCount ++;
egTemp = cvSubdiv2DGetEdge(egTemp, CV_NEXT_AROUND_LEFT );
}
while(egTemp != edge);
buf = (CvPoint*)malloc(nCount * sizeof(buf[0]));
// gather points
egTemp = edge;
for( i = 0; i < nCount; i++ ) {
CvSubdiv2DPoint* pt = cvSubdiv2DEdgeOrg(egTemp);
if(!pt)
break;
CvPoint ptInsert = cvPoint( cvRound(pt->pt.x), cvRound(pt->pt.y));
buf[i] = ptInsert;
egTemp = cvSubdiv2DGetEdge(egTemp, CV_NEXT_AROUND_LEFT );
}
if(i == nCount) {
pSeq = cvCreateSeq(CV_SEQ_KIND_GENERIC|CV_32SC2, sizeof(CvContour), sizeof(CvPoint), storage);
for( i = 0; i < nCount; i ++)
cvSeqPush(pSeq, &buf[i]);
}
free( buf );
return pSeq;
}
void DrawVoronoiDiagram(IplImage* image, SLICClusterCenter* centerList, int listSize)
{
CvRect cvRect = {0, 0, image->width, image->height};
CvMemStorage* pStorage = cvCreateMemStorage(0);
// Init Subdivision
CvSubdiv2D* pSubDiv = InitSubdivision(pStorage, cvRect);
// Add sites
for(int i = 0; i < listSize; i ++) {
SLICClusterCenter center = centerList[i];
float x = center.xy.x;
float y = center.xy.y;
if(0 <= x && x < image->width && 0 <= y && y < image->height) {
CvPoint2D32f fPoint = cvPoint2D32f(x, y);
LocatePoint(pSubDiv, fPoint);
cvSubdivDelaunay2DInsert(pSubDiv, fPoint);
}
}
// Calculate voronoi tessellation
cvCalcSubdivVoronoi2D(pSubDiv);
// Draw edges
int nEdgeCount = pSubDiv->edges->total;
int nElementSize = pSubDiv->edges->elem_size;
CvPoint** ppPoints = new CvPoint*[1];
ppPoints[0] = new CvPoint[2048];
int pnPointCount[1];
CvSeqReader reader;
cvStartReadSeq( (CvSeq*)(pSubDiv->edges), &reader, 0);
for(int i = 0; i < nEdgeCount; i++) {
CvQuadEdge2D* edge = (CvQuadEdge2D*)(reader.ptr);
if( CV_IS_SET_ELEM( edge )) {
CvSubdiv2DEdge e = (CvSubdiv2DEdge)edge;
CvSeq *pSeq = ExtractSeq(cvSubdiv2DRotateEdge( e, 1 ), pStorage);
if(pSeq != NULL) {
pnPointCount[0] = pSeq->total;
for(int j = 0; j < pSeq->total; j ++) {
CvPoint pt = *CV_GET_SEQ_ELEM(CvPoint, pSeq, j);
ppPoints[0][j] = cvPoint(pt.x, pt.y);
}
cvPolyLine(image, ppPoints, pnPointCount, 1, -1, CV_RGB(0, 0, 255));
}
}
CV_NEXT_SEQ_ELEM( nElementSize, reader);
}
delete [] ppPoints[0];
delete [] ppPoints;
cvReleaseMemStorage(&pStorage);
}
int test_gslic_1(pi::CParamArray *pa)
{
IplImage* frame = cvLoadImage("./data/photo.png");
FastImgSeg* mySeg = new FastImgSeg();
mySeg->initializeFastSeg(frame->width,frame->height, 2000);
unsigned char* imgBuffer=(unsigned char*)malloc(frame->width * frame->height * sizeof(unsigned char) * 4);
CvImgToBuffer(frame, imgBuffer);
mySeg->LoadImg(imgBuffer);
cvNamedWindow("frame",0);
float weight = 5.1f;
mySeg->DoSegmentation(SLIC, weight);
mySeg->Tool_GetFilledImg();
//mySeg->Tool_GetMarkedImg();
//mySeg->Tool_DrawSites();
OutputImgToCvImg(mySeg->markedImg, frame);
cvShowImage("frame",frame);
//mySeg->centerList
DrawVoronoiDiagram(frame, mySeg->centerList, mySeg->nMaxSegs);
cvShowImage("frame",frame);
cvWaitKey(0);
cvSaveImage("photo_segmented.png", frame);
cvDestroyWindow( "frame" );
cvFree(&frame);
delete mySeg;
return 0;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
int test_gslic(CParamArray *pa)
{
int testNum = svar.GetInt("testNum", 1);
int useGPU = svar.GetInt("useGPU", 0);
// load image
string imgFN = svar.GetString("image", "./data/test640.png");
cv::Mat img = cv::imread(imgFN);
int imgW, imgH, imgC, block;
imgW = img.cols;
imgH = img.rows;
imgC = img.channels();
block = imgW * imgH;
dbg_pt("input img W, H, C = %3d, %3d, %3d\n", imgW, imgH, imgC);
cv::Mat img2;
int *seg;
uint8_t *imgBuf;
{
if( imgC == 1 )
cv::cvtColor(img, img2, CV_GRAY2BGR);
else
img2 = img;
// convert image
imgBuf = new uint8_t[imgW*imgH*4];
uint8_t *pix = img2.data;
for(int iy=0; iy<imgH; iy++) {
for(int ix=0; ix<imgW; ix++) {
imgBuf[(iy*imgW + ix)*4 + 0] = *(pix++);
imgBuf[(iy*imgW + ix)*4 + 1] = *(pix++);
imgBuf[(iy*imgW + ix)*4 + 2] = *(pix++);
}
}
}
int segNum = 2000;
float weight = 0.1;
segNum = svar.GetInt("segNum", 2000);
weight = svar.GetDouble("weight", 0.1);
FastImgSeg* mySeg = new FastImgSeg();
mySeg->initializeFastSeg(imgW, imgH, segNum);
mySeg->LoadImg(imgBuf);
for(int i=0; i<testNum; i++) {
timer.enter("gSLIC");
mySeg->DoSegmentation(SLIC, weight);
timer.leave("gSLIC");
}
// draw segmentation results
seg = mySeg->segMask;
int segIMax = 0;
set<int> segSet;
for(int i=0; i<imgW*imgH; i++) {
int si = seg[i];
if( si > segIMax ) segIMax = si;
set<int>::iterator it = segSet.find(si);
if( it == segSet.end() ) segSet.insert(si);
}
int segN = segSet.size();
printf("segNum = %d, segIMax = %d\n", segN, segIMax);
uint8_t *colorLUT = new uint8_t[segIMax*3];
for(int i=0; i<segIMax; i++) {
colorLUT[i*3 + 0] = rand() % 256;
colorLUT[i*3 + 1] = rand() % 256;
colorLUT[i*3 + 2] = rand() % 256;
}
cv::Mat imgSeg;
imgSeg.create(imgH, imgW, CV_8UC3);
uint8_t *pImg = imgSeg.data;
for(int i=0; i<imgW*imgH; i++) {
int si = seg[i];
pImg[i*3 + 0] = colorLUT[si*3 + 0];
pImg[i*3 + 1] = colorLUT[si*3 + 1];
pImg[i*3 + 2] = colorLUT[si*3 + 2];
}
string fnImgOut = fmt::sprintf("%s_out.png", imgFN);
cv::imwrite(fnImgOut, imgSeg);
delete [] imgBuf;
delete [] colorLUT;
return 0;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
int test_slic(CParamArray *pa)
{
int testNum = svar.GetInt("testNum", 1);
int useGPU = svar.GetInt("useGPU", 0);
// load image
string imgFN = svar.GetString("image", "./data/photo.png");
cv::Mat img = cv::imread(imgFN);
int imgW, imgH, imgC, block;
imgW = img.cols;
imgH = img.rows;
imgC = img.channels();
block = imgW * imgH;
dbg_pt("input img: %s", imgFN.c_str());
dbg_pt("input img W, H, C = %3d, %3d, %3d\n", imgW, imgH, imgC);
// load parameters
int regionSize = svar.GetInt("regionSize", 16);
float regularization = svar.GetDouble("regularization", 0.1);
int minRegionSize = svar.GetInt("minRegionSize", 16);
// do segmentation
PI_SLIC slic;
slic.setMode(imgW, imgH, useGPU, regionSize, regularization, minRegionSize);
int32_t *segBuf = new int32_t[imgW*imgH];
for(int i=0; i<testNum; i++) {
timer.enter("PI_SLIC");
slic.doSegment(img, segBuf);
timer.leave("PI_SLIC");
}
// draw segmentation results
int segIMax = 0;
set<int32_t> segSet;
for(int i=0; i<imgW*imgH; i++) {
int32_t si = segBuf[i];
if( si > segIMax ) segIMax = si;
set<int32_t>::iterator it = segSet.find(si);
if( it == segSet.end() ) segSet.insert(si);
}
int segN = segSet.size();
dbg_pt("segNum = %d, segIMax = %d\n", segN, segIMax);
uint8_t *colorLUT = new uint8_t[segIMax*3];
for(int i=0; i<segIMax; i++) {
colorLUT[i*3 + 0] = rand() % 256;
colorLUT[i*3 + 1] = rand() % 256;
colorLUT[i*3 + 2] = rand() % 256;
}
cv::Mat imgSeg;
imgSeg.create(imgH, imgW, CV_8UC3);
uint8_t *pImg = imgSeg.data;
for(int i=0; i<imgW*imgH; i++) {
int si = segBuf[i];
pImg[i*3 + 0] = colorLUT[si*3 + 0];
pImg[i*3 + 1] = colorLUT[si*3 + 1];
pImg[i*3 + 2] = colorLUT[si*3 + 2];
}
string fnImgOut = fmt::sprintf("%s_out_%d.png", imgFN, useGPU);
cv::imwrite(fnImgOut, imgSeg);
delete [] segBuf;
delete [] colorLUT;
return 0;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
struct TestFunctionArray g_fa[] =
{
TEST_FUNC_DEF(test_cslic, "Test SLIC segmentation (CPU)"),
TEST_FUNC_DEF(test_gslic_1, "Test gSLIC segmentation (demo 1)"),
TEST_FUNC_DEF(test_gslic, "Test gSLIC segmentation"),
TEST_FUNC_DEF(test_slic, "Test SLIC segmentation"),
{NULL, "NULL", "NULL"},
};
int main(int argc, char *argv[])
{
// setup debug trace
dbg_stacktrace_setup();
// run function
return svar_main(argc, argv, g_fa);
}