Problems with coursera image blur matlab problem

Hey all,
I am near completing the introduction to matlab programming course on Coursera.
function[output] = blur(img,w)
change = w;
emptymatrix = zeros(length(img(:,1)),length(img(1,:)));
for z = 1:length(img(1,:))
for i = 1:length(img(:,1))
if (i + change) <= length(img(:,1)) && (i - change) >= 1 && (z + change) <= length(img(1,:)) && (z - change) >= 1
submatrix = img((i-change):(i+change),(z-change):(z+change));
emptymatrix(i,z) = uint8(sum(submatrix(:))/numel(submatrix));
output = uint8(emptymatrix);
elseif i == 1 && z == 1
submatrix = img((i):(i+change),(z):(z+change));
emptymatrix(i,z) = uint8(sum(submatrix(:))/numel(submatrix));
output = uint8(emptymatrix);
elseif i == length(img(:,1)) && z == length(img(1,:))
submatrix = img((i-change):(i),(z-change):(z));
emptymatrix(i,z) = uint8(sum(submatrix(:))/numel(submatrix));
output = uint8(emptymatrix);
elseif i == 1 && z == length(img(1,:))
submatrix = img((i):(i+change),(z-change):(z));
emptymatrix(i,z) = uint8(sum(submatrix(:))/numel(submatrix));
output = uint8(emptymatrix);
elseif i == length(img(:,1)) && z == 1
submatrix = img((i-change):(i),(z):(z+change));
emptymatrix(i,z) = uint8(sum(submatrix(:))/numel(submatrix));
output = uint8(emptymatrix);
elseif i == 1 && z-change >= 1 && z+change <= length(img(1,:))
submatrix = img((i):(i+change),(z-change):(z+change));
emptymatrix(i,z) = uint8(sum(submatrix(:))/numel(submatrix));
output = uint8(emptymatrix);
elseif i == length(img(:,1)) && z-change >= 1 && z+change <= length(img(1,:))
submatrix = img((i-change):(i),(z-change):(z+change));
emptymatrix(i,z) = uint8(sum(submatrix(:))/numel(submatrix));
output = uint8(emptymatrix);
elseif z == 1 && i-change >= 1 && i+change <= length(img(:,1))
submatrix = img((i-change):(i+change),(z):(z+change));
emptymatrix(i,z) = uint8(sum(submatrix(:))/numel(submatrix));
output = uint8(emptymatrix);
elseif z == length(img(1,:)) && i-change >= 1 && i+change <= length(img(:,1))
submatrix = img((i-change):(i+change),(z-change):(z));
emptymatrix(i,z) = uint8(sum(submatrix(:))/numel(submatrix));
output = uint8(emptymatrix);
end
end
end
output = uint8(output);
end
I have attached a screenshot of the problem below and of the output the online program is giving me.
Could anyone give me a helping hand as to where I am going wrong?

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 13 Apr. 2020
Bearbeitet: Walter Roberson am 13 Apr. 2020
I would suggest to you that you could save a lot of code by using max() and min() on the coordinates, like
max(1, column-w):min(column+w, number_of_columns)
That will get you a block of values that you can take the mean of.

10 Kommentare

