-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.cpp
377 lines (307 loc) · 12.6 KB
/
pipeline.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include <algorithm>
#include <opencv2/opencv.hpp>
#include <data.h>
#include <pipeline.h>
cv::Mat initialBinarize(cv::Mat image);
cv::Mat isolateStaffs(cv::Mat bin_image);
cv::Mat isolateNotes(cv::Mat bin_image);
cv::Mat horizontalHoughSegments(cv::Mat horiz_line_image);
std::vector<cv::Vec4i> completeHorizontalLines(cv::Mat horiz_line_sements);
std::vector<int> sortedYPositions(std::vector<cv::Vec4i> lines);
std::vector<std::vector<int> > clusterStaves(std::vector<int> line_ys, int img_columns);
std::vector<cv::Rect> noteRectangles(cv::Mat binary_notes);
int intersectingLineIndex(cv::Rect note_bounds, const std::vector<int>& staff);
bool rectsOverlap(const cv::Rect& a, const cv::Rect& b);
Song readImage(const char *fpath) {
std::cout << "entered pipeline" << std::endl;
std::cout << fpath << std::endl;
cv::Mat image;
image = cv::imread(fpath);
Song song;
//scales big phone pictures.
cv::resize(image, image, cv::Size(770, 988));
cv::Mat binary = initialBinarize(image);
cv::Mat binary_staves = isolateStaffs(binary);
cv::Mat binary_notes = isolateNotes(image);
cv::Mat horizontal_segments = horizontalHoughSegments(binary_staves);
std::vector<cv::Vec4i> full_lines = completeHorizontalLines(horizontal_segments);
std::vector<int> valid_line_ys = sortedYPositions(full_lines);
std::vector<std::vector<int> > staves = clusterStaves(valid_line_ys, image.cols);
std::cout << "Found " << staves.size() << " staves" << std::endl;
std::vector<cv::Rect> note_rects = noteRectangles(binary_notes);
for (const std::vector<int>& staff : staves) {
std::vector<cv::Rect> staff_notes;
for (const cv::Rect note : note_rects) {
if (intersectingLineIndex(note, staff) >= 0) {
staff_notes.push_back(note);
}
}
std::sort(staff_notes.begin(), staff_notes.end(),
[](const cv::Rect& a, const cv::Rect& b) {
return a.x > b.x;
}
);
while (!staff_notes.empty()) {
std::vector<cv::Rect> chord_bounds {
staff_notes.back()
};
staff_notes.pop_back();
for (int i = staff_notes.size() - 1; i >= 0; --i) {
const cv::Rect& prev_chord_note = chord_bounds.back();
if (rectsOverlap(staff_notes[i], prev_chord_note)) {
chord_bounds.push_back(staff_notes[i]);
staff_notes.erase(staff_notes.begin() + i);
++i;
}
}
Chord chord;
for (const cv::Rect& chord_note : chord_bounds) {
int i = intersectingLineIndex(chord_note, staff);
int c4_distance = (5 - i) * 2 + 2;
chord.push_back(Note::quarter(c4_distance));
}
song.push_back(chord);
}
}
// draw the rectangles
cv::Mat rect_img = cv::Mat::zeros(image.size(), image.type());
for (int i = 0; i < note_rects.size(); ++i) {
auto color = cv::Scalar(255, 255, 255);
cv::rectangle(rect_img, note_rects[i].tl(), note_rects[i].br(), color, 2);
}
//display steps
for (int i = 0; i < staves.size(); i++) {
for (int j = 0; j < staves[i].size(); j++) {
int y = staves[i][j];
cv::line(image, cv::Point(0, y), cv::Point(2000, y), cv::Scalar(50*j,20*i,0), 1, cv::LINE_AA);
}
}
cv::namedWindow("Initial Image", cv::WINDOW_AUTOSIZE);
cv::imshow("Initial Image", image);
cv::namedWindow("First Thresholding", cv::WINDOW_AUTOSIZE);
cv::imshow("First Thresholding", binary);
cv::imwrite("binary-image.png", binary);
// cv::namedWindow("Morphised Staff Lines", cv::WINDOW_AUTOSIZE);
// cv::imshow("Morphised Staff Lines", binary_staves);
//
// cv::namedWindow("big brain time", cv::WINDOW_AUTOSIZE);
// cv::imshow("big brain time", horizontal_segments);
cv::namedWindow("Morphised Notes", cv::WINDOW_AUTOSIZE);
cv::imshow("Morphised Notes", binary_notes);
cv::namedWindow("Bounding Rectangles", cv::WINDOW_AUTOSIZE);
cv::imshow("Bounding Rectangles", rect_img);
// cv::waitKey();
return song;
}
cv::Mat initialBinarize(cv::Mat image) {
std::cout << "initialBinarize" << std::endl;
cv::Mat binary;
cv:cvtColor(image, binary, cv::COLOR_RGB2GRAY);
cv::adaptiveThreshold(binary, binary, 255, cv::ADAPTIVE_THRESH_GAUSSIAN_C, cv::THRESH_BINARY_INV, 11, 4);
return binary;
}
cv::Mat isolateStaffs(cv::Mat bin_image) {
std::cout << "isolateStaffs" << std::endl;
int horizontal_size = bin_image.cols / 30;
cv::Mat horiz_structure = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(horizontal_size, 1));
int horizontal_size2 = 9;
cv::Mat horiz_structure2 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(horizontal_size2, 1));
// apply morphology operations
cv::Mat staves;
dilate(bin_image, staves, horiz_structure2, cv::Point(-1, -1));
erode(staves, staves, horiz_structure2, cv::Point(-1, -1));
erode(staves, staves, horiz_structure, cv::Point(-1, -1));
dilate(staves, staves, horiz_structure, cv::Point(-1, -1));
return staves;
}
cv::Mat isolateNotes(cv::Mat image) {
std::cout << "isolateNotes" << std::endl;
int vertical_size = 5;
cv::Mat verticalStructure = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(vertical_size, vertical_size));
cv::Mat binary;
cv::inRange(image, cv::Scalar(0, 0, 0), cv::Scalar(225, 225, 225), binary);
// Apply morphology operations
erode(binary, binary, verticalStructure, cv::Point(-1, -1));
dilate(binary, binary, verticalStructure, cv::Point(-1, -1));
return binary;
}
cv::Mat horizontalHoughSegments(cv::Mat horiz_line_image) {
std::cout << "horizontalHoughSegments" << std::endl;
//first hough transform this begins to isolate the staff lines out by finding chucks of them
std::vector<cv::Vec4i> hough_out;
cv::HoughLinesP(horiz_line_image, hough_out, 1, CV_PI / 180, 150, 100, 80);
std::cout << hough_out.size() << std::endl;
double slope_sum = 0.0;
for (const cv::Vec4i& hough_seg : hough_out) {
double slope =
((double)hough_seg[1] - (double)hough_seg[3])
/ (hough_seg[0] - hough_seg[2]);
slope_sum += slope;
}
// slope filtering to rat out noice and eighth note bars
// this will purify the lines a little so staffs are easier to pick out
double slope_avg = slope_sum / hough_out.size();
cv::Mat staff_lines(horiz_line_image.rows, horiz_line_image.cols, CV_8UC1);
staff_lines = 0;
std::cout << "fuck" << std::endl;
for (const cv::Vec4i& hough_seg : hough_out)
{
double slope = ((double)hough_seg[1] - (double)hough_seg[3]) / (hough_seg[0] - hough_seg[2]);
if(abs(slope_avg - slope) < .02)
{
cv::line(staff_lines, cv::Point(hough_seg[0], hough_seg[1]), cv::Point(hough_seg[2], hough_seg[3]), cv::Scalar(255,0,0), 2, cv::LINE_AA);
}
}
return staff_lines;
}
std::vector<cv::Vec4i> completeHorizontalLines(cv::Mat horiz_line_sements) {
std::cout << "completeHorizontalLines" << std::endl;
//galaxy brain strat use the hough again!!! links staff line segments
std::vector<cv::Vec4i> hough_out;
cv::HoughLinesP(horiz_line_sements, hough_out, 1, CV_PI / 180, 200, 400, 400);
//slope filter agian to pull any other inconsistent lines
double slope_sum = 0.0;
std::cout << "hi" << std::endl;
for (const cv::Vec4i& hough_seg : hough_out) {
double slope = ((double)hough_seg[1] - (double)hough_seg[3]) / (hough_seg[0] - hough_seg[2]);
slope_sum += slope;
}
std::cout << "hi" << std::endl;
double slope_avg = slope_sum / hough_out.size();
for(int j = 0; j < hough_out.size(); j++) {
double slope = ((double)hough_out[j][1] - (double)hough_out[j][3]) / (hough_out[j][0] - hough_out[j][2]);
if(abs(slope_avg - slope) > .02)
{
auto at_point = hough_out.begin() + j;
hough_out.erase(at_point);
j--;
}
}
return hough_out;
}
std::vector<int> sortedYPositions(std::vector<cv::Vec4i> lines) {
std::cout << "sortedYPositions" << std::endl;
//cluster really close lines to make potential staff lines
if (lines.empty()) {
std::cerr << "Didn't receive any hough lines to sort" << std::endl;
return std::vector<int>();
}
std::vector<int> line_ys;
line_ys.push_back((lines[0][1] + lines[0][3]) / 2);
for (const cv::Vec4i& line : lines) {
bool fit = false;
int midpoint = (line[1] + line[3]) / 2;
for (const int clustered_y : line_ys) {
if(abs(midpoint - clustered_y) <= 3) {
fit = true;
}
}
if(!fit) {
line_ys.push_back(midpoint);
}
}
// sort the staff lines
std::sort(line_ys.begin(), line_ys.end());
return line_ys;
}
std::vector<std::vector<int> > clusterStaves(std::vector<int> line_ys, int img_columns) {
std::cout << "clusterStaves" << std::endl;
std::vector<std::vector<int> > staves;
std::vector<int> pot_staff;
//bunch up the staff lines
while (!line_ys.empty()) {
if (pot_staff.empty()) {
pot_staff.push_back(line_ys.back());
line_ys.pop_back();
if (line_ys.back() > pot_staff[0] - .035 * img_columns && line_ys.size() > 0) {
pot_staff.push_back(line_ys.back());
line_ys.pop_back();
}
else {
pot_staff.clear();
}
}
else {
if (line_ys.back() >= pot_staff.back() - (pot_staff[pot_staff.size() - 2] - pot_staff.back()) * 2) {
pot_staff.push_back(line_ys.back());
line_ys.pop_back();
}
else {
if(pot_staff.size() >= 5) {
staves.push_back(std::vector<int>(pot_staff));
pot_staff.clear();
}
else if(pot_staff.size() < 5) {
pot_staff.clear();
}
}
}
}
//pull extrunious lines till only 5 remain
for (std::vector<int>& staff : staves) {
while(staff.size() > 5) {
int sum = 0;
for(int i = 0; i < staff.size(); i++) {
sum += staff[i];
}
int staffavg = sum / staff.size();
auto max = staff.begin();
for(auto j = staff.begin(); j != staff.end(); j++) {
if(abs(*j - staffavg) > *max) {
max = j;
}
}
staff.erase(max);
}
}
return staves;
}
std::vector<cv::Rect> noteRectangles(cv::Mat binary_notes) {
std::cout << "noteRectangles" << std::endl;
cv::Mat canny;
cv::Canny(binary_notes, canny, 128, 255);
std::vector<std::vector<cv::Point> > contours;
cv::findContours(canny, contours, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE);
std::vector<std::vector<cv::Point> > contours_poly(contours.size());
std::vector<cv::Rect> bounding_rects(contours.size());
for (int i = 0; i < contours.size(); ++i) {
cv::approxPolyDP(contours[i], contours_poly[i], 3, true);
bounding_rects[i] = cv::boundingRect(contours_poly[i]);
}
// remove objects with extra wide aspect ratios to filter out
// the ties between eigth and sixteenth notes
for (int i = 0; i < bounding_rects.size(); ++i) {
if (bounding_rects[i].width > (2 * bounding_rects[i].height)) {
bounding_rects.erase(bounding_rects.begin() + i);
contours_poly.erase(contours_poly.begin() + i);
--i;
}
}
// remove extraordinarily large objects to filter out punch holes
int total_width = 0;
for (const cv::Rect& rect : bounding_rects) {
total_width += rect.width;
}
int max_width = 2 * total_width / bounding_rects.size();
for (int i = 0; i < bounding_rects.size(); ++i) {
if (bounding_rects[i].width > max_width) {
bounding_rects.erase(bounding_rects.begin() + i);
contours_poly.erase(contours_poly.begin() + i);
--i;
}
}
return bounding_rects;
}
int intersectingLineIndex(cv::Rect note_bounds, const std::vector<int>& staff) {
for (int i = 0; i < staff.size(); ++i) {
int line = staff[i];
if (note_bounds.y < line && (note_bounds.y + note_bounds.height) > line) {
return i;
}
}
return -1;
}
bool rectsOverlap(const cv::Rect& a, const cv::Rect& b) {
return (a.x < (b.x + b.width) && a.x > b.x) ||
(b.x < (a.x + a.width) && b.x > a.x);
}