# Problems with coursera image blur matlab problem

7 Ansichten (letzte 30 Tage)
ey21 am 13 Apr. 2020
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?
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

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 Kommentare8 ältere Kommentare anzeigen8 ältere Kommentare ausblenden
Walter Roberson am 14 Apr. 2020
I can't think of a reason to have it there.
Emre Yavuz am 14 Apr. 2020
@ Walter Roberson, agreed!

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (1)

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
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by