Image filtering is a technique used to reduce noise, smooth images, and highlight features. In OpenCV, there are several filtering methods such as normal blur, Gaussian blur, median blur, and bilateral filtering. Each has its own use cases in image preprocessing.
A simple averaging filter that replaces each pixel with the average of its neighboring pixels.
import cv2
img = cv2.imread("sample.jpg")
blur = cv2.blur(img, (5,5)) # Kernel size 5x5
cv2.imshow("Normal Blur", blur)
cv2.waitKey(0)
cv2.destroyAllWindows()
A weighted blur where pixels closer to the center have more influence. Useful for reducing Gaussian noise.
gaussian = cv2.GaussianBlur(img, (5,5), 0)
cv2.imshow("Gaussian Blur", gaussian)
cv2.waitKey(0)
cv2.destroyAllWindows()
Replaces each pixel with the median value of its neighborhood. Very effective for removing salt-and-pepper noise.
median = cv2.medianBlur(img, 5)
cv2.imshow("Median Blur", median)
cv2.waitKey(0)
cv2.destroyAllWindows()
Smooths the image while preserving edges. Often used in cartoon effects and detail-preserving smoothing.
bilateral = cv2.bilateralFilter(img, 9, 75, 75)
cv2.imshow("Bilateral Filter", bilateral)
cv2.waitKey(0)
cv2.destroyAllWindows()
✅ In this lesson, you learned how to apply Normal Blur, Gaussian Blur, Median Blur,
and Bilateral Filtering in OpenCV.
These techniques are widely used for noise reduction, edge preservation, and preprocessing
before computer vision tasks.
👉 Next, we will explore edge detection techniques (Canny, Sobel, Laplacian).
