MATLAB Answers

Write a function called blur that blurs the input image

282 views (last 30 days)
Write a function called blur that blurs the input image. The function is to be called like this:
output = blur(img,w);
where img, the input image is a two-dimensional matrix of grayscale pixel values between 0 and 255. Blurring is to be carried out by averaging the pixel values in the vicinity of every pixel. Specifically, the output pixel value is the mean of the pixels in a square submatrix of size 2w+1 where the given pixel sits in the center. For example, if w is 1, then we use a 3x3 matrix, that is, we average all the neighboring pixels of the given pixel and itself. Only use valid pixels when portions of the blurring matrix fall outside the image. For example, the blurred value corresponding to w = 1 at index (1,1) would be the mean of of elements (1,1), (1, 2), (2,1) and (2, 2). Both input img and output output are of type uint8.
You can download the test image here
Opens in new tab
to use in MATLAB.
I am using the following code but it gives me an error. Can anyone please tell me where I am going wrong?
function output = blur(img,w)
B=double(img);
[m,n] = size(B);
k=2*w+1;
for i = 1:m
for j = 1:n
p=i-fix(k/2);
q=i+fix(k/2);
r=j-fix(k/2);
s=j+fix(k/2);
if p<1
p=1;
end
if q>m
q=m;
end
if r<1
r=1;
end
if s>n
s=n;
end
A=B([p:q],[r:s]);
B(i,j)=mean(A(:));
end
end
output=uint8(B);
end

  4 Comments

Show 1 older comment
Namarta Kapil
Namarta Kapil on 20 Jul 2019
  • Simple testVariable output has an incorrect value. Tested with a 5x5 uint8 matrix with all 255-s in the first and last rows and columns and zeros everywhere else and w = 1
  • Assessment result: incorrectUsing image fileVariable output has an incorrect value. Tested with the vandy.png file and w = 1

Sign in to comment.

Accepted Answer

Aditya Sawant
Aditya Sawant on 22 Jul 2019
Your program is correct. But you are overlapping resultant matrix with input matrix which is creating proble.
For e.g. you have calculated avg pixel for w=1 and for location(1,1) which will store at same matrix at location(1,1). But for (1,2) while avg surrounding cells it will consider (1,1) location of matrix B where on location(1,1) you saved your result/output value not input value.
So just create new output matrix like mentioned below. Hope it will help you.
function output = blur(img,w)
B=double(img);
[m,n] = size(B);
k=2*w+1;
for i = 1:m
for j = 1:n
p=i-fix(k/2);
q=i+fix(k/2);
r=j-fix(k/2);
s=j+fix(k/2);
if p<1
p=1;
end
if q>m
q=m;
end
if r<1
r=1;
end
if s>n
s=n;
end
A=B([p:q],[r:s]);
C(i,j)=mean(A(:));
end
end
output=uint8(C);
end

More Answers (2)

KALYAN ACHARJYA
KALYAN ACHARJYA on 21 Jul 2019
Edited: KALYAN ACHARJYA on 21 Jul 2019
I didnot find any error.
im=imread('vandy.png');
y=blur(im,3);
imshow(y);
66.png

  5 Comments

Show 2 older comments
Namarta Kapil
Namarta Kapil on 21 Jul 2019
This is an assignment question for my MATLAB course on coursera so when I submit this code to run it, it gives me the above error.
Namarta Kapil
Namarta Kapil on 21 Jul 2019
I have tried changing the values and the image does get more blurry but I still get the same error when I submit it.
KALYAN ACHARJYA
KALYAN ACHARJYA on 21 Jul 2019
Sorry, I have no idea about other third party website/submitting. Just request you check properly and submit. If you have any Matlab ralated questions, you are welcome.
Good Luck!

Sign in to comment.


Walter Roberson
Walter Roberson on 21 Jul 2019
A=B([p:q],[r:s]);
B(i,j)=mean(A(:));
You are overwriting B as you go. You should be storing into a different array than you are using as the source to calculate the values,

  3 Comments

Namarta Kapil
Namarta Kapil on 21 Jul 2019
Isn't that what I am supposed to do to blur the image? I want to blur the original image and if I understood the question right, then according to the question "Blurring is to be carried out by averaging the pixel values in the vicinity of every pixel. Specifically, the output pixel value is the mean of the pixels in a square submatrix of size 2w+1 where the given pixel sits in the center". I have tried a lot of changes in the code but it doesnt seem to work. The code does blur the picture but its still giving me an error for some reason. I feel like maybe I am missing a step or two but what I can't figure it out.
Walter Roberson
Walter Roberson on 21 Jul 2019
Consider the difference between,
"You have a line of holes. Visit each one starting from the second and dig it one foot deeper than the one to its left"
compared to
"You have a line of holes. Visit each one starting from the second and dig it one foot deeper than the depth that the one to its left started out as"
Specifically, the output pixel value is the mean of the pixels in a square submatrix of size 2w+1 where the given pixel sits in the center.
"square submatrix" is referring to the square submatrix of the input pixels, not a square submatrix that includes already-blurred pixels.

Sign in to comment.

Sign in to answer this question.