-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patha1.cpp
More file actions
executable file
·1279 lines (1111 loc) · 33.2 KB
/
Copy patha1.cpp
File metadata and controls
executable file
·1279 lines (1111 loc) · 33.2 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
#include <SImage.h>
#include <SImageIO.h>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <queue>
#include <utility>
#include <DrawText.h>
#include <set>
using namespace std;
#define PI 3.14159
// The simple image class is called SDoublePlane, with each pixel represented as
// a double (floating point) type. This means that an SDoublePlane can represent
// values outside the range 0-255, and thus can represent squared gradient magnitudes,
// harris corner scores, etc.
//
// The SImageIO class supports reading and writing PNG files. It will read in
// a color PNG file, convert it to grayscale, and then return it to you in
// an SDoublePlane. The values in this SDoublePlane will be in the range [0,255].
//
// To write out an image, call write_png_file(). It takes three separate planes,
// one for each primary color (red, green, blue). To write a grayscale image,
// just pass the same SDoublePlane for all 3 planes. In order to get sensible
// results, the values in the SDoublePlane should be in the range [0,255].
//
// Below is a helper functions that overlays rectangles
// on an image plane for visualization purpose.
// Draws a rectangle on an image plane, using the specified gray level value and line width.
//
void overlay_rectangle(SDoublePlane &input, int _top, int _left, int _bottom, int _right, double graylevel, int width)
{
for(int w=-width/2; w<=width/2; w++) {
int top = _top+w, left = _left+w, right=_right+w, bottom=_bottom+w;
// if any of the coordinates are out-of-bounds, truncate them
top = min( max( top, 0 ), input.rows()-1);
bottom = min( max( bottom, 0 ), input.rows()-1);
left = min( max( left, 0 ), input.cols()-1);
right = min( max( right, 0 ), input.cols()-1);
// draw top and bottom lines
for(int j=left; j<=right; j++)
input[top][j] = input[bottom][j] = graylevel;
// draw left and right lines
for(int i=top; i<=bottom; i++)
input[i][left] = input[i][right] = graylevel;
}
}
// DetectedSymbol class may be helpful!
// Feel free to modify.
//
typedef enum {NOTEHEAD=0, QUARTERREST=1, EIGHTHREST=2} Type;
class DetectedSymbol {
public:
int row, col, width, height;
Type type;
char pitch;
double confidence;
};
// Function that outputs the ascii detection output file
void write_detection_txt(const string &filename, const vector<struct DetectedSymbol> &symbols)
{
ofstream ofs(filename.c_str());
for(int i=0; i<symbols.size(); i++)
{
const DetectedSymbol &s = symbols[i];
ofs << s.row << " " << s.col << " " << s.width << " " << s.height << " ";
if(s.type == NOTEHEAD)
ofs << "filled_note " << s.pitch;
else if(s.type == EIGHTHREST)
ofs << "eighth_rest _";
else
ofs << "quarter_rest _";
ofs << " " << s.confidence << endl;
}
}
void write_image(const string &filename, const SDoublePlane &input)
{
SDoublePlane output_planes[3];
for(int i=0; i<3; i++)
{
output_planes[i] = input;
}
SImageIO::write_png_file(filename.c_str(), output_planes[0], output_planes[1], output_planes[2]);
}
double image_max(const SDoublePlane &input)
{
double max=0;
for (int i = 0; i < input.rows(); ++i)
{
for (int j = 0; j < input.cols(); ++j)
{
if (input[i][j] > max)
{
max = input[i][j];
}
}
}
return max;
}
double image_min(const SDoublePlane &input)
{
double min=0;
for (int i = 0; i < input.rows(); ++i)
{
for (int j = 0; j < input.cols(); ++j)
{
if (input[i][j] < min)
{
min = input[i][j];
}
}
}
return min;
}
SDoublePlane normalize_image(const SDoublePlane &input)
{
SDoublePlane output(input);
double max = image_max(output);
double min = image_min(output);
for (int i = 0; i < input.rows(); ++i)
{
for (int j = 0; j < input.cols(); ++j)
{
output[i][j] = (output[i][j] - min) / (max - min) * 255;
}
}
return output;
}
SDoublePlane complement_image(const SDoublePlane &input)
{
SDoublePlane output(input);
for (int i = 0; i < input.rows(); ++i)
{
for (int j = 0; j < input.cols(); ++j)
{
output[i][j] = 255 - output[i][j];
}
}
return output;
}
SDoublePlane scale_image(const SDoublePlane &input, double ratio)
{
int m = input.rows();
int n = input.cols();
int m2 = input.rows()*ratio;
int n2 = input.cols()*ratio;
SDoublePlane output(m2, n2);
if (ratio > 0.5)
{
for (int i = 0; i < m2; i++)
{
int sk = i/ratio;
int ek = (i + 1)/ratio - 0.00001;
for (int j = 0; j < n2; j++)
{
int sl = j/ratio;
int el = (j + 1)/ratio - 0.00001;
output[i][j] = input[sk][sl];
output[i][j] += input[sk][el];
output[i][j] += input[ek][sl];
output[i][j] += input[ek][el];
output[i][j] /= 4.0;
}
}
}
else
{
int span = 1.0/ratio + 0.5;
for (int i = 0; i < m2; i++)
{
int sk = i/ratio;
int ek = (i - 1)/ratio - 0.00001;
for (int j = 0; j < n2; j++)
{
int sl = j/ratio;
int el = (j - 1)/ratio - 0.00001;
output[i][j] = 0;
for (int u = sk; u <= ek; u++)
{
for (int v = sl; v <= el; v++)
{
output[i][j] += input[u][v];
}
}
output[i][j] /= (ek - sk + 1)*(el - sl + 1);
}
}
}
return output;
}
// Function that outputs a visualization of detected symbols
void write_detection_image(const string &filename, const vector<DetectedSymbol> &symbols, const SDoublePlane &input)
{
SDoublePlane output_planes[3];
for(int i=0; i<3; i++)
output_planes[i] = input;
for(int i=0; i<symbols.size(); i++)
{
const DetectedSymbol &s = symbols[i];
overlay_rectangle(output_planes[s.type], s.row, s.col, s.row+s.height-1, s.col+s.width-1, 255, 2);
overlay_rectangle(output_planes[(s.type+1) % 3], s.row, s.col, s.row+s.height-1, s.col+s.width-1, 0, 2);
overlay_rectangle(output_planes[(s.type+2) % 3], s.row, s.col, s.row+s.height-1, s.col+s.width-1, 0, 2);
if(s.type == NOTEHEAD)
{
char str[] = {s.pitch, 0};
draw_text(output_planes[0], str, s.row, s.col+s.width+1, 0, 2);
draw_text(output_planes[1], str, s.row, s.col+s.width+1, 0, 2);
draw_text(output_planes[2], str, s.row, s.col+s.width+1, 0, 2);
}
}
SImageIO::write_png_file(filename.c_str(), output_planes[0], output_planes[1], output_planes[2]);
}
// The rest of these functions are incomplete. These are just suggestions to
// get you started -- feel free to add extra functions, change function
// parameters, etc.
//
//
// Print the value of the image
//
void print_image_value1(const SDoublePlane &input)
{ int sum=0;
for (int i = 0; i < input.cols(); ++i)
{ sum=0;
for (int j = 0; j < input.rows(); ++j)
{ sum=sum+input[j][i];
//cout << input[i][j] << " ";
}
//cout << endl;
cout<<sum<<"|||"<<i<<endl;
}
}
void print_image_value(const SDoublePlane &input)
{
for (int i = 0; i < input.rows(); ++i)
{
for (int j = 0; j < input.cols(); ++j)
{
cout << input[i][j] << " ";
}
cout << endl;
}
}
// compare two image values
// returns true if they are similar, false otherwise
bool compare_image_value(const SDoublePlane &image1, const SDoublePlane &image2)
{
if (image1.rows() != image2.rows() || image1.cols() != image2.cols())
return false;
for (int i = 0; i < image1.rows(); ++i)
{
for (int j = 0; j < image1.cols(); ++j)
{
if (image1[i][j] != image2[i][j])
return false;
}
}
return true;
}
// Convolve an image with a separable convolution kernel
SDoublePlane convolve_separable(const SDoublePlane &input, const SDoublePlane &row_filter, const SDoublePlane &col_filter)
{
SDoublePlane output(input.rows(), input.cols());
SDoublePlane temp(input.rows(), input.cols());
// convolve with row filter
for (int i = 0; i < input.rows(); ++i)
{
for (int j = 0; j < input.cols(); ++j)
{
double sum = 0.0;
for (int k = 0; k < row_filter.cols(); ++k)
{
int l = j + row_filter.cols()/2 - k;
// Doing reflection if out of boundary
if (l < 0) l = -l;
if (l >= input.cols()) l = 2 * input.cols() - l - 1;
sum += row_filter[0][k] * input[i][l];
}
temp[i][j] = sum;
}
}
// convolve with col filter
for (int i = 0; i < input.rows(); ++i)
{
for (int j = 0; j < input.cols(); ++j)
{
double sum = 0.0;
for (int k = 0; k < col_filter.rows(); ++k)
{
int l = i + col_filter.rows()/2 - k;
// Doing reflection if out of boundary
if (l < 0) l = -l;
if (l >= input.rows()) l = 2 * input.rows() - l - 1;
sum += col_filter[k][0] * temp[l][j];
}
output[i][j] = sum;
}
}
return output;
}
// Convolve an image with a general convolution kernel
//
SDoublePlane convolve_general(const SDoublePlane &input, const SDoublePlane &filter)
{
SDoublePlane output(input.rows(), input.cols());
// Convolution code here
int pixel_value=0;
int frow2,fcol2,frow,fcol;
for(int irow=0;irow<input.rows();irow++){
for(int icol=0;icol<input.cols();icol++){
pixel_value=0;
for(frow=-filter.rows()/2,frow2=0;frow<=filter.rows()/2;frow++,frow2++){
for(fcol=-filter.cols()/2,fcol2=0;fcol<=filter.cols()/2;fcol++,fcol2++){
if( (irow+frow) >= 0 && (icol+fcol) >= 0 && (irow+frow) < input.rows() && (icol+fcol) < input.cols()){
pixel_value=pixel_value+(input[irow+frow][icol+fcol] * filter[frow2][fcol2]);
}
}
}
output[irow][icol]=pixel_value;
}
}
return output;
}
// Apply a sobel operator to an image, returns the result
// _gx=true for horizontal gradient, false for vertical gradient
SDoublePlane sobel_gradient_filter(const SDoublePlane &input, bool _gx)
{
SDoublePlane output(input.rows(), input.cols());
SDoublePlane row_filter(1, 3), col_filter(3, 1);
if (_gx)
{
row_filter[0][0] = -1.0;
row_filter[0][1] = 0.0;
row_filter[0][2] = 1.0;
col_filter[0][0] = 1.0/8.0;
col_filter[1][0] = 2.0/8.0;
col_filter[2][0] = 1.0/8.0;
}
else
{
row_filter[0][0] = 1.0/8.0;
row_filter[0][1] = 2.0/8.0;
row_filter[0][2] = 1.0/8.0;
col_filter[0][0] = 1.0;
col_filter[1][0] = 0.0;
col_filter[2][0] = -1.0;
}
SDoublePlane sobel = convolve_separable(input, row_filter, col_filter);
return sobel;
}
// Apply an edge detector to an image, returns the binary edge map
// Pass thresh=0 to ignore binary map, else pass thresh [1-255]
// white_value only applies when thresh!=0, pass 1 for 0-1 image, or 255 for 0-255 binary image
// Returns edge_map and gradient_angle
pair<SDoublePlane, SDoublePlane> find_edges(const SDoublePlane &input, double thresh=0, double white_value=1)
{
SDoublePlane G(input.rows(), input.cols());
SDoublePlane Rotation(input.rows(), input.cols());
SDoublePlane Gx, Gy;
Gx = sobel_gradient_filter(input, true);
Gy = sobel_gradient_filter(input, false);
for (int i = 0; i < input.rows(); ++i)
{
for (int j = 0; j < input.cols(); ++j)
{
G[i][j] = sqrt(Gx[i][j]*Gx[i][j]+Gy[i][j]*Gy[i][j]);
if (G[i][j] > 255) G[i][j] = 255;
if ( abs(Gx[i][j]) < 0.0001)
Rotation[i][j] = PI / 2.0;
else
Rotation[i][j] = atan(Gy[i][j] / Gx[i][j]);
}
}
if (abs(thresh) > 0.0001)
{
for (int i = 0; i < G.rows(); ++i)
for (int j = 0; j < G.cols(); ++j)
G[i][j] = (G[i][j]>thresh?white_value:0);
}
return make_pair(G, Rotation);
}
SDoublePlane create_gaussian_filter(int size, double sigma)
{
SDoublePlane filter(size, size);
if (size % 2 == 0)
{
printf("Gaussian filter size must be odd.\n");
return filter;
}
for(int i = -size/2; i <= size/2; i++)
{
for(int j = -size/2; j <= size/2; j++)
{
filter[i+size/2][j+size/2] = 1.0/(2.0*PI*sigma*sigma)*exp(-1.0*(i*i+j*j)/(2*sigma*sigma));
}
}
return filter;
}
SDoublePlane edge_thinning_non_maximum_suppress(const pair<SDoublePlane, SDoublePlane> &edge, const double threshold,
const double range, double white_value=1, bool double_pass=true)
{
SDoublePlane gradient_value = edge.first;
SDoublePlane gradient_angle = edge.second;
SDoublePlane edge_map(gradient_value.rows(), gradient_value.cols());
for (int i = 0; i < gradient_value.rows(); ++i)
{
for (int j = 0; j < gradient_value.cols(); ++j)
{
if (gradient_value[i][j] < threshold)
{
edge_map[i][j] = 0;
continue;
}
bool is_local_maxima = true;
for (double d = -range/2.0; d <= range/2.0; ++d)
{
if ( abs(d) < 0.0001)
continue;
int r = round( i - d * sin(gradient_angle[i][j]) );
int c = round( j + d * cos(gradient_angle[i][j]) );
if (r < 0 || c < 0 || r >= gradient_value.rows() || c >= gradient_value.cols())
break;
if (gradient_value[r][c] > gradient_value[i][j] + 0.0001)
{
is_local_maxima = false;
break;
}
}
edge_map[i][j] = is_local_maxima?white_value:0;
}
}
if (double_pass == true)
{
for (int i = 0; i < edge_map.rows(); ++i)
{
for (int j = 0; j < edge_map.cols(); ++j)
{
if (abs(edge_map[i][j]) < 0.001)
{
continue;
}
double avgr = i, avgc = j, cnt = 1;
edge_map[i][j] = 0;
for (double d = -range/2.0; d <= range/2.0; ++d)
{
int r = round( i - d * sin(gradient_angle[i][j]) );
int c = round( j + d * cos(gradient_angle[i][j]) );
if (r < 0 || c < 0 || r >= edge_map.rows() || c >= edge_map.cols() || (r == i && j == c))
continue;
if ( edge_map[r][c] > 0.0001 )
{
// If there gradient angle is different, skip
if ( abs(gradient_angle[i][j]-gradient_angle[r][c]) > 0.1)
break;
edge_map[r][c] = 0;
avgr += r;
avgc += c;
cnt++;
}
}
edge_map[int(avgr/cnt)][int(avgc/cnt)] = white_value;
}
}
}
return edge_map;
}
struct compare_priority_queue
{
bool operator()(const pair<int,double> &lhs, const pair<int,double> &rhs) const
{
return rhs.second < lhs.second;
}
};
SDoublePlane compute_distance_matrix(SDoublePlane &edge_map)
{
SDoublePlane D(edge_map.rows(), edge_map.cols());
// Do a dijkstra in O(nlgn), n=total number of pixel in edge_map
priority_queue< pair<int,double>, vector< pair<int,double> >, compare_priority_queue> Q;
const int n_col = D.cols();
const int n_row = D.rows();
for (int i = 0; i < n_row; ++i)
{
for (int j = 0; j < n_col; ++j)
{
if ( edge_map[i][j] > 0.1)
{
D[i][j] = 0;
Q.push(make_pair(i*n_col+j, 0.0));
}
else
D[i][j] = -1;
}
}
while (Q.empty() == false)
{
pair<int,double> u = Q.top();
int row = u.first / n_col;
int col = u.first % n_col;
double w;
Q.pop();
for (int i = -1; i <= 1; ++i)
{
for (int j = -1; j <= 1; ++j)
{
if (row+i<0 || row+i>=n_row || col+j<0 || col+j>=n_col || (i==0 && j==0))
continue;
w = (i*j==0?1:1.414);
if ( abs(D[row+i][col+j]+1) < 0.0001 || D[row+i][col+j] > D[row][col] + w)
{
D[row+i][col+j] = D[row][col] + w;
Q.push( make_pair( (row+i)*n_col+(col+j), D[row+i][col+j] ) );
}
}
}
}
return D;
}
// Match template using edge detection method
vector<DetectedSymbol> match_template_by_edge(const SDoublePlane &input, const vector<SDoublePlane> &template_image,
double edge_threshold, vector<double> &template_threshold)
{
// Compute binary edge map with threshold value
pair<SDoublePlane, SDoublePlane> simple_edge = find_edges(input);
SDoublePlane edge_map = edge_thinning_non_maximum_suppress(simple_edge, edge_threshold, template_image[0].cols()/2.86, 255, true);
write_image("edges.png", simple_edge.first);
write_image("edges_thin.png", edge_map);
// Compute D: min distance to an edge pixel for all (i,j) in edge_map
SDoublePlane D = compute_distance_matrix(edge_map);
SDoublePlane score(input.rows(), input.cols());
vector<DetectedSymbol> symbols;
for (int template_type = 0; template_type < template_image.size(); ++template_type)
{
SDoublePlane edge_map_template = edge_thinning_non_maximum_suppress(find_edges(template_image[template_type]), edge_threshold, template_image[0].cols()/2.86, 1, true);
SDoublePlane D_template = compute_distance_matrix(edge_map_template);
for (int i = 0; i < input.rows()-template_image[template_type].rows()+1; ++i)
{
for (int j = 0; j < input.cols()-template_image[template_type].cols()+1; ++j)
{
score[i][j] = 0;
for (int k = 0; k < template_image[template_type].rows(); ++k)
{
for (int l = 0; l < template_image[template_type].cols(); ++l)
{
if (edge_map_template[k][l] > 0.001)
score[i][j] += edge_map_template[k][l] * D[i+k][j+l];
else
score[i][j] += abs( D[i+k][j+l] - D_template[k][l]);
}
}
bool skip_match = false;
for (vector<DetectedSymbol>::iterator it = symbols.begin(); it != symbols.end(); ++it)
{
// Skip nearby matched to avoid double detection
if (it->confidence > 0.5 && it->type == template_type && abs(it->row - i) < it->height*0.4
&& abs(it->col - j) < it->width*0.5)
{
skip_match = true;
break;
}
}
if (skip_match == true)
continue;
DetectedSymbol s;
s.row = i;
s.col = j;
s.width = template_image[template_type].cols();
s.height = template_image[template_type].rows();
s.type = (Type) (template_type);
s.confidence = 0.5+0.5*(template_threshold[template_type]-score[i][j])/template_threshold[template_type];
s.pitch = 'A';
if (s.confidence > 0.4)
symbols.push_back(s);
// Skip a few next columns
if (s.confidence > 0.5)
j += s.width * 0.5;
}
}
}
return symbols;
}
//find max votes from accumulator
double find_max_vote(const SDoublePlane &acc)
{
double max=0;
for(int i=0;i<acc.rows();i++){
if(acc[i][0] > max)max=acc[i][0];
}
return max;
}
//find min votes from accumulator
double find_min_vote(const SDoublePlane &acc)
{
double min=acc[0][0];
for(int i=1;i<acc.rows();i++){
if(acc[i][0] < min ) min=acc[i][0];
}
return min;
}
//Do Min Max Normalization on the votes of accumulator
SDoublePlane normalize_votes(const SDoublePlane &acc)
{
SDoublePlane normalized(acc.rows(),acc.cols());
double min = find_min_vote(acc);
double max = find_max_vote(acc);
for(int i=0;i<acc.rows();i++){
normalized[i][0] = (acc[i][0] - min)/(max-min);
}
return normalized;
}
//for every first line of the staff set the other 4 lines of staff using the best spacing found
SDoublePlane set_staff(const SDoublePlane &row_votes,int best_space,int intercept_value,int staff_number)
{
set<int> row_nums;
int last_row=0;
for(int i=0;i<5;i++){
row_nums.insert(intercept_value + (i*best_space) );
last_row=intercept_value+(i*best_space);
}
for(int j=staff_number;j<=last_row;j++){
if(j <row_votes.rows()){
if(row_nums.count(j) == 1){
row_votes[j][best_space]=255;
}
else{
row_votes[j][best_space]=0;
}
}
}
return row_votes;
}
//using the normalized votes find the best row co-ordinates for staff lines
SDoublePlane find_best_line_intercepts(const SDoublePlane &row_votes,const SDoublePlane &normed_votes,int best_space,double norm_threshold=0.55,int neighbour_threshold=4,int start=0)
{
SDoublePlane row_spacing=row_votes;
if(start < row_votes.rows()){
SDoublePlane staff_lines(row_votes.rows(),1);
int i=0;
double best_value=0;
int intercept_value=0;
while(i<row_votes.rows()){
if(normed_votes[i][0] > norm_threshold){
best_value=normed_votes[i][0];
intercept_value=i;
for(int j=1;j<neighbour_threshold;j++){
if(normed_votes[i+j][0] > best_value ){
best_value=normed_votes[i+j][0];
intercept_value=i+j;
}
}
row_spacing=set_staff(row_spacing,best_space,intercept_value,start);
i=intercept_value+(4*(best_space))+neighbour_threshold;
start=intercept_value+(4*best_space)+neighbour_threshold;
}
i++;
}
}
return row_spacing;
}
//from the row co-ordinates/best space matrix find the space parameter with high votes
int find_best_spacing(const SDoublePlane &row_spacing)
{
long max=0,sum=0;
int best_space=0;
for(int i=2;i<row_spacing.cols();i++){
sum=0;
for(int j=0;j<row_spacing.rows();j++){
sum=sum+row_spacing[j][i];
}
if(sum > max){
max=sum;
best_space=i;
}
}
return best_space;
}
bool is_max_in_neighbour_for_hough(const SDoublePlane &input, int y, int x, int w, int h)
{
int hw = w / 2;
int hh = h / 2;
for (int i = -hh; i <= hh; i++)
{ int k = y + i;
for (int j = -hw; j <= hw; j++)
{
int l = x + j;
if (k < 0 || k >= input.rows())
{
continue;
}
if (l < 0 || l >= input.cols())
{
continue;
}
if (k == y && l == x)
{
continue;
}
if (input[k][l] < input[y][x] )
{
return false;
}
}
}
return true;
}
SDoublePlane non_maximum_suppress_for_hough(const SDoublePlane &input, int w, int h)
{
double threshold = 170;
SDoublePlane output(input.rows(), input.cols());
for (int i = 0; i < input.rows(); i++)
{
for (int j = 0; j < input.cols(); j++)
{
if (input[i][j] < threshold &&
is_max_in_neighbour_for_hough(input, i, j, w, h))
{
output[i][j] = 255;
}
else
{
output[i][j] = 0;
}
}
}
return output;
}
//return a pair of 1-d SDoublePlane with 255 set for staff lines and integer representing distance between the staff lines
//max_suppress = 1 to do a line thinning before hough, 0 otherwise
pair<SDoublePlane,int> hough_transform(const SDoublePlane &input,int max_suppress,double threshold=0.001,int neighborhood=4,double norm_threshold=0.55)
{
SDoublePlane edges(input.rows(),input.cols());
if(max_suppress == 1){
edges=non_maximum_suppress_for_hough(input,0,2);
}
else{
edges=input;
}
SDoublePlane accumulator(edges.rows(),1);
SDoublePlane row_spacing(edges.rows(),edges.rows());
for(int i=0;i<edges.rows();i++){
for(int j=0;j<edges.cols();j++){
if(edges[i][j] > threshold ){
accumulator[i][0]=accumulator[i][0] + 1;
}
}
}
SDoublePlane normed_votes=normalize_votes(accumulator);
int cur=0,count=0;
for(int i=0;i<edges.rows();i++){
for(int j=0;j<edges.cols();j++){
if(edges[i][j] > threshold && normed_votes[i][0] > norm_threshold){
count=0;
for(int z=i-1;z>0;z--){
if(normed_votes[z][0]>=norm_threshold && abs(i-z) > neighborhood && z!=i && count<1){cur=i;count++;row_spacing[z][abs(i-z)]++;}
}
}
}
}
int best_space=find_best_spacing(row_spacing);
SDoublePlane best_row_intercepts= find_best_line_intercepts(row_spacing,normed_votes,best_space);
for(int i=0;i<best_row_intercepts.rows();i++){
accumulator[i][0]=best_row_intercepts[i][best_space];
}
return make_pair(accumulator,best_space);
}
//draw lines on the image after hough transform
SDoublePlane get_lines(const SDoublePlane &acc,const SDoublePlane &input,int rgb)
{
SDoublePlane lines = input;
if(rgb==1){
for(int i=0;i<acc.rows();i++){
if(acc[i][0] == 255){
for(int j=0;j<input.cols();j++){
lines[i][j]=255;
}
}
}}
else{
for(int i=0;i<acc.rows();i++){
if(acc[i][0] == 255){
for(int j=0;j<input.cols();j++){
lines[i][j]=0;
}
}
}}
return lines;
}
// Get Hamming distance map
SDoublePlane get_Hamming_distance(const SDoublePlane &input, const SDoublePlane &target)
{
SDoublePlane output(input.rows(), input.cols());
// change to convolution function later
for (int i = 0; i < input.rows(); i++)
{
for (int j = 0; j < input.cols(); j++)
{
double sum = 0;
for (int u = 0; u < target.rows(); u++)
{
for (int v = 0; v < target.cols(); v++)
{
int k = i + u, l = j + v;
if (k >= input.rows())
{
k = input.rows() - 1 - (k - input.rows() + 1);
}
if (l >= input.cols())
{
l = input.cols() - 1 - (l - input.cols() + 1);
}
double a = input[k][l] / 255;
double b = target[u][v] / 255;
sum += a * b;
sum += (1 - a) * (1 - b);
}
}
output[i][j] = sum / (target.rows() * target.cols()) * 255;
}
}
return output;
}
double plane_max(const SDoublePlane &input)
{
double max = 0;
for (int i = 0; i < input.rows(); i++)
{
for (int j = 0; j < input.cols(); j++)
{
if (input[i][j] > max)
{
max = input[i][j];
}
}
}
return max;
}
bool is_max_in_neighbour(const SDoublePlane &input, int y, int x, int w, int h)
{
int hw = w / 2;
int hh = h / 2;
for (int i = -hh; i <= hh; i++)
{
for (int j = -hw; j <= hw; j++)
{
int k = y + i;
int l = x + j;
if (k < 0 || k >= input.rows())
{
continue;
}
if (l < 0 || l >= input.cols())
{
continue;
}
if (k == y && l == x)
{
continue;
}
if (input[k][l] > input[y][x])
{
return false;
}
else if (input[k][l] == input[y][x])
{
if (i < 0 || j < 0)
{
return false;
}
}
}