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.
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 inimg_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 filtermaskonimg_1and store the filtered image inimg_2. - Line 7: Lastly we use the
imshowfunction to plot both the original and the blurred image side by side using.
Free Resources