I have this image which has been heavily obscured by impulse(salt&pepper) noise and I am trying to filter it using a median filter. I was able to filter the picture to a slightly readable state, but I want them to filter out to a much more clear image. How can my median filter be improved?
The picture is represented by a (n,m,4) array, with grayscale rgb values between 0 (black) and 1 (white). I used a 3x3 matrix for my median filter, and gave the image a 1-pixel wide white border around each edge.
def medianFilter(image=np.ndarray):
for i in range(1,image.shape[0]):
for j in range(1,image.shape[1]):
square = np.array(image[i-1:i 2,j-1:j 2])
if(square[1,1][0]==1 or square[1,1][0]==0):
square[1,1] = np.median((square))
image[i-1:i 2,j-1:j 2] = square
return image
CodePudding user response:
OK so starting with the picture you uploaded, we can do the following:
import numpy as np
from scipy.signal import medfilt2d
import matplotlib.pyplot as plt
import cv2
img = cv2.imread('fBq2z.png', cv2.IMREAD_GRAYSCALE)
Median Filtering
The best way to handle salt & pepper noise is to use median filter. Since the median filter is directly affected by the kernel size, too small is not enough for a good median estimation and too big will catch artifacts in the median filter. After playing with different kernel sizes, 11
was best.
median_kernel = 11
# median filtering
median_filtered = medfilt2d(img, kernel_size=median_kernel)
The result of the median filterring is:
Image inpainting
The next stage will be to fix the places where the salt & pepper was prominent. This is done using image inpainting. Two algorithms are typically used for traditional inpainting, modern approaches use neural networks for that.