-
Notifications
You must be signed in to change notification settings - Fork 2
/
segment.cu
executable file
·1385 lines (1226 loc) · 41 KB
/
segment.cu
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Hierarchical Superpixel Segmentation by Parallel CRTrees Labeling.
* The step-complexity is O(log(N)) and the work-complexity is O(N) in average.
* The memory required is O(N).
* Author: Tingman Yan (tmyann@outlook.com)
*/
#include "segment.hpp"
#include <cfloat>
/*
* distance function for pixels
*/
__forceinline__ __device__ float
distL2(const float4& s, const float4& n)
{
float dist_3d = (s.x - n.x) * (s.x - n.x) + (s.y - n.y) * (s.y - n.y) +
(s.z - n.z) * (s.z - n.z);
float h_x = (s.x - n.x) * 1.f;
dist_3d += h_x > 0.f ? h_x : 0.f;
return dist_3d;
}
/*
* distance function for superpixels
* per-channel calculation, can be reduced by atomicAdd
*/
__forceinline__ __device__ float
distL2(const float& s, const float& n, const int& c)
{
float dist = (s - n) * (s - n);
if (c == 0) {
float h = (s - n) * 1.f;
dist += h > 0.f ? h : 0.f;
}
return dist;
}
/*
* help funciton
* find the nearest neighbor and coresponding distance for a pixel
*/
__forceinline__ __device__ void
find_minimum(int& minimum_id,
float& minimum,
const float& dist,
const int& index)
{
if (dist < minimum) {
minimum = dist;
minimum_id = index;
}
}
/*
* search the nearest neighbor in the image grid
* can initialize with 4-neighbor or 8-neighbor grids
* 8-neighbor gives higher BR, but worser UE performance
*/
__global__ void
compute_1nn_grid_k(const float4* const img_f4_d,
int* const nn_d,
int2* const bd_d,
float* const dist_d,
float* const dist_min_d,
const int num_nb,
const int width,
const int height)
{
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
int index = y * width + x;
float4 val = img_f4_d[index];
float minimum = FLT_MAX;
int minimum_id = 0;
if (x >= width || y >= height)
return;
// unroll the loop runs faster, although this is redundancy
if (y > 0) {
int index_u = index - width;
float4 val_u = img_f4_d[index_u];
float dist = distL2(val, val_u);
find_minimum(minimum_id, minimum, dist, index_u);
bd_d[num_nb * index].y = index_u;
if (dist_d)
dist_d[num_nb * index] = dist;
} else
bd_d[num_nb * index].y = index;
__syncthreads();
if (y < height - 1) {
int index_d = index + width;
float4 val_d = img_f4_d[index_d];
float dist = distL2(val, val_d);
find_minimum(minimum_id, minimum, dist, index_d);
bd_d[num_nb * index + 1].y = index_d;
if (dist_d)
dist_d[num_nb * index + 1] = dist;
} else
bd_d[num_nb * index + 1].y = index;
__syncthreads();
if (x > 0) {
int index_l = index - 1;
float4 val_l = img_f4_d[index_l];
float dist = distL2(val, val_l);
find_minimum(minimum_id, minimum, dist, index_l);
bd_d[num_nb * index + 2].y = index_l;
if (dist_d)
dist_d[num_nb * index + 2] = dist;
} else
bd_d[num_nb * index + 2].y = index;
__syncthreads();
if (x < width - 1) {
int index_r = index + 1;
float4 val_r = img_f4_d[index_r];
float dist = distL2(val, val_r);
find_minimum(minimum_id, minimum, dist, index_r);
bd_d[num_nb * index + 3].y = index_r;
if (dist_d)
dist_d[num_nb * index + 3] = dist;
} else
bd_d[num_nb * index + 3].y = index;
__syncthreads();
if (num_nb == 8) {
if (y > 0 && x > 0) {
int index_ul = index - width - 1;
float4 val_ul = img_f4_d[index_ul];
float dist = distL2(val, val_ul);
find_minimum(minimum_id, minimum, dist, index_ul);
bd_d[num_nb * index + 4].y = index_ul;
if (dist_d)
dist_d[num_nb * index + 4] = dist;
} else
bd_d[num_nb * index + 4].y = index;
__syncthreads();
if (y > 0 && x < width - 1) {
int index_ur = index - width + 1;
float4 val_ur = img_f4_d[index_ur];
float dist = distL2(val, val_ur);
find_minimum(minimum_id, minimum, dist, index_ur);
bd_d[num_nb * index + 5].y = index_ur;
if (dist_d)
dist_d[num_nb * index + 5] = dist;
} else
bd_d[num_nb * index + 5].y = index;
__syncthreads();
if (y < height - 1 && x < width - 1) {
int index_dr = index + width + 1;
float4 val_dr = img_f4_d[index_dr];
float dist = distL2(val, val_dr);
find_minimum(minimum_id, minimum, dist, index_dr);
bd_d[num_nb * index + 6].y = index_dr;
if (dist_d)
dist_d[num_nb * index + 6] = dist;
} else
bd_d[num_nb * index + 6].y = index;
__syncthreads();
if (y < height - 1 && x > 0) {
int index_dl = index + width - 1;
float4 val_dl = img_f4_d[index_dl];
float dist = distL2(val, val_dl);
find_minimum(minimum_id, minimum, dist, index_dl);
bd_d[num_nb * index + 7].y = index_dl;
if (dist_d)
dist_d[num_nb * index + 7] = dist;
} else
bd_d[num_nb * index + 7].y = index;
__syncthreads();
}
nn_d[index] = minimum_id;
dist_min_d[index] = minimum;
for (int i = 0; i < num_nb; ++i)
bd_d[num_nb * index + i].x = index;
}
/*
* used to initialize the label of image pixels
*/
__global__ void
set_to_identity(int* const i1_d, const int N)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index >= N)
return;
i1_d[index] = index;
}
/*
* simply draw the boundaries
*/
__global__ void
draw_boundary(uchar3* const seg_d,
const int* const clus_d,
const int width,
const int height)
{
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x >= width || y >= height)
return;
int index = y * width + x;
int clus_c = clus_d[index];
if (x > 0) { // for boundary point to left
int clus_left = clus_d[index - 1];
if (clus_left != clus_c)
seg_d[index] = make_uchar3(0, 0, 0);
}
if (y > 0) { // for boundary point to up
int clus_up = clus_d[index - width];
if (clus_up != clus_c)
seg_d[index] = make_uchar3(0, 0, 0);
}
/* if (bold)
if (x < width - 1) { // for boundary point to right
int clus_right = clus_d[index + 1];
if (clus_right != clus_c)
seg_d[index] = make_uchar3(0, 0, 0);
}
if (y < height - 1) { // for boundary point to down
int clus_down = clus_d[index + width];
if (clus_down != clus_c)
seg_d[index] = make_uchar3(0, 0, 0);
}
*/
}
/*
* visualize which segmentations are the cycle-roots of the CRTress
*/
__global__ void
draw_cycle_root(uchar3* const seg_d,
int* const mask_d,
const int width,
const int height)
{
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x >= width || y >= height)
return;
int index = y * width + x;
if (mask_d[index]) {
uchar3 c = seg_d[index];
seg_d[index] = make_uchar3(c.x, 0, 0);
}
}
/*
* draw segmentation labels in the uchar3 format
*/
__global__ void
draw_labels_uchar3(uchar3* const seg_d,
const int* const clus_d,
const int width,
const int height)
{
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x >= width || y >= height)
return;
int index = y * width + x;
int clus_c = clus_d[index] + 1; // make index start from 1
// code int to uchar3
uchar clus_x = (uchar)(clus_c & 0xFF);
uchar clus_y = (uchar)(clus_c >> 8 & 0xFF);
uchar clus_z = (uchar)(clus_c >> 16 & 0xFF);
// BGR order
seg_d[index] = make_uchar3(clus_x, clus_y, clus_z);
}
/*
* draw segmentation label in int format
*/
__global__ void
draw_labels_int(int* const seg_d,
const int* const clus_d,
const int width,
const int height)
{
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x >= width || y >= height)
return;
int index = y * width + x;
int clus_c = clus_d[index] + 1; // make index start from 1
seg_d[index] = clus_c;
}
/*
* after one iteration of clustering, map the index of boundaries to
* the new cluster indices
*/
__global__ void
map_boundary(const int* const clus_d, int2* const bd_d, const int num_bd)
{
int index = blockDim.x * blockIdx.x + threadIdx.x;
if (index >= num_bd)
return;
int2 bd = bd_d[index];
bd.x = clus_d[bd.x];
bd.y = clus_d[bd.y];
bd_d[index] = bd;
}
/*
* a predicate for removing duplicated boundaries
* boundaries with the same indices or be the same with the previous boundary
* are duplicated
*/
__global__ void
is_diff_bd(const int2* const bd_d, int* const predicate_d, const int num_bd)
{
int index = blockDim.x * blockIdx.x + threadIdx.x;
if (index >= num_bd)
return;
int2 bd = bd_d[index];
if ((index == num_bd - 1 || bd_d[index + 1] != bd) && (bd.x != bd.y)) {
predicate_d[index] = 1;
} else
predicate_d[index] = 0;
}
/*
* atomic reduce the distance associated with boundaries
*/
__global__ void
reduce_dist(const int2* const bd_d,
int* const pos_scan_d,
const float* const dist_d,
float* const dist_rd_d,
const int num_bd,
const Linkage link)
{
int index = blockDim.x * blockIdx.x + threadIdx.x;
if (index >= num_bd)
return;
int2 bd = bd_d[index];
if (bd.x == bd.y) {
return;
}
int t_id = pos_scan_d[index];
float dist = dist_d[index];
if (link == MinLink)
atomicMinFloat(&dist_rd_d[t_id], dist);
else if (link == MaxLink)
atomicMaxFloat(&dist_rd_d[t_id], dist);
else { // Sum link or Mean link
atomicAdd(&dist_rd_d[t_id], dist);
}
}
/*
* get the start index of boundaries
*/
__global__ void
cast_boundary_s(const int2* const bd_d, int* const bds_d, const int num_bd)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index >= num_bd)
return;
bds_d[index] = bd_d[index].x;
}
/*
* find the corresponding index of the minimum distance
*/
__global__ void
min_value_to_label(const int2* const bd_d,
const float* const dist_d,
float* const min_dist_d,
int* nn_d,
const int num_bd)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index >= num_bd)
return;
int2 bd = bd_d[index];
float dist = dist_d[index];
float min_dist = min_dist_d[bd.x];
if (min_dist == dist) {
// nn_d[bd.x] = bd.y;
// if there are multiple same minimum, select the one with the maximum
// index to avoid inconsistent results during multiple runs
atomicMax(&nn_d[bd.x], bd.y);
}
}
/*
* set to a constant 'zero' value
*/
template<typename T>
__global__ void
set_to_zero(T* const t1_d, const int N, const T zero)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index >= N)
return;
t1_d[index] = zero;
}
/*
* atomic reduce to compute maximum distance
*/
__global__ void
compute_dist_max_atomic(const int* const clus_d,
const float* const dist_min_d,
float* dist_max_d,
const int num_clus_p)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index >= num_clus_p)
return;
int cc = clus_d[index];
float dist = dist_min_d[index];
atomicMaxFloat(&dist_max_d[cc], dist);
}
/*
* atomic reduce to compute minimum distance
*/
__global__ void
compute_dist_min_atomic(const int* const bds_d,
const float* const dist_d,
float* dist_min_d,
const int num_bd)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index >= num_bd)
return;
int bds = bds_d[index];
float dist = dist_d[index];
atomicMinFloat(&dist_min_d[bds], dist);
}
/*
* atomicAdd reduce along the x axis per channel
*/
__global__ void
reduce_rows_atomic(const float* const mean_d,
float* const mean_rd_d,
const int* const clus_d,
const int length,
const int length_rd,
const int channels)
{
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x >= length || y >= channels)
return;
int cc = clus_d[x];
int index = y * length + x;
int index_rd = y * length_rd + cc;
atomicAdd(&mean_rd_d[index_rd], mean_d[index]);
}
/*
* sum to mean
*/
__global__ void
sum_to_mean(float* const mean_d, const int length, const int channels)
{
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x >= length || y >= channels - 1)
return;
int index = y * length + x;
int index_w = (channels - 1) * length + x;
float w = mean_d[index_w];
mean_d[index] /= w;
}
/*
* compute the distance of superpixels for Centroid and Ward Linkage
*/
__global__ void
compute_dist_mean_k(const float* const mean_d,
const int2* const bd_d,
float* const dist_d,
const int num_bd,
const int num_clus,
const int channels,
const bool is_compact)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index >= num_bd)
return;
int2 bd = bd_d[index];
int s = bd.x, t = bd.y;
// This is a BUG, have found no reason
/*float dist = 0;*/
/*for (int i = 0; i < channels - 1; ++i) {*/
/*int base_id = i * num_clus;*/
/*dist += distL2(mean_d[base_id + s], mean_d[base_id + t], i);*/
/*}*/
// This gives correct result as the atomic version
float dist = distL2(mean_d[s], mean_d[t], 0) +
distL2(mean_d[s + num_clus], mean_d[t + num_clus], 1) +
distL2(mean_d[s + num_clus * 2], mean_d[t + num_clus * 2], 2);
// TODO: can combine color and pos dist to achieve compactness
if (is_compact)
;
dist_d[index] = dist;
}
/*
* compute the distance of superpixels for Centroid and Ward Linkage
* atomic version
*/
__global__ void
compute_dist_mean_atomic(const float* const mean_d,
const int2* const bd_d,
float* const dist_d,
const int num_bd,
const int num_clus,
const int channels)
{
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x >= num_bd || y >= channels - 1)
return;
int2 bd = bd_d[x];
int s = bd.x, t = bd.y;
int base_id = y * num_clus;
float dist = distL2(mean_d[base_id + s], mean_d[base_id + t], y);
atomicAdd(&dist_d[x], dist);
}
/*
* Centroid to Ward Linkage conversion
*/
__global__ void
ward_weight(const float* const mean_d,
const int2* const bd_d,
float* const dist_d,
const int num_bd,
const int num_clus,
const int channels)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index >= num_bd)
return;
int2 bd = bd_d[index];
int s = bd.x, t = bd.y;
int base_id = (channels - 1) * num_clus;
float s_w = mean_d[base_id + s];
float t_w = mean_d[base_id + t];
dist_d[index] = s_w * t_w / (s_w + t_w) * dist_d[index];
}
/*
* pernalize the distance if it is larger than the target's
* maximum inner-cluster distance
*/
__global__ void
pernalize_dist_k(const int2* const bd_d,
const float* const dist_max_d,
float* const dist_d,
const int num_bd)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index >= num_bd)
return;
// int s = bd_d[index].x;
int t = bd_d[index].y;
// This gives higher boundary recall, but lower under segmentation error
// dist_d[index] += abs(dist_max_d[s] - dist_max_d[t]);
float delta_d = dist_d[index] - dist_max_d[t];
if (delta_d > 0) {
dist_d[index] += delta_d;
}
// This makes the distance symetry, such that the cycle-root has only two
// vertices
// int s = bd_d[index].x;
// int t = bd_d[index].y;
// float delta_d_s = dist_d[index] - dist_max_d[s];
// float delta_d_t = dist_d[index] - dist_max_d[t];
// if (delta_d_s > 0) {
// dist_d[index] += delta_d_s;
// }
// if (delta_d_t > 0) {
// dist_d[index] += delta_d_t;
// }
}
/*
* update the label of image pixels
*/
__global__ void
update_image_label_k(int* img_clus_d,
int* const clus_d,
int* img_mask_d,
int* const mask_d,
const int im_size)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index >= im_size)
return;
int label = img_clus_d[index];
img_clus_d[index] = clus_d[label];
if (img_mask_d)
img_mask_d[index] = mask_d[label];
}
/*
* pos_scan_d stores the number of child vertices
* dist_d stores the summation of inner-cluster distance
*/
__global__ void
reduce_dist_pos_atomic(const float* const dist_min_d,
const int* const clus_d,
float* const dist_d,
int* const pos_scan_d,
const int num_clus_p)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index >= num_clus_p)
return;
int cc = clus_d[index];
float dist_min = dist_min_d[index];
atomicAdd(&pos_scan_d[cc], 1);
atomicAdd(&dist_d[cc], dist_min);
}
/*
* bound the label and number of child vertices of a superpixel
* for the latter sorting
*/
__global__ void
set_label_pos(int2* const bd_scan_d, int* pos_scan_d, const int num_clus)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index >= num_clus)
return;
int2 bd_scan;
bd_scan.x = index;
bd_scan.y = pos_scan_d[index];
bd_scan_d[index] = bd_scan;
}
/*
* unpack the sorted label and pos
*/
__global__ void
unpack_label_pos(int2* const bd_scan_d,
int* const bds_d,
int* const pos_scan_d,
const int num_clus)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index >= num_clus)
return;
int2 bd_scan = bd_scan_d[index];
bds_d[index] = bd_scan.x;
pos_scan_d[index] = bd_scan.y;
}
/*
* determine if a superpixel shall be split to its child vertices
*/
__global__ void
is_break_cluster(int* const predicate_d,
int* const pos_scan_d,
const int target_clus,
const int num_clus)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index >= num_clus)
return;
int expect_num = pos_scan_d[index] + num_clus - (index + 1);
int expect_num_p = 0;
if (index > 0)
expect_num_p = pos_scan_d[index - 1] + num_clus - index;
if (expect_num >= target_clus) {
predicate_d[index] = 0;
if (expect_num_p < target_clus) {
predicate_d[index] = pos_scan_d[index];
pos_scan_d[num_clus - 1] = index;
}
} else
predicate_d[index] = pos_scan_d[index];
}
/*
* help function
*/
__global__ void
pre_clus_label(int* const predicate_d,
const int breaked_id_h,
const int num_breaked_clus,
const int num_clus)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index >= num_clus)
return;
if (predicate_d[index] == 0)
predicate_d[index] = index - breaked_id_h + num_breaked_clus - 1;
else
predicate_d[index] -= 1;
}
/*
* determine the new label of clusters
*/
__global__ void
reset_clus_label(int* const clus_d,
int* const pos_scan_d,
const int num_clus_p,
const int num_breaked_clus,
const int num_breaked_exact)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index >= num_clus_p)
return;
int cc = clus_d[index];
int pre_cc = pos_scan_d[cc];
if (pre_cc < num_breaked_clus) {
// the atomic return old, instead of old - value
// clus_d[index] = atomicSub(&pos_scan_d[cc], 1);
int new_cc = atomicSub(&pos_scan_d[cc], 1);
if (new_cc >= num_breaked_exact)
clus_d[index] = num_breaked_exact;
else
clus_d[index] = new_cc;
} else {
clus_d[index] = pre_cc + num_breaked_exact - num_breaked_clus + 1;
}
}
__global__ void
img_struct_to_array(const float4* const, float* const, const int, const int);
__global__ void
img_uchar3_to_float4(const uchar3*, float4*, const int, const int);
__global__ void
img_float4_to_uchar3(const float4*, uchar3*, const int, const int);
__global__ void
img_uchar3_to_float1(const uchar3*, float*, const int, const int);
__global__ void
img_float4_to_float1(const float4*, float*, const int, const int, const int);
__global__ void
img_float1_to_float4(const float*, float4*, const int, const int, const int);
__global__ void
img_float1_to_uchar3(const float*, uchar3*, const int, const int);
__global__ void
img_BGR_to_LAB(float4*, const int, const int);
__global__ void
img_LAB_to_BGR(float4*, const int, const int);
/*********************************************************************************/
// code for host
void
img_gauss_blur(float4*&, float4*&, float*, const double, const int, const int);
void
img_sobel_grad(float4*&, float4*&, const int, const int);
// The image opearations can also implemented by OpenCV GPU
// which would be much simpler
/*
* copy image from host to device
*/
void
SegHAC::img_CPU_to_GPU(const cv::Mat& src,
float4*& img_f4_d,
const double sigma)
{
cudaMemcpy(
img_u3_d, src.data, sizeof(uchar3) * im_size, cudaMemcpyHostToDevice);
img_uchar3_to_float4<<<img_grids, m_blocks>>>(
img_u3_d, img_f4_d, width, height);
if (sigma > FLT_MIN)
img_gauss_blur(img_f4_d, buf_f4_d, filter_d, sigma, width, height);
img_BGR_to_LAB<<<img_grids, m_blocks>>>(img_f4_d, width, height);
}
/*
* copy image from device to host
*/
void
SegHAC::img_GPU_to_CPU(const float* const img_f1_d, cv::Mat& dst_img)
{
img_float1_to_float4<<<img_grids, m_blocks>>>(
img_f1_d, img_f4_d, width, height, 0);
img_LAB_to_BGR<<<img_grids, m_blocks>>>(img_f4_d, width, height);
img_float4_to_uchar3<<<img_grids, m_blocks>>>(
img_f4_d, img_u3_d, width, height);
cudaMemcpy(
dst_img.data, img_u3_d, sizeof(uchar3) * im_size, cudaMemcpyDeviceToHost);
}
/*
* compute the nearest neighbor of pixels in the
* image grid
*/
void
SegHAC::compute_1nn_grid(const float4* const img_f4_d,
int* const nn_d,
int2* const bd_d,
float* const dist_d,
float* const dist_min_d)
{
compute_1nn_grid_k<<<img_grids, m_blocks>>>(
img_f4_d, nn_d, bd_d, dist_rd_d, dist_min_d, num_nb, width, height);
if (dist_rd_d)
cudaMemcpy(
dist_d, dist_rd_d, sizeof(float) * num_bd, cudaMemcpyDeviceToDevice);
}
/*
* initialize the mean vector
*/
void
SegHAC::init_data_mean(const float4* const img_f4_d, float* const mean_d)
{
img_struct_to_array<<<img_grids, m_blocks>>>(
img_f4_d, mean_d, width, height);
}
/*
* initialize the image label
*/
void
SegHAC::initialize_image_label(int* const img_clus_d, const int im_size)
{
set_to_identity<<<img_grid, m_block>>>(img_clus_d, im_size);
num_bd = im_size * num_nb;
num_clus_isp.clear();
num_bd_isp.clear();
}
void
SegHAC::update_image_label(int* img_clus_d,
int* const clus_d,
int* img_mask_d,
int* const mask_d)
{
if (img_mask_d)
cudaMemset(img_mask_d, 0, sizeof(int) * im_size);
update_image_label_k<<<img_grid, m_block>>>(
img_clus_d, clus_d, img_mask_d, mask_d, im_size);
}
void draw_1nn_graph(cv::Mat& seg_h, const int* const nn_d, const int* const img_clus_d,
const int num_clus, const int width, const int height){
int* nn_h = new int[num_clus];
int* img_clus_h = new int[width * height];
float* pos_h = new float[num_clus * 3];
cudaMemcpy(nn_h, nn_d, sizeof(int)*num_clus, cudaMemcpyDeviceToHost);
cudaMemcpy(img_clus_h, img_clus_d, sizeof(int)*width*height, cudaMemcpyDeviceToHost);
for (int i=0;i<num_clus * 3;++i) pos_h[i] = 0;
for (int i=0;i<width * height;++i) {
int c = img_clus_h[i];
pos_h[c] += i % width;
pos_h[num_clus + c] += i / width;
pos_h[num_clus * 2 + c] += 1;
}
for (int i=0;i<num_clus;++i) {
pos_h[i] /= pos_h[num_clus * 2 +i];
pos_h[num_clus + i] /= pos_h[num_clus * 2 +i];
}
for (int i=0;i<num_clus;++i) {
int nn = nn_h[i];
float s_x = pos_h[i];
float s_y = pos_h[num_clus + i];
float t_x = pos_h[nn];
float t_y = pos_h[num_clus + nn];
cv::Point2f s_p(s_x, s_y);
cv::Point2f t_p(t_x, t_y);
cv::Point2f c_p = s_p + (t_p-s_p)/ 3 * 2;
cv::circle(seg_h, s_p, 2, cv::Scalar(0,0,0), 2, cv::LINE_AA);
cv::arrowedLine(seg_h, s_p, c_p, cv::Scalar(0,0,0), 2, cv::LINE_AA,0, 0.10);
cv::line(seg_h, c_p, t_p, cv::Scalar(0,0,0), 2, cv::LINE_AA);
}
delete[] pos_h;
delete[] img_clus_h;
delete[] nn_h;
}
cv::Mat
SegHAC::draw_segmentation(int* img_clus_d, int* img_mask_d)
{
if (m_seg_format == BoundaryFormat) {
cudaMemcpy(seg_u3_d,
img_u3_d,
sizeof(uchar3) * im_size,
cudaMemcpyDeviceToDevice);
draw_boundary<<<img_grids, m_blocks>>>(
seg_u3_d, img_clus_d, width, height);
if (img_mask_d)
draw_cycle_root<<<img_grids, m_blocks>>>(
seg_u3_d, img_mask_d, width, height);
} else if (m_seg_format == LabelUchar3Format) {
cudaMemset(seg_u3_d, 0, sizeof(uchar3) * im_size);
draw_labels_uchar3<<<img_grids, m_blocks>>>(
seg_u3_d, img_clus_d, width, height);
} else if (m_seg_format == LabelIntFormat) {
cudaMemset(seg_i1_d, 0, sizeof(uchar3) * im_size);
draw_labels_int<<<img_grids, m_blocks>>>(
seg_i1_d, img_clus_d, width, height);
}
cv::Mat seg_h;
if (m_seg_format == BoundaryFormat || m_seg_format == LabelUchar3Format) {
seg_h = cv::Mat(height, width, CV_8UC3);
cudaMemcpy(seg_h.data,
seg_u3_d,
sizeof(uchar3) * im_size,
cudaMemcpyDeviceToHost);
} else if (m_seg_format == LabelIntFormat) {
seg_h = cv::Mat(height, width, CV_32SC1);
cudaMemcpy(
seg_h.data, seg_i1_d, sizeof(int) * im_size, cudaMemcpyDeviceToHost);
}
return seg_h;
}
void
SegHAC::compute_dist_pos(const float* const dist_min_d,
const int* const clus_d,
float* const dist_d,
int* const pos_scan_d,
const int num_clus_p,
const int num_clus)
{
int grid_p = (num_clus_p + m_block - 1) / m_block;
cudaMemset(pos_scan_d, 0, sizeof(int) * num_clus);
cudaMemset(dist_d, 0, sizeof(float) * num_clus);
reduce_dist_pos_atomic<<<grid_p, m_block>>>(
dist_min_d, clus_d, dist_d, pos_scan_d, num_clus_p);
}
void
SegHAC::compute_scaned_pos(float* const dist_d,
int* const pos_scan_d,
int* const bds_d,
const int num_clus)
{
int grid = (num_clus + m_block - 1) / m_block;
// use bd_scan_d as buffer here
set_label_pos<<<grid, m_block>>>(bd_scan_d, pos_scan_d, num_clus);