Emre Yavuz
Emre Yavuz am 13 Apr. 2020
@ Walter Roberson, thank you for your suggestion although I am not sure as to where to implement that. Do you think that will solve the 'incorrect answer' statement the program is bringing up?
ey21
ey21 am 13 Apr. 2020
@Walter Robertson, the 2nd part does not seem to be accepting my code (screenshot) whilst the first part does
emptymatrix = zeros(length(img(:,1)),length(img(1,:)));
That creates emptymatrix as a double()
emptymatrix(i,z) = uint8(sum(submatrix(:))/numel(submatrix));
The right hand side is uint8() because of the uint8() that you use. The left hand side is double(), so the uint8() value will be converted to double. Have you considered the possibility of initializing emptymatrix as uint8 to avoid that, and to avoid needing to uint8() the right hand side?
output = uint8(emptymatrix);
Why are you doing that every iteration inside both for loops?
output = uint8(output);
Under what circumstances would output not already be uint8 after the loops?
Emre Yavuz
Emre Yavuz am 14 Apr. 2020
Bearbeitet: Walter Roberson am 14 Apr. 2020
@ Walter Roberson, I have edited my code below but it is still not accepting...
function[output] = blur(img,w)
change = w;
emptymatrix = uint8(zeros(size(img)));
for z = 1:length(img(1,:))
for i = 1:length(img(:,1))
if (i + change) <= length(img(:,1)) && (i - change) >= 1 && (z + change) <= length(img(1,:)) && (z - change) >= 1
submatrix1 = img((i-change):(i+change),(z-change):(z+change));
emptymatrix(i,z) = (sum(submatrix1(:))/numel(submatrix1));
elseif i == 1 && z == 1
submatrix2 = img((i):(i+change),(z):(z+change));
emptymatrix(i,z) = (sum(submatrix2(:))/numel(submatrix2));
elseif i == length(img(:,1)) && z == length(img(1,:))
submatrix3 = img((i-change):(i),(z-change):(z));
emptymatrix(i,z) = (sum(submatrix3(:))/numel(submatrix3));
elseif i == 1 && z == length(img(1,:))
submatrix4 = img((i):(i+change),(z-change):(z));
emptymatrix(i,z) = (sum(submatrix4(:))/numel(submatrix4));
elseif i == length(img(:,1)) && z == 1
submatrix5 = img((i-change):(i),(z):(z+change));
emptymatrix(i,z) = (sum(submatrix5(:))/numel(submatrix5));
elseif i == 1 && z-change >= 1 && z+change <= length(img(1,:))
submatrix6 = img((i):(i+change),(z-change):(z+change));
emptymatrix(i,z) = (sum(submatrix6(:))/numel(submatrix6));
elseif i == length(img(:,1)) && z-change >= 1 && z+change <= length(img(1,:))
submatrix7 = img((i-change):(i),(z-change):(z+change));
emptymatrix(i,z) = (sum(submatrix7(:))/numel(submatrix7));
elseif z == 1 && i-change >= 1 && i+change <= length(img(:,1))
submatrix8 = img((i-change):(i+change),(z):(z+change));
emptymatrix(i,z) = (sum(submatrix8(:))/numel(submatrix8));
elseif z == length(img(1,:)) && i-change >= 1 && i+change <= length(img(:,1))
submatrix9 = img((i-change):(i+change),(z-change):(z));
emptymatrix(i,z) = (sum(submatrix9(:))/numel(submatrix9));
end
end
end
output = emptymatrix;
end
I believe I have covered every index in the empty matrix but somehow when I print the emptymatrix at the end, there are some zeros appearing where there should be non-zero numbers.
For example when my input matrix is this:
225 225 225 225 225
225 0 0 0 225
225 0 0 0 225
225 0 0 0 225
225 225 225 225 225
[output] = blur(img,2)
Gives me:
5×5 uint8 matrix
125 0 135 0 125
0 0 0 0 0
135 0 144 0 135
0 0 0 0 0
125 0 135 0 125
But I believe it should give me:
5×5 uint8 matrix
125 113 135 113 125
113 98 124 98 113
135 124 144 124 135
113 98 124 98 113
125 113 135 113 125
You should clean up your code:
[rows, cols, ~] = size(img);
Now use rows instead of length(img(:,1)) and cols instead of length(img(1,:))
And I really do recommend using max() and min(), the code because very easy.
@walter roberson, finally managed to solve it using this:
function[output] = blur(img,w)
[rows, cols, ~] = size(img);
emptymatrix = uint8(zeros(size(img)));
for z = 1:cols
for i = 1:rows
maxrow = min(i + w,rows);
minrow = max(i - w,1);
maxcol = min(z + w,cols);
mincol = max(z - w,1);
if minrow >= 1 && maxrow <= rows && mincol >= 1 && maxcol <= cols
submatrix1 = img((minrow):(maxrow),(mincol):(maxcol));
emptymatrix(i,z) = (sum(submatrix1(:))/numel(submatrix1));
end
end
end
output = emptymatrix;
end
Thanks so much for your help!
Could you give an example of a situation in which the if came out false?
ey21
ey21 am 14 Apr. 2020
@ Walter Roberson, is this a trick question? As in, the if line does not need to be there?
I can't think of a reason to have it there.
Emre Yavuz
Emre Yavuz am 14 Apr. 2020
@ Walter Roberson, agreed!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Muhammad Qaisar Ali
Muhammad Qaisar Ali am 27 Jun. 2020
function output = blur(img,w);
[r,c]=size(img);
output=ones(r,c);
for ri=1:r
for ci=1:c
% checking for indicies,and making limits for sub matrix.
if ri-w<1
sub_mat_all_row_indicies=1:(ri+w);
elseif ri+w>r
sub_mat_all_row_indicies=(ri-w):r;
else
sub_mat_all_row_indicies=(ri-w):(ri+w);
end
if ci-w<1
sub_mat_all_col_indicies=1:(ci+w);
elseif ci+w>c
sub_mat_all_col_indicies=(ci-w):c;
else
sub_mat_all_col_indicies=(ci-w):(ci+w);
end
sub_mat=img(sub_mat_all_row_indicies,sub_mat_all_col_indicies); %make sub matrix/window.
% bluring or averaging.
output(ri,ci)=mean(sub_mat(:));
end
end
output=uint8(output); %converting to grayscle. 0 to 255.
end

Kategorien

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by