Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Noise Removal
Noise is random variation in pixel values caused by camera sensors, compression, or transmission errors. Removing noise before processing improves the accuracy of detection algorithms.
8 min•By Priygop Team•Updated 2026
Types of Noise and How to Remove Them
Types of Noise and How to Remove Them
import cv2
import numpy as np
image = cv2.imread("photo.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# --- ADD ARTIFICIAL NOISE FOR DEMONSTRATION ---
# Salt-and-pepper noise (random black/white pixels)
def add_salt_pepper(img, amount=0.02):
noisy = img.copy()
n_pixels = int(amount * img.size)
# Salt (white)
coords = [np.random.randint(0, i-1, n_pixels) for i in img.shape]
noisy[coords[0], coords[1]] = 255
# Pepper (black)
coords = [np.random.randint(0, i-1, n_pixels) for i in img.shape]
noisy[coords[0], coords[1]] = 0
return noisy
# Gaussian noise (random intensity variation)
def add_gaussian_noise(img, std=25):
noise = np.random.normal(0, std, img.shape).astype(np.int16)
noisy = np.clip(img.astype(np.int16) + noise, 0, 255).astype(np.uint8)
return noisy
sp_noisy = add_salt_pepper(gray)
gauss_noisy = add_gaussian_noise(gray)
# --- REMOVE NOISE ---
# Salt-and-pepper → use Median blur
sp_denoised = cv2.medianBlur(sp_noisy, 5)
# Gaussian noise → use Gaussian blur or Non-local Means
gauss_denoised_gb = cv2.GaussianBlur(gauss_noisy, (5, 5), 0)
gauss_denoised_nlm = cv2.fastNlMeansDenoising(
gauss_noisy, h=10, templateWindowSize=7, searchWindowSize=21
)
cv2.imwrite("sp_noisy.jpg", sp_noisy)
cv2.imwrite("sp_denoised.jpg", sp_denoised)
cv2.imwrite("gauss_denoised.jpg", gauss_denoised_nlm)
print("Noise removal complete")Diagram
Loading diagram…
Machine Learning follows a structured pipeline from data to deployment