Skip to content

Instantly share code, notes, and snippets.

@zbrasseaux
Created January 21, 2020 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zbrasseaux/ff7aeae2c78cd2c5c6e99726022950c4 to your computer and use it in GitHub Desktop.
Save zbrasseaux/ff7aeae2c78cd2c5c6e99726022950c4 to your computer and use it in GitHub Desktop.
function [x_filtered_std_dev] = hw2_02
% generate 10 images of size 256x256 of pixel value 128
x = ones(10,256,256)*128;
% apply gaussian noise
x = imnoise(x, 'gaussian', 0, 2.0);
% solution for homework problem 2
x_filtered = apply_box_filter(x, 3);
x_filtered_std_dev = generate_std_dev(x_filtered);
end
%% function for finding the mean of all images
% used in generate_std_dev function
function [outputMatrix] = matrix_mean(inputMatrix)
number_of_images = length(inputMatrix);
% memory allocation
temp_matrix = zeros(size(inputMatrix(1)));
% summation of all image values
for i = 1:number_of_images
temp_matrix = temp_matrix + inputMatrix(i);
end
% returns matrix where each pixel value is the mean of all the other
% pixels at that index for all images
outputMatrix = (1/number_of_images) * temp_matrix;
end
%% function for finding the stddev of all images
function [output_val] = generate_std_dev(input_matrix)
number_of_images = length(input_matrix);
% memory allocation
temp_matrix = zeros(size(input_matrix(1)));
% calls function to get avg matrix
matrix_average = matrix_mean(input_matrix);
% summation of all squared values
for i = 1:number_of_images
temp_matrix = temp_matrix + (matrix_average - input_matrix(i)).^2;
end
% divide temp
temp_matrix = temp_matrix * (1/(number_of_images-1));
% return sqrt value
output_val = sqrt(temp_matrix);
end
%% function for applying the filter for #2
function [filtered_matrix] = apply_box_filter (input_matrix, filter_size)
% generate the filter (variable size so i can do different filters)
box_filter = ones(filter_size, filter_size) * 1/(filter_size.^2);
temp_matrix = zeros(size(input_matrix(1)));
% convolve the filter over the matrix
for i = 1:length(input_matrix)
temp_matrix(i) = conv2(input_matrix(i), box_filter, 'same');
end
% return the filtered matrix
filtered_matrix = temp_matrix;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment