-
Notifications
You must be signed in to change notification settings - Fork 1
/
histogram.cu
executable file
·821 lines (671 loc) · 25.3 KB
/
histogram.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
#include "dependencies.h"
__device__ int dev_histogram[256] = {0};
__device__ float dev_normalized_histogram[256] = {0};
__device__ float dev_cdf[256] = {0};
__device__ int dev_equalization_values[256] = {0};
/*color gpu variables*/
__device__ int dev_histogram_red[256] = {0};
__device__ float dev_normalized_histogram_red[256] = {0};
__device__ float dev_cdf_red[256] = {0};
__device__ int dev_equalization_values_red[256] = {0};
__device__ int dev_histogram_green[256] = {0};
__device__ float dev_normalized_histogram_green[256] = {0};
__device__ float dev_cdf_green[256] = {0};
__device__ int dev_equalization_values_green[256] = {0};
__device__ int dev_histogram_blue[256] = {0};
__device__ float dev_normalized_histogram_blue[256] = {0};
__device__ float dev_cdf_blue[256] = {0};
__device__ int dev_equalization_values_blue[256] = {0};
__global__ void k_1D_extract_histogram(unsigned char* input, int data_size, int thread_load) {
int blockId = blockIdx.x + blockIdx.y * gridDim.x;
int threadId = (blockId * (blockDim.x * blockDim.y) + (threadIdx.y * blockDim.x) + threadIdx.x) * thread_load;
if (threadId >= data_size) {
return;
}
for(int i = 0 ; i < thread_load; i++){
atomicAdd(&dev_histogram[input[threadId + i]], 1);
}
}
__global__ void k_1D_extract_histogram_shared_mem(unsigned char* input, int data_size, int thread_load) {
__shared__ unsigned int cache[256];
int thread_id_in_block = (threadIdx.x * blockDim.y) + threadIdx.y;
int blockId = blockIdx.x + blockIdx.y * gridDim.x;
int threadId = (blockId * (blockDim.x * blockDim.y) + (threadIdx.y * blockDim.x) + threadIdx.x) * thread_load;
if (threadId >= data_size) {
return;
}
if (thread_id_in_block < 256) {
cache[thread_id_in_block] = 0;
}
__syncthreads();
for(int i = 0 ; i < thread_load; i++){
int idx = threadId + i;
atomicAdd(&cache[(input[idx])], 1);
}
__syncthreads();
if (thread_id_in_block < 256) {
atomicAdd(&dev_histogram[thread_id_in_block], cache[thread_id_in_block]);
}
}
__global__ void k_1D_normalize_cdf_equalization(int pixels) {
int threadId = blockIdx.x * blockDim.x + threadIdx.x;
float sum = 0.0f;
dev_normalized_histogram[threadId] = dev_histogram[threadId] / (float)(pixels);
__syncthreads();
for (int i = 0; i <= threadId; i++) {
sum += dev_normalized_histogram[i];
}
dev_cdf[threadId] = sum;
dev_equalization_values[threadId] = int((dev_cdf[threadId] * 255.0f) + 0.5f);
}
__global__ void k_1D_normalize_cdf_equalization_shared_mem(int pixels) {
__shared__ float cache_normalized_histogram[256];
__shared__ float cache_cdf[256];
int threadId = blockIdx.x * blockDim.x + threadIdx.x;
cache_normalized_histogram[threadId] = dev_histogram[threadId] / (float)(pixels);
__syncthreads();
float sum = 0.0f;
for (int i = 0; i <= threadId; i++) {
sum += cache_normalized_histogram[i];
}
cache_cdf[threadId] = sum;
dev_equalization_values[threadId] = int((cache_cdf[threadId] * 255.0f) + 0.5f);
}
__global__ void k_1D_histogram_equalization(unsigned char* input, int pixels, int thread_load) {
int blockId = blockIdx.x + blockIdx.y * gridDim.x;
int threadId = (blockId * (blockDim.x * blockDim.y) + (threadIdx.y * blockDim.x) + threadIdx.x) * thread_load;
if (threadId >= pixels) {
return;
}
for(int i = 0; i < thread_load; i++){
input[threadId + i] = static_cast<uchar>(dev_equalization_values[input[threadId + i]]);
}
}
__global__ void k_1D_histogram_equalization_shared_mem(unsigned char* input, int pixels, int thread_load) { /*load the cache before threadId control*/
__shared__ int cache_equalization_values[256];
int thread_id_in_block = (threadIdx.x * blockDim.y) + threadIdx.y;
int blockId = blockIdx.x + blockIdx.y * gridDim.x;
int threadId = (blockId * (blockDim.x * blockDim.y) + (threadIdx.y * blockDim.x) + threadIdx.x) * thread_load;
if(thread_id_in_block < 256){
cache_equalization_values[thread_id_in_block] = dev_equalization_values[thread_id_in_block];
}
if (threadId >= pixels) {
return;
}
__syncthreads();
for(int i = 0 ; i < thread_load; i++){
input[threadId + i] = static_cast<uchar>(cache_equalization_values[input[threadId + i]]);
}
}
__global__ void k_3D_extract_histogram(unsigned char* input, int pixels, int thread_load) {
int blockId = blockIdx.x + blockIdx.y * gridDim.x;
int threadId = (blockId * (blockDim.x * blockDim.y) + (threadIdx.y * blockDim.x) + threadIdx.x) * thread_load;
if (threadId >= pixels) {
return;
}
for(int i = 0; i < thread_load; i++){
int idx = threadId + i;
switch (idx % 3)
{
case 0:
atomicAdd(&dev_histogram_red[input[idx]], 1);
break;
case 1:
atomicAdd(&dev_histogram_green[input[idx]], 1);
break;
case 2:
atomicAdd(&dev_histogram_blue[input[idx]], 1);
break;
default:
break;
}
}
}
__global__ void k_3D_extract_histogram_shared_mem(unsigned char* input, int channels, int thread_load) {
__shared__ unsigned int cache_histogram_red[256];
__shared__ unsigned int cache_histogram_green[256];
__shared__ unsigned int cache_histogram_blue[256];
int threadIdInBlock = (threadIdx.x * blockDim.y) + threadIdx.y;
int blockId = blockIdx.x + blockIdx.y * gridDim.x;
int threadId = (blockId * (blockDim.x * blockDim.y) + (threadIdx.y * blockDim.x) + threadIdx.x) * thread_load;
if (threadIdInBlock < 256) {
cache_histogram_red[threadIdInBlock] = 0;
cache_histogram_green[threadIdInBlock] = 0;
cache_histogram_blue[threadIdInBlock] = 0;
}
__syncthreads();
if (threadId >= channels) {
return;
}
for(int i = 0 ; i < thread_load ; i++){
int idx = threadId + i;
switch (idx % 3)
{
case 0:
atomicAdd(&cache_histogram_red[(input[idx])], 1);
break;
case 1:
atomicAdd(&cache_histogram_green[(input[idx])], 1);
break;
case 2:
atomicAdd(&cache_histogram_blue[(input[idx])], 1);
default:
break;
}
}
__syncthreads();
if (threadIdInBlock < 256) {
atomicAdd(&dev_histogram_red[threadIdInBlock], cache_histogram_red[threadIdInBlock]);
atomicAdd(&dev_histogram_green[threadIdInBlock], cache_histogram_green[threadIdInBlock]);
atomicAdd(&dev_histogram_blue[threadIdInBlock], cache_histogram_blue[threadIdInBlock]);
}
}
__global__ void k_3D_normalize_cdf_equalization(int pixels) { /*1,256*/
int threadId = blockIdx.x * blockDim.x + threadIdx.x;
dev_normalized_histogram_red[threadId] = dev_histogram_red[threadId] / (float)(pixels);
dev_normalized_histogram_green[threadId] = dev_histogram_green[threadId] / (float)(pixels);
dev_normalized_histogram_blue[threadId] = dev_histogram_blue[threadId] / (float)(pixels);
__syncthreads();
float sum_red = 0.0f, sum_green = 0.0f, sum_blue = 0.0f;
for (int i = 0; i <= threadId; i++) {
sum_red += dev_normalized_histogram_red[i];
sum_green += dev_normalized_histogram_green[i];
sum_blue += dev_normalized_histogram_blue[i];
}
dev_cdf_red[threadId] = sum_red;
dev_cdf_green[threadId] = sum_green;
dev_cdf_blue[threadId] = sum_blue;
__syncthreads();
dev_equalization_values_red[threadId] = int((dev_cdf_red[threadId] * 255.0f) + 0.5f);
dev_equalization_values_green[threadId] = int((dev_cdf_green[threadId] * 255.0f) + 0.5f);
dev_equalization_values_blue[threadId] = int((dev_cdf_blue[threadId] * 255.0f) + 0.5f);
}
__global__ void k_3D_normalize_cdf_equalization_shared_mem(int pixels) { /*1,256*/
__shared__ float cache_normalized_histogram_red[256];
__shared__ float cache_normalized_histogram_green[256];
__shared__ float cache_normalized_histogram_blue[256];
__shared__ float cache_cdf_red[256];
__shared__ float cache_cdf_green[256];
__shared__ float cache_cdf_blue[256];
int threadId = blockIdx.x * blockDim.x + threadIdx.x;
cache_normalized_histogram_red[threadId] = dev_histogram_red[threadId] / (float)(pixels);
cache_normalized_histogram_green[threadId] = dev_histogram_green[threadId] / (float)(pixels);
cache_normalized_histogram_blue[threadId] = dev_histogram_blue[threadId] / (float)(pixels);
__syncthreads();
float sum_red = 0.0f, sum_green = 0.0f, sum_blue = 0.0f;
for (int i = 0; i <= threadId; i++) {
sum_red += cache_normalized_histogram_red[i];
sum_green += cache_normalized_histogram_green[i];
sum_blue += cache_normalized_histogram_blue[i];
}
cache_cdf_red[threadId] = sum_red;
cache_cdf_green[threadId] = sum_green;
cache_cdf_blue[threadId] = sum_blue;
dev_equalization_values_red[threadId] = int((cache_cdf_red[threadId] * 255.0f) + 0.5f);
dev_equalization_values_green[threadId] = int((cache_cdf_green[threadId] * 255.0f) + 0.5f);
dev_equalization_values_blue[threadId] = int((cache_cdf_blue[threadId] * 255.0f) + 0.5f);
}
__global__ void k_3D_histogram_equalization(unsigned char* input, int channels, int thread_load) {
int blockId = blockIdx.x + blockIdx.y * gridDim.x;
int threadId = (blockId * (blockDim.x * blockDim.y) + (threadIdx.y * blockDim.x) + threadIdx.x) * thread_load;
if (threadId >= channels) {
return;
}
for(int i = 0; i < thread_load; i++){
int idx = threadId + i;
switch (idx % 3)
{
case 0:
input[idx] = static_cast<uchar>(dev_equalization_values_red[input[idx]]);
break;
case 1:
input[idx] = static_cast<uchar>(dev_equalization_values_green[input[idx]]);
break;
case 2:
input[idx] = static_cast<uchar>(dev_equalization_values_blue[input[idx]]);
break;
default:
break;
}
}
}
__global__ void k_3D_histogram_equalization_shared_mem(unsigned char* input, int channels, int thread_load) {
__shared__ int cache_equalization_values_red[256 + 2];
__shared__ int cache_equalization_values_green[256 + 2];
__shared__ int cache_equalization_values_blue[256 + 2];
int thread_id_in_block = (threadIdx.x * blockDim.y) + threadIdx.y;
int blockId = blockIdx.x + blockIdx.y * gridDim.x;
int threadId = (blockId * (blockDim.x * blockDim.y) + (threadIdx.y * blockDim.x) + threadIdx.x) * thread_load;
if (thread_id_in_block < 256) {
cache_equalization_values_red[thread_id_in_block + 1] = dev_equalization_values_red[thread_id_in_block];
cache_equalization_values_green[thread_id_in_block + 1] = dev_equalization_values_green[thread_id_in_block];
cache_equalization_values_blue[thread_id_in_block + 1] = dev_equalization_values_blue[thread_id_in_block];
}
if (threadId >= channels) {
return;
}
__syncthreads();
for(int i = 0 ; i < thread_load; i++){
int idx = threadId + i;
switch (idx % 3)
{
case 0:
input[idx] = static_cast<uchar>(cache_equalization_values_red[input[idx] + 1]);
break;
case 1:
input[idx] = static_cast<uchar>(cache_equalization_values_green[input[idx] + 1]);
break;
case 2:
input[idx] = static_cast<uchar>(cache_equalization_values_blue[input[idx] + 1]);
break;
default:
break;
}
}
}
float histogram_equalization_gpu_3D(cv::Mat input_img, cv::Mat* output_img, bool sm) {
unsigned char* gpu_input = nullptr;
unsigned char* input = input_img.data;
unsigned char* output = output_img->data;
unsigned int thread_load = 9;
unsigned int cols = input_img.cols;
unsigned int true_cols = cols * 3;
unsigned int rows = input_img.rows;
unsigned int pixels = cols * rows;
unsigned int channels = pixels * 3;
unsigned long int size = cols * rows * sizeof(unsigned char) * 3;
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start);
CHECK_CUDA_ERROR(cudaMalloc((unsigned char**)&gpu_input, size));
CHECK_CUDA_ERROR(cudaMemcpy(gpu_input, input, size, cudaMemcpyHostToDevice));
dim3 block(32, 32);
dim3 grid((true_cols / thread_load + block.x - 1) / block.x, (rows + block.y - 1) / block.y);
if(sm){
k_3D_extract_histogram_shared_mem << <grid, block >> > (gpu_input, channels, thread_load);
CHECK_CUDA_ERROR(cudaDeviceSynchronize());
k_3D_normalize_cdf_equalization_shared_mem << <1, 256 >> > (pixels);
CHECK_CUDA_ERROR(cudaDeviceSynchronize());
k_3D_histogram_equalization_shared_mem << <grid, block >> > (gpu_input, channels, thread_load);
}else{
k_3D_extract_histogram << <grid, block >> > (gpu_input, channels, thread_load);
CHECK_CUDA_ERROR(cudaDeviceSynchronize());
k_3D_normalize_cdf_equalization<< <1, 256 >> > (pixels);
CHECK_CUDA_ERROR(cudaDeviceSynchronize());
k_3D_histogram_equalization<< <grid, block >> > (gpu_input, channels, thread_load);
}
CHECK_CUDA_ERROR(cudaMemcpy(output, gpu_input, size, cudaMemcpyDeviceToHost));
cudaEventRecord(stop);
cudaEventSynchronize(stop);
float gpuElapsedTime = 0;
cudaEventElapsedTime(&gpuElapsedTime, start, stop);
cudaFree(gpu_input);
cudaDeviceReset();
return gpuElapsedTime;
}
float histogram_equalization_gpu_1D(cv::Mat input_img, cv::Mat* output_img, bool sm) {
unsigned char* gpu_input = nullptr;
unsigned char* input = input_img.data;
unsigned char* output = output_img->data;
int thread_load = 9;
unsigned int cols = input_img.cols;
unsigned int rows = input_img.rows;
unsigned int pixels = input_img.cols * input_img.rows;
unsigned long int data_size = pixels * sizeof(unsigned char);
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start);
CHECK_CUDA_ERROR(cudaMalloc((unsigned char**)&gpu_input, data_size));
CHECK_CUDA_ERROR(cudaMemcpy(gpu_input, input, data_size, cudaMemcpyHostToDevice));
dim3 block(32, 32);
dim3 grid((cols / thread_load + block.x - 1) / block.x, (rows + block.y - 1) / block.y);
if(sm){
k_1D_extract_histogram_shared_mem << <grid, block >> > (gpu_input, pixels, thread_load);
CHECK_CUDA_ERROR(cudaDeviceSynchronize());
k_1D_normalize_cdf_equalization_shared_mem << <1, 256 >> > (pixels);
CHECK_CUDA_ERROR(cudaDeviceSynchronize());
k_1D_histogram_equalization_shared_mem << <grid, block >> > (gpu_input, pixels, thread_load);
}else{
k_1D_extract_histogram << <grid, block >> > (gpu_input, pixels, thread_load);
CHECK_CUDA_ERROR(cudaDeviceSynchronize());
k_1D_normalize_cdf_equalization << <1, 256 >> > (pixels);
CHECK_CUDA_ERROR(cudaDeviceSynchronize());
k_1D_histogram_equalization<< <grid, block >> > (gpu_input, pixels, thread_load);
}
CHECK_CUDA_ERROR(cudaMemcpy(output, gpu_input, data_size, cudaMemcpyDeviceToHost));
cudaEventRecord(stop);
cudaEventSynchronize(stop);
float gpuElapsedTime = 0;
cudaEventElapsedTime(&gpuElapsedTime, start, stop);
cudaFree(gpu_input);
cudaDeviceReset();
return gpuElapsedTime;
}
float histogram_equalization_cpu_1D(cv::Mat inputImg, cv::Mat* outputImg) {
unsigned char* input = inputImg.data;
unsigned char* output = outputImg->data;
int histogram[256] = { 0 };
float cdf[256] = { 0 };
float normalizedHistogram[256] = { 0 };
int equalization[256] = { 0 };
int pixels = inputImg.cols * inputImg.rows;
auto start = std::chrono::steady_clock::now();
for (int i = 0; i < pixels; i++) {
histogram[input[i]]++;
}
for (int i = 0; i < 256; i++) {
normalizedHistogram[i] = (histogram[i] / (float)pixels);
}
cdf[0] = normalizedHistogram[0];
for (int i = 1; i < 256; i++) {
cdf[i] = cdf[i - 1] + normalizedHistogram[i];
}
for (int i = 0; i < 256; i++) {
equalization[i] = int((cdf[i] * 255.0f) + 0.5f);
}
for (int i = 0; i < pixels; i++) {
output[i] = equalization[input[i]];
}
auto end = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start) / 1000.0f;
return elapsed.count();
}
float histogram_equalization_cpu_3D(cv::Mat input_img, cv::Mat* output_img) {
unsigned char* input = input_img.data;
unsigned char* output = output_img->data;
int histogram_red[256] = { 0 };
int histogram_green[256] = { 0 };
int histogram_blue[256] = { 0 };
float normalize_histogram_red[256] = { 0 };
float normalize_histogram_green[256] = { 0 };
float normalize_histogram_blue[256] = { 0 };
float cdf_red[256] = { 0 };
float cdf_green[256] = { 0 };
float cdf_blue[256] = { 0 };
int equalization_red[256] = { 0 };
int equalization_green[256] = { 0 };
int equalization_blue[256] = { 0 };
int pixels = input_img.cols * input_img.rows;
int size = pixels * 3 * sizeof(unsigned char);
auto start = std::chrono::steady_clock::now();
for (int i = 0; i < pixels; i++) { /*Calculating histogram of input image*/
histogram_red[input[i * 3]]++;
histogram_green[input[i * 3 + 1]]++;
histogram_blue[input[i * 3 + 2]]++;
}
for (int i = 0; i < 256; i++) { /*Calculating normalized histogram (better calculation speed)*/
normalize_histogram_red[i] = (histogram_red[i] / (float)pixels);
normalize_histogram_green[i] = (histogram_green[i] / (float)pixels);
normalize_histogram_blue[i] = (histogram_blue[i] / (float)pixels);
}
cdf_red[0] = normalize_histogram_red[0];
cdf_green[0] = normalize_histogram_green[0];
cdf_blue[0] = normalize_histogram_blue[0];
for (int i = 1; i < 256; i++) { /*Generating CDF array*/
cdf_red[i] = cdf_red[i - 1] + normalize_histogram_red[i];
cdf_green[i] = cdf_green[i - 1] + normalize_histogram_green[i];
cdf_blue[i] = cdf_blue[i - 1] + normalize_histogram_blue[i];
}
for (int i = 0; i < 256; i++) { /*Generating new pixel intensity values then assign them*/
equalization_red[i] = int((cdf_red[i] * 255.0f) + 0.5f);
equalization_green[i] = int((cdf_green[i] * 255.0f) + 0.5f);
equalization_blue[i] = int((cdf_blue[i] * 255.0f) + 0.5f);
}
for (int i = 0; i < pixels; i++) {
output[i * 3] = equalization_red[input[i * 3]];
output[i * 3 + 1] = equalization_green[input[i * 3 + 1]];
output[i * 3 + 2] = equalization_blue[input[i * 3 + 2]];
}
auto end = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start) / 1000.0f;
return elapsed.count();
}
float histogram_equalization_cpu_parallel_1D(cv::Mat inputImg, cv::Mat* outputImg) {
const unsigned char* input = inputImg.data;
unsigned char* output = outputImg->data;
const unsigned int rows = inputImg.rows;
const unsigned int cols = inputImg.cols;
int histogram[256] = { 0 };
float normalizedHistogram[256] = { 0 };
float cdf[256] = { 0 };
int equalization[256] = { 0 };
int pixels = cols * rows;
std::vector <std::thread> threads;
std::mutex mtx;
std::condition_variable cv;
const int MAX_THREAD_SUPPORT = 12;
const int stride = rows / MAX_THREAD_SUPPORT;
const int stride_for_256 = 256 / MAX_THREAD_SUPPORT;
int step1_count = 0;
int step2_count = 0;
int step3_count = 0;
int step4_count = 0;
auto start = std::chrono::steady_clock::now();
for (int id = 0; id < MAX_THREAD_SUPPORT; id++) {
threads.push_back(std::thread([&,id] () {
int range_start = stride * id;
int range_end = (id == MAX_THREAD_SUPPORT - 1) ? rows : stride * (id + 1);
int t_histogram[256] = {0};
for (int r = range_start; r < range_end; r++) {
for (int c = 0; c < cols; c++) {
{
t_histogram[input[r * cols + c]]++;
}
}
}
{
std::unique_lock<std::mutex> lck(mtx);
for(int i = 0 ; i < 256 ; i++){
histogram[i] += t_histogram[i];
}
}
{
std::unique_lock<std::mutex> lck(mtx);
if (++step1_count == MAX_THREAD_SUPPORT) {
cv.notify_all();
}
else {
cv.wait(lck);
}
}
range_start = stride_for_256 * id;
range_end = (id == MAX_THREAD_SUPPORT - 1) ? 256 : stride_for_256 * (id + 1);
for (int i = range_start; i < range_end; i++) {
normalizedHistogram[i] = histogram[i] / (float)pixels;
}
{
std::unique_lock<std::mutex> lck(mtx);
if (++step2_count == MAX_THREAD_SUPPORT) {
cv.notify_all();
}
else {
cv.wait(lck);
}
}
cdf[0] = normalizedHistogram[0];
for (int i = range_start; i < range_end; i++) {
if(i == 0)
continue;
float sum = 0.0f;
for (int j = 0; j <= i; j++) {
sum += normalizedHistogram[j];
}
cdf[i] = sum;
}
{
std::unique_lock<std::mutex> lck(mtx);
if (++step3_count == MAX_THREAD_SUPPORT) {
cv.notify_all();
}
else {
cv.wait(lck);
}
}
for (int i = range_start; i < range_end; i++) {
equalization[i] = int((cdf[i] * 255.0f) + 0.5f);
}
{
std::unique_lock<std::mutex> lck(mtx);
if (++step4_count == MAX_THREAD_SUPPORT) {
cv.notify_all();
}
else {
cv.wait(lck);
}
}
range_start = stride * id;
range_end = (id == MAX_THREAD_SUPPORT - 1) ? rows : stride * (id + 1);
for (int r = range_start; r < range_end; r++) {
for (int c = 0; c < cols; c++) {
int index = r * cols + c;
output[index] = equalization[input[index]];
}
}
}));
}
for (std::thread& thread : threads) {
thread.join();
}
auto end = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start) / 1000.0f;
return elapsed.count();
}
float histogram_equalization_cpu_parallel_3D(cv::Mat inputImg, cv::Mat* outputImg) {
const unsigned char* input = inputImg.data;
unsigned char* output = outputImg->data;
const unsigned int rows = inputImg.rows;
const unsigned int cols = inputImg.cols;
int histogram_red[256] = { 0 };
int histogram_green[256] = { 0 };
int histogram_blue[256] = { 0 };
float normalize_histogram_red[256] = { 0 };
float normalize_histogram_green[256] = { 0 };
float normalize_histogram_blue[256] = { 0 };
float cdf_red[256] = { 0 };
float cdf_green[256] = { 0 };
float cdf_blue[256] = { 0 };
int equalization_red[256] = { 0 };
int equalization_green[256] = { 0 };
int equalization_blue[256] = { 0 };
int pixels = cols * rows;
std::vector <std::thread> threads;
std::mutex mtx;
std::condition_variable cv;
const int MAX_THREAD_SUPPORT = 12;
const int stride = rows / MAX_THREAD_SUPPORT;
const int stride_for_256 = 256 / MAX_THREAD_SUPPORT;
int step1_count = 0;
int step2_count = 0;
int step3_count = 0;
int step4_count = 0;
auto start = std::chrono::steady_clock::now();
for (int id = 0; id < MAX_THREAD_SUPPORT; id++) {
threads.push_back(std::thread([&,id] () {
int range_start = stride * id;
int range_end = (id == MAX_THREAD_SUPPORT - 1) ? rows : stride * (id + 1);
int local_histogram_red[256] = {0};
int local_histogram_green[256] = {0};
int local_histogram_blue[256] = {0};
for (int r = range_start; r < range_end; r++) {
for (int c = 0; c < cols; c++) {
{
int index = (r * cols + c) * 3;
local_histogram_red[input[index]]++;
local_histogram_green[input[index + 1]]++;
local_histogram_blue[input[index + 2]]++;
}
}
}
{
std::unique_lock<std::mutex> lck(mtx);
for(int i = 0 ; i < 256 ; i++){
histogram_red[i] += local_histogram_red[i];
histogram_green[i] += local_histogram_green[i];
histogram_blue[i] += local_histogram_blue[i];
}
}
{
std::unique_lock<std::mutex> lck(mtx);
if (++step1_count == MAX_THREAD_SUPPORT) {
cv.notify_all();
}
else {
cv.wait(lck);
}
}
range_start = stride_for_256 * id;
range_end = (id == MAX_THREAD_SUPPORT - 1) ? 256 : stride_for_256 * (id + 1);
for (int i = range_start; i < range_end; i++) {
normalize_histogram_red[i] = histogram_red[i] / (float)pixels;
normalize_histogram_green[i] = histogram_green[i] / (float)pixels;
normalize_histogram_blue[i] = histogram_blue[i] / (float)pixels;
}
{
std::unique_lock<std::mutex> lck(mtx);
if (++step2_count == MAX_THREAD_SUPPORT) {
cv.notify_all();
}
else {
cv.wait(lck);
}
}
cdf_red[0] = normalize_histogram_red[0];
cdf_green[0] = normalize_histogram_green[0];
cdf_blue[0] = normalize_histogram_blue[0];
for (int i = range_start; i < range_end; i++) {
float sum_red = 0;
float sum_green = 0;
float sum_blue = 0;
for (int j = 0; j <= i; j++) {
sum_red += normalize_histogram_red[j];
sum_green += normalize_histogram_green[j];
sum_blue += normalize_histogram_blue[j];
}
cdf_red[i] = sum_red;
cdf_green[i] = sum_green;
cdf_blue[i] = sum_blue;
}
{
std::unique_lock<std::mutex> lck(mtx);
if (++step3_count == MAX_THREAD_SUPPORT) {
cv.notify_all();
}
else {
cv.wait(lck);
}
}
for (int i = range_start; i < range_end; i++) {
equalization_red[i] = int((cdf_red[i] * 255.0f) + 0.5f);
equalization_green[i] = int((cdf_green[i] * 255.0f) + 0.5f);
equalization_blue[i] = int((cdf_blue[i] * 255.0f) + 0.5f);
}
{
std::unique_lock<std::mutex> lck(mtx);
if (++step4_count == MAX_THREAD_SUPPORT) {
cv.notify_all();
}
else {
cv.wait(lck);
}
}
range_start = stride * id;
range_end = (id == MAX_THREAD_SUPPORT - 1) ? rows : stride * (id + 1);
for (int r = range_start; r < range_end; r++) {
for (int c = 0; c < cols; c++) {
int index = (r * cols + c) * 3;
output[index] = equalization_red[input[index]];
output[index + 1] = equalization_green[input[index + 1]];
output[index + 2] = equalization_blue[input[index + 2]];
}
}
}));
}
for (std::thread& thread : threads) {
thread.join();
}
auto end = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start) / 1000.0f;
return elapsed.count();
}