music_file='img/nabucodonosor.jpg'; % sheet music
mask_file='img/nabucodonosor_mask.jpg'; % note template

M=imread(music_file);
M = rgb2gray(M);
figure;
imshow(M);

M=255-M;

M2 = imtophat(M, strel('disk', 5));
figure;
imshow(M2);

M3 = im2bw(M2,.1);
figure;
imshow(M3);

M4 = imopen(M3, strel('disk', 4));
figure;
imshow(M4);

SE = imread(mask_file);
SE = im2bw(SE, .5);
% A better result is obtained with a smaller template
%SE = imerode(SE, [1;1]);

M5 = imopen(M3, SE);
figure;
imshow(M5);

M6r = M;
M6r(find(M5)) = 255;
M6g = M;
M6g(find(M5)) = 0;
M6b = M6g;
M6(:,:,1) = M6r;
M6(:,:,2) = M6g;
M6(:,:,3) = M6b;
figure;
imshow(M6);

