Skip to content

Instantly share code, notes, and snippets.

@Aroueterra
Created July 13, 2021 12:14
Show Gist options
  • Save Aroueterra/f4d9207298d237e05119011694940395 to your computer and use it in GitHub Desktop.
Save Aroueterra/f4d9207298d237e05119011694940395 to your computer and use it in GitHub Desktop.
disp('Project in Image Processing');
pkg load image;
function remove_noise (f)
clc;
close all;
clear;
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);
[rows, columns, 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)
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);
[rows, columns, numberOfColorBands] = size(img);
if numberOfColorBands > 1
img = rgb2gray(img);
end
img_gray = img;
[pic_length, pic_width] = size(img_gray);
%imshow(img_gray);
%imhist(img);
% binarization
T1 = graythresh(img_gray);
img_gray_new = imbinarize(img_gray,T1);
img_gray(find(img_gray>T1)) = 255;
%img_gray(find(img_gray<=T1)) = 0;
% split 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);
%imshow(img_gray_col_new);
staff_lines = find(img_gray_col_new == 0);
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
%imshow(img_gray_new);
fixed_img = mat2gray(img_gray_new);
imshow(fixed_img);
% for k=1:num_intervals
% start = splits(k);
% final = splits(k+1);
% interval = double(img_gray_new(start:final,:));
% eval([ 'interval',num2str(k),'=','interval;']);
% [x, y] = size(interval);
% eval([ 'pic_length',num2str(k), ' = x' ]);
% eval([ 'interval',num2str(k),'_row =','sum(interval,1)/pic_length',num2str(k) ]);
% %interval =strcat(interval,'.jpg')
% % ...
% end
%imshow(interval);
end
function [idx,file_name] = show_dialog()
items = {'Moire Noise Removal','Music Staff Line Segmentation'};
[idx,tf] = listdlg('PromptString',{'Select a job to execute.',...
'Go.',''},...
'SelectionMode','single','ListString',items);
prompt = {'Enter image name:'};
title = 'Input';
dims = [1 35];
definput = {'cameraman.tif'};
if (tf ~= 0)
file_name = inputdlg(prompt,title,dims,definput);
end
end
%////////////
%/// MAIN ///
%////////////
[idx,file_name] = show_dialog();
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment