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

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

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);

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
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,

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.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/472573-write-a-function-called-blur-that-blurs-the-input-image#comment_726367

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/472573-write-a-function-called-blur-that-blurs-the-input-image#comment_726367

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/472573-write-a-function-called-blur-that-blurs-the-input-image#comment_726528

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/472573-write-a-function-called-blur-that-blurs-the-input-image#comment_726528

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/472573-write-a-function-called-blur-that-blurs-the-input-image#comment_726529

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/472573-write-a-function-called-blur-that-blurs-the-input-image#comment_726529

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/472573-write-a-function-called-blur-that-blurs-the-input-image#comment_726591

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/472573-write-a-function-called-blur-that-blurs-the-input-image#comment_726591

Sign in to comment.