Updated April 18, 2023
Introduction to OpenCV Median Filter
The random variations in the pixel values of a given image can be defined as the noise in the image and there are several algorithms to remove the noise from a given image and median filter is one of such algorithms to remove the noise from a given image in OpenCV using which an entire image will be scanned with the help of a small matrix and the central pixel value is recalculated by computing the median of all the values in the matrix and to implement the median filter algorithm, we make use of a function called medianBlur() function in OpenCV.
Syntax:
Below is the syntax to define medianBlur() function in OpenCV is as follows:
medianBlur(source_image, kernel_size)
where source_image is the image from which the noise is to be removed using medianBlur() function and
kernel_size represents the size of the kernel which must be a positive odd integer.
Working of medianBlur() function in OpenCV
Below we are discussing the Working of medianBlur() function in OpenCV is as follows:
- In order to remove random variations in the pixel values of the given image or the noise, we make use of the median filter in OpenCV.
- The function medialBlur() is used to remove the noise from the given image.
- The image whose noise must be removed is read using imread() function.
- Then medianBlur() function is applied on the image along with specifying the sie of the kernel to remove the noise from the image.
- The medianBlur() function returns an image with the noise removed from the image.
Examples of OpenCV Median Filter
Below are the examples of OpenCV Median Filter:
Example #1
OpenCV program in python to demonstrate medianBlur() function to read the given image and remove the noise from the given image and display it as the output on the screen:
Code:
#importing all the required modules
import cv2 as cv
#reading the image whose noise is to be removed using imread() function
imageread = cv.imread('C:/Users/admin/Desktop/educba.jpg')
#using medinaBlur() function to remove the noise from the given image
imagenormal = cv.medianBlur(imageread, 5)
#displaying the noiseless image as the output on the screen
cv.imshow('Noiseless_image',imagenormal)
cv.waitKey(0)
cv.destroyAllWindows()
Output:
Explanation: In the above program, we are importing the required modules. Then we are reading the image whose noise must be removed, using imread() function. Then we are making use of medianBlur() function to remove the noise from the image. Then we are displaying the noiseless image as the output on the screen.
Example #2
OpenCV program in python to demonstrate medianBlur() function to read the given image and remove the noise from the given image and display it as the output on the screen:
Code:
#importing all the required modules
import cv2 as cv
#reading the image whose noise is to be removed using imread() function
imageread = cv.imread('C:/Users/admin/Desktop/plane.jpg')
#using medinaBlur() function to remove the noise from the given image
imagenormal = cv.medianBlur(imageread, 5)
#displaying the noiseless image as the output on the screen
cv.imshow('Noiseless_image',imagenormal)
cv.waitKey(0)
cv.destroyAllWindows()
Output:
Explanation: In the above program, we are importing the required modules. Then we are reading the image whose noise must be removed, using imread() function. Then we are making use of use of medianBlur() function to remove the noise from the image. Then we are displaying the noiseless image as the output on the screen.
Example #3
OpenCV program in python to demonstrate medianBlur() function to read the given image and remove the noise from the given image and display it as the output on the screen:
Code:
#importing all the required modules
import cv2 as cv
#reading the image whose noise is to be removed using imread() function
imageread = cv.imread('C:/Users/admin/Desktop/car.jpg')
#using medinaBlur() function to remove the noise from the given image
imagenormal = cv.medianBlur(imageread, 5)
#displaying the noiseless image as the output on the screen
cv.imshow('Noiseless_image',imagenormal)
cv.waitKey(0)
cv.destroyAllWindows()
Output:
Explanation: In the above program, we are importing the required modules. Then we are reading the image whose noise must be removed, using imread() function. Then we are making use of use of medianBlur() function to remove the noise from the image. Then we are displaying the noiseless image as the output on the screen.
Example #4
OpenCV program in python to demonstrate medianBlur() function to read the given image and remove the noise from the given image and display it as the output on the screen:
Code:
#importing all the required modules
import cv2 as cv
#reading the image whose noise is to be removed using imread() function
imageread = cv.imread('C:/Users/admin/Desktop/tree.jpg')
#using medinaBlur() function to remove the noise from the given image
imagenormal = cv.medianBlur(imageread, 5)
#displaying the noiseless image as the output on the screen
cv.imshow('Noiseless_image',imagenormal)
cv.waitKey(0)
cv.destroyAllWindows()
Output:
Explanation: In the above program, we are importing the required modules. Then we are reading the image whose noise must be removed, using imread() function. Then we are making use of use of medianBlur() function to remove the noise from the image. Then we are displaying the noiseless image as the output on the screen.
Example #5
OpenCV program in python to demonstrate medianBlur() function to read the given image and remove the noise from the given image and display it as the output on the screen:
Code:
#importing all the required modules
import cv2 as cv
#reading the image whose noise is to be removed using imread() function
imageread = cv.imread('C:/Users/admin/Desktop/logo.png')
#using medinaBlur() function to remove the noise from the given image
imagenormal = cv.medianBlur(imageread, 5)
#displaying the noiseless image as the output on the screen
cv.imshow('Noiseless_image',imagenormal)
cv.waitKey(0)
cv.destroyAllWindows()
Output:
Explanation: In the above program, we are importing the required modules. Then we are reading the image whose noise must be removed, using imread() function. Then we are making use of use of medianBlur() function to remove the noise from the image. Then we are displaying the noiseless image as the output on the screen.
Conclusion
In this article, we have learnt the concept of median filtering in OpenCV using medianBlur() function, the syntax to define medianBlur() function, working of medianBlur() function with corresponding programming examples and their outputs to demonstrate them.
Recommended Articles
We hope that this EDUCBA information on “OpenCV Median Filter” was beneficial to you. You can view EDUCBA’s recommended articles for more information.