This repository was archived by the owner on Jun 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvc.c
More file actions
1774 lines (1556 loc) · 66.5 KB
/
vc.c
File metadata and controls
1774 lines (1556 loc) · 66.5 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
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
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// INSTITUTO POLITÉCNICO DO CÁVADO E DO AVE
// 2011/2012
// ENGENHARIA DE SISTEMAS INFORMÁTICOS
// VISÃO POR COMPUTADOR
//
// [ DUARTE DUQUE - dduque@ipca.pt ]
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Desabilita (no MSVC++) warnings de funções não seguras (fopen, sscanf, etc...)
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <malloc.h>
#include <math.h>
#include "vc.h"
#include "performanceMeasure.c"
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// FUNÇÕES: ALOCAR E LIBERTAR UMA IMAGEM
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Alocar memória para uma imagem
IVC *vc_image_new(int width, int height, int channels, int levels) {
IVC *image = (IVC *) malloc(sizeof(IVC));
if (image == NULL) return NULL;
if ((levels <= 0) || (levels > 255)) return NULL;
image->width = width;
image->height = height;
image->channels = channels;
image->levels = levels;
image->bytesperline = image->width * image->channels;
image->data = (unsigned char *) malloc(image->width * image->height * image->channels * sizeof(char));
if (image->data == NULL) {
return vc_image_free(image);
}
return image;
}
// Libertar memória de uma imagem
IVC *vc_image_free(IVC *image) {
if (image != NULL) {
if (image->data != NULL) {
free(image->data);
image->data = NULL;
}
free(image);
image = NULL;
}
return image;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// FUNÇÕES: LEITURA E ESCRITA DE IMAGENS (PBM, PGM E PPM)
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
char *netpbm_get_token(FILE *file, char *tok, int len) {
char *t;
int c;
for (;;) {
while (isspace(c = getc(file)));
if (c != '#') break;
do c = getc(file);
while ((c != '\n') && (c != EOF));
if (c == EOF) break;
}
t = tok;
if (c != EOF) {
do {
*t++ = (char) c;
c = getc(file);
} while ((!isspace(c)) && (c != '#') && (c != EOF) && (t - tok < len - 1));
if (c == '#') ungetc(c, file);
}
*t = 0;
return tok;
}
long int unsigned_char_to_bit(const unsigned char *datauchar, unsigned char *databit, int width, int height) {
int x, y;
int countbits;
long int pos, counttotalbytes;
unsigned char *p = databit;
*p = 0;
countbits = 1;
counttotalbytes = 0;
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
pos = width * y + x;
if (countbits <= 8) {
// Numa imagem PBM:
// 1 = Preto
// 0 = Branco
//*p |= (datauchar[pos] != 0) << (8 - countbits);
// Na nossa imagem:
// 1 = Branco
// 0 = Preto
*p |= (datauchar[pos] == 0) << (8 - countbits);
countbits++;
}
if ((countbits > 8) || (x == width - 1)) {
p++;
*p = 0;
countbits = 1;
counttotalbytes++;
}
}
}
return counttotalbytes;
}
void bit_to_unsigned_char(unsigned char *databit, unsigned char *datauchar, int width, int height) {
int x, y;
int countbits;
long int pos;
unsigned char *p = databit;
countbits = 1;
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
pos = width * y + x;
if (countbits <= 8) {
// Numa imagem PBM:
// 1 = Preto
// 0 = Branco
//datauchar[pos] = (*p & (1 << (8 - countbits))) ? 1 : 0;
// Na nossa imagem:
// 1 = Branco
// 0 = Preto
datauchar[pos] = (*p & (1 << (8 - countbits))) ? 0 : 1;
countbits++;
}
if ((countbits > 8) || (x == width - 1)) {
p++;
countbits = 1;
}
}
}
}
IVC *vc_read_image(char *filename) {
FILE *file = NULL;
IVC *image = NULL;
unsigned char *tmp;
char tok[20];
long int size, sizeofbinarydata;
int width, height, channels;
int levels = 255;
int v;
// Abre o ficheiro
if ((file = fopen(filename, "rb")) != NULL) {
// Efectua a leitura do header
netpbm_get_token(file, tok, sizeof(tok));
if (strcmp(tok, "P4") == 0) {
channels = 1;
levels = 1;
} // Se PBM (Binary [0,1])
else if (strcmp(tok, "P5") == 0) channels = 1; // Se PGM (Gray [0,MAX(level,255)])
else if (strcmp(tok, "P6") == 0) channels = 3; // Se PPM (RGB [0,MAX(level,255)])
else {
#ifdef VC_DEBUG
printf("ERROR -> vc_read_image():\n\tFile is not a valid PBM, PGM or PPM file.\n\tBad magic number!\n");
#endif
fclose(file);
return NULL;
}
if (levels == 1) // PBM
{
if (sscanf(netpbm_get_token(file, tok, sizeof(tok)), "%d", &width) != 1 ||
sscanf(netpbm_get_token(file, tok, sizeof(tok)), "%d", &height) != 1) {
#ifdef VC_DEBUG
printf("ERROR -> vc_read_image():\n\tFile is not a valid PBM file.\n\tBad size!\n");
#endif
fclose(file);
return NULL;
}
// Aloca memória para imagem
image = vc_image_new(width, height, channels, levels);
if (image == NULL) return NULL;
sizeofbinarydata = (image->width / 8 + ((image->width % 8) ? 1 : 0)) * image->height;
tmp = (unsigned char *) malloc(sizeofbinarydata);
if (tmp == NULL) return 0;
#ifdef VC_DEBUG
printf("Width = %d | Height = %d | Channels = %d | Levels = %d\n", image->width, image->height,
image->channels, image->levels);
#endif
if ((v = (int) fread(tmp, sizeof(unsigned char), sizeofbinarydata, file)) != sizeofbinarydata) {
#ifdef VC_DEBUG
printf("ERROR -> vc_read_image():\n\tPremature EOF on file.\n");
#endif
vc_image_free(image);
fclose(file);
free(tmp);
return NULL;
}
bit_to_unsigned_char(tmp, image->data, image->width, image->height);
free(tmp);
} else // PGM ou PPM
{
if (sscanf(netpbm_get_token(file, tok, sizeof(tok)), "%d", &width) != 1 ||
sscanf(netpbm_get_token(file, tok, sizeof(tok)), "%d", &height) != 1 ||
sscanf(netpbm_get_token(file, tok, sizeof(tok)), "%d", &levels) != 1 || levels <= 0 || levels > 255) {
#ifdef VC_DEBUG
printf("ERROR -> vc_read_image():\n\tFile is not a valid PGM or PPM file.\n\tBad size!\n");
#endif
fclose(file);
return NULL;
}
// Aloca memória para imagem
image = vc_image_new(width, height, channels, levels);
if (image == NULL) return NULL;
#ifdef VC_DEBUG
printf("Width = %d | Height = %d | Channels = %d | Levels = %d\n", image->width, image->height,
image->channels, image->levels);
#endif
size = image->width * image->height * image->channels;
if ((v = (int) fread(image->data, sizeof(unsigned char), size, file)) != size) {
#ifdef VC_DEBUG
printf("ERROR -> vc_read_image():\n\tPremature EOF on file.\n");
#endif
vc_image_free(image);
fclose(file);
return NULL;
}
}
fclose(file);
} else {
#ifdef VC_DEBUG
printf("ERROR -> vc_read_image():\n\tFile not found.\n");
#endif
}
return image;
}
int vc_write_image(char *filename, IVC *image) {
FILE *file = NULL;
unsigned char *tmp;
long int totalbytes, sizeofbinarydata;
if (image == NULL) return 0;
if ((file = fopen(filename, "wb")) != NULL) {
if (image->levels == 1) {
sizeofbinarydata = (image->width / 8 + ((image->width % 8) ? 1 : 0)) * image->height + 1;
tmp = (unsigned char *) malloc(sizeofbinarydata);
if (tmp == NULL) return 0;
fprintf(file, "%s %d %d\n", "P4", image->width, image->height);
totalbytes = unsigned_char_to_bit(image->data, tmp, image->width, image->height);
printf("Total = %ld\n", totalbytes);
if (fwrite(tmp, sizeof(unsigned char), totalbytes, file) != totalbytes) {
#ifdef VC_DEBUG
fprintf(stderr, "ERROR -> vc_read_image():\n\tError writing PBM, PGM or PPM file.\n");
#endif
fclose(file);
free(tmp);
return 0;
}
free(tmp);
} else {
fprintf(file, "%s %d %d 255\n", (image->channels == 1) ? "P5" : "P6", image->width, image->height);
if (fwrite(image->data, image->bytesperline, image->height, file) != image->height) {
#ifdef VC_DEBUG
fprintf(stderr, "ERROR -> vc_read_image():\n\tError writing PBM, PGM or PPM file.\n");
#endif
fclose(file);
return 0;
}
}
fclose(file);
return 1;
}
return 0;
}
//
// Created by Helder Carvalho on 07/03/2021.
//
/**
* Convert a Gray image to a Negative
* @param src_dst -> Image In and Out
* @return -> 0 (Error) ou 1 (Successes)
*/
int vc_gray_negative(IVC *src_dst) {
// Error check
if ((src_dst->width <= 0) || (src_dst->height <= 0) || (src_dst->data == NULL) || (src_dst->channels != 1))
return 0;
// Generate image
int size = src_dst->width * src_dst->height * src_dst->channels;
for (int pos = 0; pos < size; pos += src_dst->channels) {
src_dst->data[pos] = 255 - src_dst->data[pos];
}
return 1;
}
/**
* Convert a Gray image to an RGB image
* @param src -> Image to convert
* @param dst -> Converted image
* @return -> 0 (Error) ou 1 (Successes)
*/
int vc_gray_to_rgb(IVC *src, IVC *dst) {
// Error check
if ((src->width <= 0) || (src->height <= 0) || (src->data == NULL) || (dst->data == NULL)) return 0;
if ((src->width != dst->width) || (src->height != dst->height)) return 0;
if ((src->channels != 1) || (dst->channels != 3)) return 0;
// Generate image
for (int y = 0; y < src->height; y++) {
for (int x = 0; x < src->width; x++) {
long int pos_src = y * src->bytesperline + x * src->channels,
pos_dst = y * dst->bytesperline + x * dst->channels;
if (src->data[pos_src] == 255) {
dst->data[pos_dst] = dst->data[pos_dst + 1] = dst->data[pos_dst + 2] = 255;
} else {
dst->data[pos_dst] = dst->data[pos_dst + 1] = dst->data[pos_dst + 2] = 0;
}
}
}
return 1;
}
/**
* Scale a Gray image to an RGB image (Temperature related)
* @param src -> Image to convert
* @param dst -> Converted image
* @return -> 0 (Error) ou 1 (Successes)
*/
int vc_scale_gray_to_rgb(IVC *src, IVC *dst) {
// Error check
if ((src->width <= 0) || (src->height <= 0) || (src->data == NULL) || (dst->data == NULL)) return 0;
if ((src->width != dst->width) || (src->height != dst->height)) return 0;
if ((src->channels != 1) || (dst->channels != 3)) return 0;
// Generate image
for (int y = 0; y < src->height; y++) {
for (int x = 0; x < src->width; x++) {
long int pos_src = y * src->bytesperline + x * src->channels,
pos_dst = y * dst->bytesperline + x * dst->channels;
int v = src->data[pos_src];
if (v < 64) {
dst->data[pos_dst] = 0;
dst->data[pos_dst + 1] = (v * 4);
dst->data[pos_dst + 2] = 255;
} else if (v < 128) {
dst->data[pos_dst] = 0;
dst->data[pos_dst + 1] = 255;
dst->data[pos_dst + 2] = 255 - (v - 64) * 4;
} else if (v < 192) {
dst->data[pos_dst] = (v - 128) * 4;
dst->data[pos_dst + 1] = 255;
dst->data[pos_dst + 2] = 0;
} else {
dst->data[pos_dst] = 255;
dst->data[pos_dst + 1] = 255 - (v - 192) * 4;
dst->data[pos_dst + 2] = 0;
}
}
}
return 1;
}
/**
* Segment a Gray image using threshold to a Gray image
* @param src -> Image to convert
* @param dst -> Converted image
* @param threshold -> Threshold to become white pixel color
* @return -> 0 (Error) ou 1 (Successes)
*/
int vc_gray_to_binary(IVC *src, IVC *dst, int threshold) {
// Error check
if ((src->width <= 0) || (src->height <= 0) || (src->data == NULL) || (dst->data == NULL) || (src->channels != 1))
return 0;
if ((src->width != dst->width) || (src->height != dst->height) || (src->channels != dst->channels)) return 0;
// Generate image
for (int pos = 0, size = src->width * src->height * src->channels; pos < size; pos += src->channels) {
if (src->data[pos] > threshold)
dst->data[pos] = 255;
else
dst->data[pos] = 0;
}
return 1;
}
/**
* Segment a Gray image using Mean threshold to a Gray image
* @param src -> Image to convert
* @param dst -> Converted image
* @return -> 0 (Error) ou 1 (Successes)
*/
int vc_gray_to_binary_global_mean(IVC *src, IVC *dst) {
// Error check
if ((src->width <= 0) || (src->height <= 0) || (src->data == NULL) || (dst->data == NULL) || (src->channels != 1))
return 0;
if ((src->width != dst->width) || (src->height != dst->height) || (src->channels != dst->channels)) return 0;
// Generate image
int threshold = 0;
for (int pos = 0, size = src->width * src->height * src->channels; pos < size; pos += src->channels) {
threshold += src->data[pos];
}
vc_gray_to_binary(src, dst, threshold / (src->width * src->height));
return 1;
}
/**
* Segment a Gray image using Neighborhood Midpoint threshold to a Gray image
* @param src -> Image to convert
* @param dst -> Converted image
* @param kernel -> Neighborhood size (in pixels)
* @return -> 0 (Error) ou 1 (Successes)
*/
int vc_gray_to_binary_neighborhood_midpoint(IVC *src, IVC *dst, int kernel) {
// Error check
if ((src->width <= 0) || (src->height <= 0) || (src->data == NULL) || (dst->data == NULL) || (src->channels != 1))
return 0;
if ((src->width != dst->width) || (src->height != dst->height) || (src->channels != dst->channels)) return 0;
// Generate image
int k_movement = (kernel - 1) / 2;
for (int x = 0; x < src->width; ++x) {
for (int y = 0; y < src->height; ++y) {
int pos = y * src->bytesperline + x * src->channels;
int v_min = src->data[pos], v_max = src->data[pos];
for (int kx = (x - k_movement), kx_max = x + k_movement; kx <= kx_max; ++kx) {
if ((kx < 0) || (kx >= src->width)) continue; // out of bounds, jump to next iteration (next "kx")
for (int ky = (y - k_movement), ky_max = y + k_movement; ky <= ky_max; ++ky) {
if ((ky < 0) || (ky >= src->height)) continue; // out of bounds, jump to next iteration (next "ky")
int pos_k = ky * src->bytesperline + kx * src->channels;
v_min = min(v_min, src->data[pos_k]);
v_max = max(v_max, src->data[pos_k]);
}
}
// ( Threshold )
if (src->data[pos] > ((v_min + v_max) / 2))
dst->data[pos] = 255;
else
dst->data[pos] = 0;
}
}
return 1;
}
/**
* Creates a Histogram of a Gray image
* @param src -> Image to create the Histogram for
* @param dst -> Histogram in image format
* @return -> 0 (Error) ou 1 (Successes)
*/
int vc_gray_histogram_show(IVC *src, IVC *dst) {
// Error check
if ((src->width <= 0) || (src->height <= 0) || (src->data == NULL) || (dst->data == NULL) || (src->channels != 1))
return 0;
if ((dst->width != 256) || (dst->height != 256)) return 0;
// Get the total quantity of pixels
int src_size = src->width * src->height * src->channels;
// Count the quantity of pixels for each brightness level
int bright_count[256] = {0};
for (int pos = 0; pos < src_size; pos += src->channels) {
// The position in bright_count is the value of the pixel brightness
bright_count[src->data[pos]]++;
}
// Calculate the Probability Density Function for each brightness level and gets the max PDF value
float pdf[256] = {0.0f}, pdf_max = 0.0f;
for (int i = 0; i <= 255; i++) {
pdf[i] = (float) bright_count[i] / (float) src_size;
pdf_max = max(pdf_max, pdf[i]);
}
// Calculate the Normalized value of each element of the Probability Density Function, between 0 and 1
float pdf_normalized[256] = {0.0f};
for (int i = 0; i <= 255; i++) {
pdf_normalized[i] = pdf[i] / pdf_max;
}
// Set all the pixels of the dst image to black
memset(dst->data, 0, dst->width * dst->height * dst->channels);
// Create the histogram
for (int x = 0; x < dst->width; x++)
for (int y = dst->height - 1;
(float) y > (float) (dst->height - 1) - pdf_normalized[x] * (float) (dst->width - 1); y--)
dst->data[y * dst->bytesperline + x] = 255;
return 1;
}
/**
* Equalizes the histogram of a Gray image
* @param src -> Image to equalize
* @param dst -> Equalized image
* @return -> 0 (Error) ou 1 (Successes)
*/
int vc_gray_histogram_equalization(IVC *src, IVC *dst) {
// Error check
if ((src->width <= 0) || (src->height <= 0) || (src->data == NULL) || (dst->data == NULL) || (src->channels != 1))
return 0;
if ((src->width != dst->width) || (src->height != dst->height) || (src->channels != dst->channels)) return 0;
// Get the total quantity of pixels (with 1 channel image it's the same as the src_size of the image)
int src_size = src->width * src->height * src->channels;
// Count the quantity of pixels for each brightness level
int bright_count[256] = {0};
for (int pos = 0; pos < src_size; pos += src->channels) {
// The position in bright_count is the value of the pixel brightness
bright_count[src->data[pos]]++;
}
// Calculate the Probability Density Function for each brightness level and the sum of the PDF to Cumulative Density
// Function (CDF).
float pdf[256] = {0.0f}, cdf[256] = {0.0f};
// First calculate for the position 0
pdf[0] = (float) bright_count[0] / (float) src_size;
cdf[0] = pdf[0];
// Now calculate for the rest of the positions
for (int i = 1; i <= 255; i++) {
pdf[i] = (float) bright_count[i] / (float) src_size;
cdf[i] = cdf[i - 1] + pdf[i];
}
// Get the first value of CDF greater than 0
float cdf_min = cdf[0];
for (int i = 0; i <= 255; i++)
if (cdf[i] > 0.0f) {
cdf_min = cdf[i];
break;
}
for (int pos = 0; pos < src_size; pos += src->channels) {
dst->data[pos] = (unsigned char) ((cdf[src->data[pos]] - cdf_min) / (1.0f - cdf_min) * 255.0f);
}
return 1;
}
/**
* Prewitt edge detection
* @param src -> Image to process
* @param dst -> Edged image
* @param threshold -> Edge threshold ]0.0; 1.0[
* @return -> 0 (Error) ou 1 (Successes)
*/
int vc_gray_edge_prewitt(IVC *src, IVC *dst, float threshold) {
// Error check
if ((src->width <= 0) || (src->height <= 0) || (src->data == NULL) || (dst->data == NULL) || (src->channels != 1))
return 0;
if ((src->width != dst->width) || (src->height != dst->height) || (src->channels != dst->channels)) return 0;
// Get the total quantity of pixels (with 1 channel image it's the same as the size of the image)
int size = src->width * src->height * src->channels;
// Set all the pixels of the dst image to black
memset(dst->data, 0, size);
// Calculate derivatives and magnitude of the vector
for (int y = 1; y < src->height - 1; y++)
for (int x = 1; x < src->width - 1; x++) {
/*
* A B C
* D X E
* F G H
*/
long int posA = (y - 1) * src->bytesperline + (x - 1) * src->channels,
posB = (y - 1) * src->bytesperline + x * src->channels,
posC = (y - 1) * src->bytesperline + (x + 1) * src->channels,
posD = y * src->bytesperline + (x - 1) * src->channels,
posX = y * src->bytesperline + x * src->channels,
posE = y * src->bytesperline + (x + 1) * src->channels,
posF = (y + 1) * src->bytesperline + (x - 1) * src->channels,
posG = (y + 1) * src->bytesperline + x * src->channels,
posH = (y + 1) * src->bytesperline + (x + 1) * src->channels;
// Derivative of x axis (left and right columns)
float sum_x = (float) ((-src->data[posA]) + (-src->data[posD]) + (-src->data[posF]) + src->data[posC] +
src->data[posE] + src->data[posH]) / 3.0f;
// Derivative of y axis (top and bottom lines)
float sum_y = (float) ((-src->data[posA]) + (-src->data[posB]) + (-src->data[posC]) + src->data[posF] +
src->data[posG] + src->data[posH]) / 3.0f;
// Calculate Magnitude
dst->data[posX] = (unsigned char) sqrtf(powf(sum_x, 2.0f) + powf(sum_y, 2));
}
// Create gray histogram
int histogram[256] = {0};
for (int y = 1; y < src->height; y++)
for (int x = 1; x < src->width; x++)
histogram[dst->data[y * src->bytesperline + x * src->channels]]++;
// Find the threshold
int histogram_threshold;
for (int i = 0, histogram_max = 0; i <= 255; i++) {
histogram_max += histogram[i];
if ((float) histogram_max >= (float) size * threshold) {
histogram_threshold = i;
break;
}
}
// Apply the threshold
vc_gray_to_binary(dst, dst, histogram_threshold);
return 1;
}
/**
* Sobel edge detection
* @param src -> Image to process
* @param dst -> Edged image
* @param threshold -> Edge threshold ]0.0; 1.0[
* @return -> 0 (Error) ou 1 (Successes)
*/
int vc_gray_edge_sobel(IVC *src, IVC *dst, float threshold) {
// Error check
if ((src->width <= 0) || (src->height <= 0) || (src->data == NULL) || (dst->data == NULL) || (src->channels != 1))
return 0;
if ((src->width != dst->width) || (src->height != dst->height) || (src->channels != dst->channels)) return 0;
// Get the total quantity of pixels (with 1 channel image it's the same as the src_size of the image)
int size = src->width * src->height * src->channels;
// Set all the pixels of the dst image to black
memset(dst->data, 0, size);
// Calculate derivatives and magnitude of the vector
for (int y = 1; y < src->height - 1; y++)
for (int x = 1; x < src->width - 1; x++) {
/*
* A B C
* D X E
* F G H
*/
long int posA = (y - 1) * src->bytesperline + (x - 1) * src->channels,
posB = (y - 1) * src->bytesperline + x * src->channels,
posC = (y - 1) * src->bytesperline + (x + 1) * src->channels,
posD = y * src->bytesperline + (x - 1) * src->channels,
posX = y * src->bytesperline + x * src->channels,
posE = y * src->bytesperline + (x + 1) * src->channels,
posF = (y + 1) * src->bytesperline + (x - 1) * src->channels,
posG = (y + 1) * src->bytesperline + x * src->channels,
posH = (y + 1) * src->bytesperline + (x + 1) * src->channels;
// Derivative of x axis (left and right columns)
float sum_x = (float) ((-src->data[posA]) + (-src->data[posD] * 2) + (-src->data[posF]) + src->data[posC] +
(src->data[posE] * 2) + src->data[posH]) / 4.0f;
// Derivative of y axis (top and bottom lines)
float sum_y = (float) ((-src->data[posA]) + (-src->data[posB] * 2) + (-src->data[posC]) + src->data[posF] +
(src->data[posG] * 2) + src->data[posH]) / 4.0f;
// Calculate Magnitude
dst->data[posX] = (unsigned char) sqrtf(powf(sum_x, 2.0f) + powf(sum_y, 2));
}
// Create gray histogram
int histogram[256] = {0};
for (int y = 1; y < src->height; y++)
for (int x = 1; x < src->width; x++)
histogram[dst->data[y * src->bytesperline + x * src->channels]]++;
// Find the threshold
int histogram_threshold;
for (int i = 0, histogram_max = 0; i <= 255; i++) {
histogram_max += histogram[i];
if ((float) histogram_max >= (float) size * threshold) {
histogram_threshold = i;
break;
}
}
// Apply the threshold
vc_gray_to_binary(dst, dst, histogram_threshold);
return 1;
}
/**
* Lowpass filter using mean
* @param src -> Image to process
* @param dst -> Filtered image
* @param kernel_size -> Neighborhood size (in pixels)
* @return -> 0 (Error) ou 1 (Successes)
*/
int vc_gray_lowpass_mean_filter(IVC *src, IVC *dst, int kernel_size) {
// Error check
if ((src->width <= 0) || (src->height <= 0) || (src->data == NULL) || (dst->data == NULL) || (src->channels != 1))
return 0;
if ((src->width != dst->width) || (src->height != dst->height) || (src->channels != dst->channels)) return 0;
// Mask size and offset
int mask_size = kernel_size * kernel_size, offset = kernel_size / 2;
// Generate image
for (int y = 0; y < src->height; y++)
for (int x = 0; x < src->width; x++) {
int pos = y * src->bytesperline + x * src->channels, average = 0;
// Process kernel
for (int ky = -offset; ky <= offset; ky++)
for (int kx = -offset; kx <= offset; kx++)
if ((y + ky >= 0) && (y + ky < src->height) && (x + kx >= 0) && (x + kx < src->width)) {
average += src->data[(y + ky) * src->bytesperline + (x + kx) * src->channels];
}
average /= mask_size;
dst->data[pos] = average;
}
return 1;
}
/**
* Lowpass filter using median
* @param src -> Image to process
* @param dst -> Filtered image
* @param kernel_size -> Neighborhood size (in pixels)
* @return -> 0 (Error) ou 1 (Successes)
*/
int vc_gray_lowpass_median_filter(IVC *src, IVC *dst, int kernel_size) {
// Error check
if ((src->width <= 0) || (src->height <= 0) || (src->data == NULL) || (dst->data == NULL) || (src->channels != 1))
return 0;
if ((src->width != dst->width) || (src->height != dst->height) || (src->channels != dst->channels)) return 0;
// Mask data array, offset, mask size and kernel size pair
int *mask_data = calloc(kernel_size * kernel_size, sizeof(int)), offset = (kernel_size / 2), mask_size =
kernel_size * kernel_size, kernel_size_pair = (((kernel_size * kernel_size) + 1) / 2);
// Generate image
for (int y = 0; y < src->height; y++)
for (int x = 0; x < src->width; x++) {
int pos = y * src->bytesperline + x * src->channels, qty = 0;
// Process kernel
for (int ky = -offset; ky <= offset; ky++)
for (int kx = -offset; kx <= offset; kx++)
if ((y + ky >= 0) && (y + ky < src->height) && (x + kx >= 0) && (x + kx < src->width)) {
mask_data[qty] = src->data[(y + ky) * src->bytesperline + (x + kx) * src->channels];
qty++;
}
if (qty != mask_size)
kernel_size_pair = ((qty + 1) / 2);
// Sort mask_data[] by ascendant order
for (int i = 0; i < qty - 1; i++)
for (int j = i + 1; j < qty; j++)
if (mask_data[j] < mask_data[i]) {
int tmp = mask_data[i];
mask_data[i] = mask_data[j];
mask_data[j] = tmp;
}
dst->data[pos] = mask_data[kernel_size_pair];
}
free(mask_data);
return 1;
}
/**
* Lowpass filter using gaussian (Fixed kernel size of 5)
* @param src -> Image to process
* @param dst -> Filtered image
* @return -> 0 (Error) ou 1 (Successes)
*/
int vc_gray_lowpass_gaussian_filter(IVC *src, IVC *dst) {
// Error check
if ((src->width <= 0) || (src->height <= 0) || (src->data == NULL) || (dst->data == NULL) || (src->channels != 1))
return 0;
if ((src->width != dst->width) || (src->height != dst->height) || (src->channels != dst->channels)) return 0;
// Kernel
int gauss[5][5] = {
{1, 4, 7, 4, 1},
{4, 16, 26, 16, 4},
{7, 26, 41, 26, 7},
{4, 16, 26, 16, 4},
{1, 4, 7, 4, 1}
}, gaus_sum;
for (int y = 0; y < src->height; y++) {
for (int x = 0; x < src->width; x++) {
gaus_sum = 0;
// The 5 is Kernel Size
for (int y_kernel = 0; y_kernel < 5; y_kernel++) {
for (int x_kernel = 0; x_kernel < 5; x_kernel++) {
long int pos_kernel = (y - y_kernel) * src->bytesperline + (x - x_kernel) * src->channels;
gaus_sum += (src->data[pos_kernel] * gauss[y_kernel][x_kernel]) / 273;
}
}
long int pos = y * src->bytesperline + x * src->channels;
dst->data[pos] = gaus_sum;
}
}
return 1;
}
/**
* Highpass filter
* @param src -> Image to process
* @param dst -> Filtered image
* @param mask_type -> Type of the mask (Accepts 1, 2 or 3)
* @return -> 0 (Error) ou 1 (Successes)
*/
int vc_gray_highpass_filter(IVC *src, IVC *dst, int mask_type) {
// Error check
if ((src->width <= 0) || (src->height <= 0) || (src->data == NULL) || (dst->data == NULL) || (src->channels != 1))
return 0;
if ((src->width != dst->width) || (src->height != dst->height) || (src->channels != dst->channels)) return 0;
// Mask
int mask[3][3], mask_sum, div;
switch (mask_type) {
default: {
return 0;
}
case 1: {
/*
* {0, -1, 0}
* {-1, 4, -1}
* {0, -1, 0}
*/
mask[0][0] = mask[0][2] = mask[2][0] = mask[2][2] = 0;
mask[0][1] = mask[1][0] = mask[1][2] = mask[2][1] = -1;
mask[1][1] = 4;
div = 6;
break;
}
case 2: {
/*
* {-1, -1, -1}
* {-1, 8, -1}
* {-1, -1, -1}
*/
mask[0][0] = mask[0][1] = mask[0][2] = mask[1][0] = mask[1][2] = mask[2][0] = mask[2][1] = mask[2][2] = -1;
mask[1][1] = 8;
div = 9;
break;
}
case 3: {
/*
* {-1, -2, -1}
* {-2, 12, -2}
* {-1, -2, -1}
*/
mask[0][0] = mask[0][2] = mask[2][0] = mask[2][2] = -1;
mask[0][1] = mask[1][0] = mask[1][2] = mask[2][1] = -2;
mask[1][1] = 12;
div = 16;
break;
}
}
for (int y = 0; y < src->height; y++) {
for (int x = 0; x < src->width; x++) {
mask_sum = 0;
long int pos = y * src->bytesperline + x * src->channels;
mask_sum += (src->data[(y - 1) * src->bytesperline + (x - 1) * src->channels] * mask[0][0]);
mask_sum += (src->data[(y - 1) * src->bytesperline + (x) * src->channels] * mask[0][1]);
mask_sum += (src->data[(y - 1) * src->bytesperline + (x + 1) * src->channels] * mask[0][2]);
mask_sum += (src->data[(y) * src->bytesperline + (x - 1) * src->channels] * mask[1][0]);
mask_sum += (src->data[pos] * mask[1][1]);
mask_sum += (src->data[(y) * src->bytesperline + (x + 1) * src->channels] * mask[1][2]);
mask_sum += (src->data[(y + 1) * src->bytesperline + (x - 1) * src->channels] * mask[2][0]);
mask_sum += (src->data[(y + 1) * src->bytesperline + (x) * src->channels] * mask[2][1]);
mask_sum += (src->data[(y + 1) * src->bytesperline + (x + 1) * src->channels] * mask[2][2]);
dst->data[pos] = abs(mask_sum) / div;
}
}
return 1;
}
/**
* Highpass filter with enhancement
* @param src -> Image to process
* @param dst -> Filtered image
* @param mask_type -> Type of the mask (Accepts 1, 2 or 3)
* @param gain -> Enhance gain amount
* @return -> 0 (Error) ou 1 (Successes)
*/
int vc_gray_highpass_filter_enhance(IVC *src, IVC *dst, int mask_type, int gain) {
// Error check
if ((src->width <= 0) || (src->height <= 0) || (src->data == NULL) || (dst->data == NULL) || (src->channels != 1))
return 0;
if ((src->width != dst->width) || (src->height != dst->height) || (src->channels != dst->channels)) return 0;
// Mask
int mask[3][3], mask_sum, div;
switch (mask_type) {
default: {
return 0;
}
case 1: {
/*
* {0, -1, 0}
* {-1, 4, -1}
* {0, -1, 0}
*/
mask[0][0] = mask[0][2] = mask[2][0] = mask[2][2] = 0;
mask[0][1] = mask[1][0] = mask[1][2] = mask[2][1] = -1;
mask[1][1] = 4;
div = 6;
break;
}
case 2: {
/*
* {-1, -1, -1}
* {-1, 8, -1}
* {-1, -1, -1}
*/
mask[0][0] = mask[0][1] = mask[0][2] = mask[1][0] = mask[1][2] = mask[2][0] = mask[2][1] = mask[2][2] = -1;
mask[1][1] = 8;
div = 9;
break;
}
case 3: {
/*
* {-1, -2, -1}
* {-2, 12, -2}
* {-1, -2, -1}
*/
mask[0][0] = mask[0][2] = mask[2][0] = mask[2][2] = -1;
mask[0][1] = mask[1][0] = mask[1][2] = mask[2][1] = -2;
mask[1][1] = 12;
div = 16;
break;
}
}
for (int y = 0; y < src->height; y++) {
for (int x = 0; x < src->width; x++) {
mask_sum = 0;
long int pos = y * src->bytesperline + x * src->channels;
mask_sum += (src->data[(y - 1) * src->bytesperline + (x - 1) * src->channels] * mask[0][0]);
mask_sum += (src->data[(y - 1) * src->bytesperline + (x) * src->channels] * mask[0][1]);
mask_sum += (src->data[(y - 1) * src->bytesperline + (x + 1) * src->channels] * mask[0][2]);
mask_sum += (src->data[(y) * src->bytesperline + (x - 1) * src->channels] * mask[1][0]);
mask_sum += (src->data[pos] * mask[1][1]);
mask_sum += (src->data[(y) * src->bytesperline + (x + 1) * src->channels] * mask[1][2]);