How to blur an image using a weighted average filter

In digital image processing, we use different filters to transform or improve the image’s size, color, and other characteristics. One of them is the weighted average filter, in which pixels are multiplied by different numbers. This gives more weightage to some pixels at the expense of others. We give more weight and importance to the center value. Then, it is multiplied by a higher value than any other value in the mask. This gives this pixel a higher contribution and impact than the other pixels. It also helps us to control the blurriness of the image.

A 3x3 weighted average filter
1 of 6

Example

When we apply this filter to an image, it blurs the image and removes the noise from it. It is also used to reduce details, such as sharpness, edges, and so on.

An example of the weighted average filter is given below.

img_1=imread("/lena_gray.jpeg");
mask=1/16*[1,2,1;2,4,2;1,2,1];
img_2=filter2(mask,img_1);
Plot=imshow([img_1, img_2]);

Explanation

  • Line 1: We use imread() to read the image and return the image data in img_1.
  • Line 3: We store the values of the 3x3 weighted average filter in mask.
  • Line 5: We use the build-in function, filter2(), to apply the filter mask on img_1 and store the filtered image in img_2.
  • Line 7: Lastly we use the imshow function to plot both the original and the blurred image side by side using.
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved