Python OpenCV: Remove Noise from an Image Using cv2.filter2D()

There are many different types of noise in raw images, like Gaussian noise, salt and pepper noise, etc. In this tutorial, we will use an example to introduce how to remove salt & pepper noise using cv2.filter2D() in python opencv.

Python OpenCV - Remove Noise from an Image Using cv2.filter2D()

1.Read an image

import numpy as np
import cv2
#read image
img_src = cv2.imread('sample.jpg')

2.Define a kernel to remove salt & pepper noise

kernel_sharpening = np.array([[-1,-1,-1],
                              [-1, 9,-1],
                              [-1,-1,-1]])

3.Use kernel in cv2.filter2D()

img_rst = cv2.filter2D(img_src,-1,kernel_sharpening)

4.Saveresultant image

cv2.imwrite('result.jpg',img_rst)

python opencv remove noise in images

To understand how to use cv2.filter2D() , you also can read this tutorial:

Python OpenCV: Implement Image Filtering Using cv2.filter2D() Convolution