Hello all,
I am trying to work through one of the problems for the 'introduction to matlab programming' course by coursera, known as the 'image blur' problem (lesson 8 – final problems, week 9). The system keeps telling me that my values are incorrect, but I am struggling to understand why? I have attached my code below. I would be very grateful if anyone could give me a helping hand.
%The code to call the function:
img = imread('vandy.png')output = blur(img,2) % I have specified the function 'blur' below
imshow(output);% The function:
function[output] = blur(img,w)here = 2*w + 1;change = w;%setting an epmpty matrix
emptymatrix = zeros(length(img(:,1)),length(img(1,:)));%working out the output pixel value for each corner
added = img((1):(1+change),(1):(1+change));addition = sum(added);additionally = sum(addition);mean = additionally/numel(added);answer = uint8(mean);emptymatrix(1,1) = answer;i = length(img(:,1));z = 1;added = img((i-change):(i),(z):(z+change));addition = sum(added);additionally = sum(addition);mean = additionally/numel(added);answer = uint8(mean);emptymatrix(i,z) = answer;i = 1;z = length(img(1,:));added = img((i):(i+change),(z-change):(z));addition = sum(added);additionally = sum(addition);mean = additionally/numel(added);answer = uint8(mean);emptymatrix(i,z) = answer;i = length(img(:,1));z = length(img(1,:));added = img((i-change):(i),(z-change):(z));addition = sum(added);additionally = sum(addition);mean = additionally/numel(added);answer = uint8(mean);emptymatrix(i,z) = answer;output = uint8(emptymatrix);% working out the pixel values for the inner square
for i = 1:length(img(:,1)) for z = 1:length(img(1,:)) if ((i + change) <= length(img(:,1))) && ((i-change) > 0) && ((z+change) <= length(img(1,:))) && ((z-change) > 0) added = img((i-change):(i+change),(z-change):(z+change)); addition = sum(added); additionally = sum(addition); mean = additionally/(here*here); answer = uint8(mean); emptymatrix(i,z) = answer; output = uint8(emptymatrix); end endend% working out the output pixel values for the outer rows and columns (excluding the corners)
for i = 2:(length(img(:,1))-1)if (i-change) > 0 && (i+change) < length(img(:,1))z = 1;added = img((i-change):(i+change),(z):(z+change));addition = sum(added);additionally = sum(addition);mean = additionally/numel(added);answer = uint8(mean);emptymatrix(i,z) = answer;output = uint8(emptymatrix);endendfor i = 2:(length(img(:,1))-1)if (i-change) > 0 && (i+change) < length(img(:,1))z = length(img(1,:));added = img((i-change):(i+change),(z-change):(z));addition = sum(added);additionally = sum(addition);mean = additionally/numel(added);answer = uint8(mean);emptymatrix(i,z) = answer;output = uint8(emptymatrix);endendfor i = 2:(length(img(1,:))-1)if (i-change) > 0 && (i+change) < length(img(1,:))z = 1;added = img((z):(z+change),(i-change):(i+change));addition = sum(added);additionally = sum(addition);mean = additionally/numel(added);answer = uint8(mean);emptymatrix(z,i) = answer;output = uint8(emptymatrix);endendfor i = 2:(length(img(1,:))-1)if (i-change) > 0 && (i+change) < length(img(1,:))z = length(img(:,1));added = img((z-change):(z),(i-change):(i+change));addition = sum(added);additionally = sum(addition);mean = additionally/numel(added);answer = uint8(mean);emptymatrix(z,i) = answer;output = uint8(emptymatrix);endendend
Best Answer