OpenCV Median Blurring for Images in Python

In this OpenCV article we are going to talk about OpenCV Median Blurring for Images in Python,

so this is the third technique for image smoothing or blurring in OpenCV . the first one was

using Averaging, and second was using Gaussian Blurring you can check the articles in the

below links.

 

 

 

 

Read More Image Smoothing Techniques in OpenCV

1: OpenCV Averaging Image Blurring in Python

2: OpenCV Gaussian Blurring for Images in Python

 

 

 

 

Median Blurring computes the median of all pixels under the kernel and the central pixel is

replaced with the median value, according to OpenCV Documentation it is mostly used for

removing salt and pepper noises.

 

 

 

 

So now this is the complete code for OpenCV Median Blurring for Images in Python

 

 

 

 

 

OK for this purpose we are going to use this image, you can see that we have a lot of

noises in this image.

Lena Noisy Image
Lena Noisy Image

 

 

 

 

 

This  line of code is used for reading of the image, make sure that you have added an image

in your working directory.

 

 

 

 

 

So in here we are going to use cv2.medianBlur() function for this purpose, we need to add

some parameters, our image and also the kernel size.

 

 

 

 

 

Because we are going to show our image in Matplotlib, so Matplotlib uses RGB (Red, Green, Blue)

color system, and OpenCV uses BGR (Blue, Green, Red) color system, we need to convert the BGR

color to RGB. if we don’t do this there will be messed up in the color.

 

 

 

 

Notecv2.waitKey() is a keyboard binding function. Its argument is the time in milliseconds.

the function waits  specified milliseconds for any keyboard event. If you press any key in that

time, the program continues. If 0 is passed, it waits indefinitely for a key stroke.

 

 

 

 

 

 

So run the complete code and this is the result.

OpenCV Median Blurring for Images in Python
OpenCV Median Blurring for Images in Python

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Share via
Copy link
Powered by Social Snap
×