-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCVFinal.m
93 lines (73 loc) · 2.85 KB
/
CVFinal.m
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
Raw = imread('MusicSheet.jpg');
Gray = rgb2gray(Raw);
BW = edge(Gray,'canny');
[H, theta, rho] = hough(BW);
peak = houghpeaks(H);
StaffAngle = theta(peak(2));
RotatedCorrected = imrotate_white(Raw, StaffAngle - 90);
%imshow(RotatedCorrected);
Gray = rgb2gray(RotatedCorrected);
BW = Gray > 200;
BW = 1 - BW;
%imshow(BW);
hzst = strel('line', 120, 0);
horz = imerode(BW, hzst);
horz = imdilate(horz, hzst);
vtst = strel('line', 7, 90);
virt = imerode(BW, vtst);
virt = imdilate(virt, vtst);
vtst2 = strel('line', 150, 90);
virt2 = imerode(BW, vtst2);
virt2 = imdilate(virt2, vtst2);
Morphed = BW - horz + virt - virt2 * 2;
imshow(bwareaopen(Morphed, 20));
CC = bwconncomp(Morphed);
S = regionprops(CC);
notes = zeros(numel(S), 4);
for j=1:numel(S)
if S(j).Area > 50
rectangle('Position', S(j).BoundingBox, 'EdgeColor', 'r', 'LineWidth', 2);
notes(j, :) = ceil(S(j).BoundingBox);
end
end
nonZero = any(notes, 2);
notes = notes(nonZero, :);
disp(notes);
images = cell(numel(S), 1);
cleanImages = cell(numel(S), 1);
for index = 1:size(notes)
try
current = (Morphed(notes(index, 2): notes(index, 2) + notes(index, 4), notes(index, 1): notes(index, 1) + notes(index, 3)));
catch
current = (Morphed(notes(index, 2): notes(index, 2) + notes(index, 4) - 1, notes(index, 1): notes(index, 1) + notes(index, 3) - 1));
end
[labeled, blobs] = bwlabel(current);
props = regionprops(labeled, 'area');
[sortedAreas, sortedIndex] = sort([props.Area], 'descend');
cleanCurrent = ismember(labeled, sortedIndex(1:1));
cleanImages{index} = current;
images{index} = cleanCurrent;
end
nonEmptyCells = false(size(images));
for x = 1:numel(images)
nonEmptyCells(x) = ~isempty(images{x});
end
images = images(nonEmptyCells);
cleanImages = cleanImages(nonEmptyCells);
subplot(1,2,1), montage(images);
subplot(1,2,2), montage(cleanImages);
%Credit to Mustafa Umit Arabul for writing this function to replace
%with white instead of black when rotating
function rotated_image = imrotate_white(image, rot_angle_degree)
RA = imref2d(size(image));
tform = affine2d([cosd(rot_angle_degree) -sind(rot_angle_degree) 0; ...
sind(rot_angle_degree) cosd(rot_angle_degree) 0; ...
0 0 1]);
Rout = images.spatialref.internal.applyGeometricTransformToSpatialRef(RA,tform);
Rout.ImageSize = RA.ImageSize;
xTrans = mean(Rout.XWorldLimits) - mean(RA.XWorldLimits);
yTrans = mean(Rout.YWorldLimits) - mean(RA.YWorldLimits);
Rout.XWorldLimits = RA.XWorldLimits+xTrans;
Rout.YWorldLimits = RA.YWorldLimits+yTrans;
rotated_image = imwarp(image, tform, 'OutputView', Rout, 'interp', 'cubic', 'fillvalues', 255);
end