I cannot pass one of the final MATLAB problems on the coursera MATLAB course. What is wrong with my code? Why won't it work for the 'Image Blur' assignment?
Mostra commenti meno recenti
1st my function
then the code to call it.
what is wrong?
function output=blur(img,w)
img=double(img);
d=w*2+1
s=size(img)
output=[]
for r=1:d:s(1)
row=[];
for c=1:d:s(2)
if r+w<=s(1) && c+w<=s(2) && r-w>=1 && c-w>=1
tot=sum(sum(img((r-w):(r+w),(c-w):(c+w))));
val=tot/(d*d);
nxt=ones(d,d);
nxt(1:end,1:end)=val;
row=[row nxt];
elseif c+w<=s(2) && r-w>=1 && c-w>=1
tot=sum(sum(img((r-w):end,(c-w):(c+w))));
val=tot/(d*(s(1)-r+w));
nxt=ones((s(1)-r+w),d);
nxt(1:end,1:end)=val;
row=[row nxt];
elseif r+w<=s(1) && r-w>=1 && c-w>=1
tot=sum(sum(img((r-w):(r+w),(c-w):end)));
val=tot/(d*(s(2)-c+w));
nxt=ones(d,(s(2)-c+w));
nxt(1:end,1:end)=val;
row=[row nxt];
elseif r+w<=s(1) && c+w<=s(2) && r-w>=1
tot=sum(sum(img((r-w):(r+w),1:(c+w))));
val=tot/(d*(c+w));
nxt=ones(d,(c+w));
nxt(1:end,1:end)=val;
row=[row nxt];
elseif r+w<=s(1) && c+w<=s(2) && c-w>=1
tot=sum(sum(img(1:(r+w),(c-w):(c+w))));
val=tot/(d*(r+w));
nxt=ones((r+w),d);
nxt(1:end,1:end)=val;
row=[row nxt];
elseif r+w<=s(1) && c+w<=s(2)
tot=sum(sum(img(1:(r+w),1:(c+w))));
val=tot/((r+w)*(c+w));
nxt=ones((c+w),(r+w));
nxt(1:end,1:end)=val;
row=[row nxt];
elseif r-w>=1 && c-w>=1
tot=sum(sum(img((r-w):end,(c-w):end)));
val=tot/((s(1)-r+w)*(s(2)-c+w));
nxt=ones((s(1)-r+w),(s(2)-c+w));
nxt(1:end,1:end)=val;
row=[row nxt];
elseif r+w<=s(1) && c-w>=1
tot=sum(sum(img(1:(r+w),(c-w):end)));
val=tot/((r+w)*(s(2)-c+w));
nxt=ones((r+w),(s(2)-c+w));
nxt(1:end,1:end)=val;
row=[row nxt];
elseif c+w<=s(2) && r-w>=1
tot=sum(sum(img((r-w):end,1:(c+w))));
val=tot/((s(1)-r+w)*(c+w));
nxt=ones((s(1)-r+w),(c+w));
nxt(1:end,1:end)=val;
row=[row nxt];
end
end
output=[output;row];
end
row=[]
s2=size(output)
if s2(1)<s(1)
for c=1:d:s(2)
if c+w<=s(2) && c-w>=1
tot=sum(sum(img((s2(1)+1):s(1),(c-w):(c+w))));
val=tot/((s(1)-s2(1))*d);
nxt=ones((s(1)-s2(1)),d);
nxt(1:end,1:end)=val;
row=[row nxt];
elseif c+w<=s(2)
tot=sum(sum(img((s2(1)+1):s(1),1:(c+w))));
val=tot/((s(1)-s2(1))*(c+w));
nxt=ones((s(1)-s2(1)),(c+w));
nxt(1:end,1:end)=val;
row=[row nxt];
elseif c-w>=1
tot=sum(sum(img((s2(1)+1):s(1),(c-w):end)));
val=tot/((s(1)-s2(1))*(s2(2)-c+w));
nxt=ones((s(1)-s2(1)),(s2(2)-c+w));
nxt(1:end,1:end)=val;
row=[row nxt];
end
end
output=[output;row];
end
row=[]
col=[]
s3=size(output)
if s3(2)<s(2)
for r=1:d:s(1)
if r+w<=s(1) && r-w>=1
tot=sum(sum(img((r-w):(r+w),(s3(2)+1):s(2))));
val=tot/(d*(s(2)-s3(2)));
nxt=ones(d,(s(2)-s3(2)));
nxt(1:end,1:end)=val;
col=[col;nxt];
elseif r+w<=s(1)
tot=sum(sum(img(1:(r+w),(s3(2)+1):s(2))));
val=tot/((r+w)*(s(2)-s3(2)));
nxt=ones((r+w),(s(2)-s3(2)));
nxt(1:end,1:end)=val;
col=[col;nxt];
elseif r-w>=1
tot=sum(sum(img((r-w):(r+w),(s3(2)+1):s(2))));
val=tot/((s3(1)-r+w)*(s(2)-s3(2)));
nxt=ones((s3(1)-r+w),(s(2)-s3(2)));
nxt(1:end,1:end)=val;
col=[col;nxt];
end
end
s4=size(col)
if s4(1)<s3(1) || s4(1)<s(1)
tot=sum(sum(img((s2(1)+1):s(1),(s3(2)+1):s(2))));
val=tot/((s(1)-s2(1))*(s(2)-s3(2)));
nxt=ones((s(1)-s2(1)),(s(2)-s3(2)));
nxt(1:end,1:end)=val;
col=[col;nxt];
end
output=[output col];
end
output=uint8(output);
img = imread('vandy.png');
output = blur(img,1);
imshow(output);
.png)
4 Commenti
Walter Roberson
il 22 Lug 2019
That is a lot of code that could be much much much simpler.
How much simpler? Well, I could write the entire function as a single expression...
Walter Roberson
il 23 Lug 2019
Hint:
Suppose you take your input array. Suppose you create a new version of it that is padded with 0 on all sides. Suppose you also take an array of 1's that is as large as the original input array, and suppose you padd that array of 1s with 0 on all sides. Now, you can unconditionally form the sum of the padded input array starting from the first row to the last, and you can unconditionally form the sum of the padded arrays of 1s and 0's, starting from the first row to the last. Now you have two arrays the same size, and the one that was created from the 1s and 0's effectively tells you how many locations inside the original array exist for each location in the original array:
0 0 0 0 0 0 0
0 1 1 1 1 1 0
0 1 1 1 1 1 0
0 1 1 1 1 1 0
0 1 1 1 1 1 0
0 1 1 1 1 1 0
0 0 0 0 0 0 0
add the 3x3 sub-arrays storing in the center of the subarrays:
0 0 0 0 0 0 0
0 4 6 6 6 4 0
0 6 9 9 9 9 0
0 6 9 9 9 9 0
0 6 9 9 9 9 0
0 6 9 9 9 9 0
0 4 6 6 6 4 0
0 0 0 0 0 0 0
take out the core:
4 6 6 6 4
6 9 9 9 9
6 9 9 9 9
6 9 9 9 9
6 9 9 9 9
4 6 6 6 4
and those are the values you would have to divide the summed input array by.
Notice that no tests are needed about which corner condition you are in: you just do the inside of the padded array and everything takes care of itself.
Brandon McLaughlin
il 23 Lug 2019
raj praveen dasari
il 2 Apr 2020
brandon can you send the code that you got. because iam facing the same problem
Risposte (1)
Anastasios Papadopoulos
il 11 Gen 2020
function output = blur(img,w)
[m,n] = size(img);
img_1 = zeros(m + 2*w, n + 2*w);
for i=(1+w):(m+w)
for j=(1+w):(n+w)
img_1(i,j) = img(i-w,j-w);
end
end
k = 2*w + 1;
img_2 = img_1;
for i=(1+w):(m+w)
for j=(1+w):(n+w)
img_2(i,j) = mean(mean([img_1((i-w):(i+w),(j-w):(j+w))]));
end
end
output = uint8(img_2((w+1):(m+w),(w+1):(n+w)));
This code blurrs the test png file, although I faced failure again according to auto_correction.
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!