Skip to content

Instantly share code, notes, and snippets.

@Aroueterra
Last active July 14, 2021 04:48
Show Gist options
  • Save Aroueterra/2e68cd9ac5bd08bb7100e6242f0f2441 to your computer and use it in GitHub Desktop.
Save Aroueterra/2e68cd9ac5bd08bb7100e6242f0f2441 to your computer and use it in GitHub Desktop.
clc;
close all;
clear;
disp('Project in Image Processing');
%////////////
%/// MAIN ///
%////////////
[idx,file_name] = show_dialog();
if isempty(file_name)
disp('Closing Program');
return;
end
file_name = file_name{1};
switch idx
case 1
disp('Starting: Moire Noise Removal');
remove_noise(file_name);
case 2
disp('Starting: Music StaffLine Segmentation');
segment_lines(file_name);
otherwise
disp('application is exiting')
end
function remove_noise(f)
workspace;
format long g;
format compact;
directory = fileparts(which(f));
filename = f;
fullFileName = fullfile(directory, filename);
if ~exist(fullFileName, 'file')
fullFileName = filename;
if ~exist(fullFileName, 'file')
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
img = imread(fullFileName);
[~, ~, numberOfColorBands] = size(img);
if numberOfColorBands > 1
img = rgb2gray(img);
end
subplot(1, 3, 1);
imshow(img, [0, max(max(img))]);
set(gcf, 'Name', fullFileName);
title('Original Image', 'FontSize', 12);
set(gcf, 'units','normalized','outerposition',[0, 0, 1, 1]);
%Make an image with moire noise in it.
double_img = floor(im2double(img) * 255);
ratio=0.1;
double_img = padarray(double_img,[30,30],'symmetric','both');
[m,n]=size(double_img);
nj1 = zeros(m,n);
nj2 = zeros(m,n);
% N1
for i= 1:m
for j=1:n
nj1(i,j)=ratio*255*(sin(1.8*i)+sin(1.8*j)+sin(i+j)+sin(2.2*i+2.2*j)+sin(1.8*i-1.8*j)+sin(i-j)+sin(2.2*i-2.2*j));
end
end
% N2
for i= 1:m
for j=1:n
nj2(i,j)=ratio*255*(sin(1.1*i+1.1*j)+sin(1.5*i)+sin(1.5*j+2.2*j)+sin(1.1*i-1.1*j));
end
end
temp_img = double_img + nj1 + nj2;
n_img(1:m-60,1:n-60)=temp_img(31:m-30,31:n-30);
subplot(1, 3, 2);
imshow(n_img/255, []);
axis on;
title('Image with Moire Noise Pattern', 'FontSize', 12);
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
temp_img=agnf(temp_img);
output_img(1:m-60,1:n-60)=temp_img(31:m-30,31:n-30);
subplot(1, 3, 3);
imshow(output_img/255, []);
title('Image cleaned with Adaptive Gaussian Notch Filter', 'FontSize', 12);
set(gcf, 'units','normalized','outerposition',[0, 0, 1, 1]);
end
function segment_lines(f)
option = questdlg('Choose music sheet element to view?', ...
'Music Sheet Elements', ...
'NOTES','STAVES','NOTES');
directory = fileparts(which(f));
filename = f;
fullFileName = fullfile(directory, filename);
if ~exist(fullFileName, 'file')
fullFileName = filename;
if ~exist(fullFileName, 'file')
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
img = imread(fullFileName);
[~, ~, numberOfColorBands] = size(img);
if numberOfColorBands > 1
img = rgb2gray(img);
end
img_gray = img;
[~, pic_width] = size(img_gray);
% binarization
T1 = graythresh(img_gray);
img_gray_new = imbinarize(img_gray,T1);
% segment intervals
img_gray_col = sum(img_gray_new,2)/pic_width;
T2 = graythresh(img_gray_col);
img_gray_col_new = imbinarize(img_gray_col,T2);
% segment
if (isequal(option,'NOTES'))
staff_lines = find(img_gray_col_new == 0);
else
staff_lines = find(img_gray_col_new == 1);
end
num_intervals = size(staff_lines,1)/5;
splits = [];
for i = 1:num_intervals-1
splits = [splits round((staff_lines(5*i)+staff_lines(5*i+1))/2)];
end
%gap = round(splits(2)-splits(1))/2;
%splits = [staff_lines(1)-gap splits];
%splits = [splits staff_lines(num_intervals*5)+gap];
for j = 1:length(staff_lines)
idx = staff_lines(j);
img_gray_new(idx,:) = 1;
end
fixed_img = mat2gray(img_gray_new);
imshow(fixed_img);
end
function [idx,file_name] = show_dialog()
file_name = {};
items = {'Moire Noise Removal','Music Staff Line Segmentation'};
[idx,tf] = listdlg('PromptString',{'Select a job to execute.',...
'Go',''},...
'SelectionMode','single','ListSize', [250 50],'ListString',items);
prompt = {'Enter image name:'};
title = 'Input';
dims = [1 35];
if (idx == 1)
definput = {'cameraman.tif'};
else
definput = {'odetojoy_min.png'};
end
if (tf ~= 0)
file_name = inputdlg(prompt,title,dims,definput);
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment