# How can I filter the gaussian noise from an image using segmentation?

Hello guys, I’ve added some Gaussian Noise to my image but it looks like it’s not completely eliminated from my image, what am I doing wrong? This is my code:

``````# Import numpy, skimage and matplotlib
import numpy as np
from skimage import io,color, util
import matplotlib.pyplot as plt

# Grayscale conversion
img = color.rgb2gray(img)
# add Gaussian noise to the image
# the variance sets the width of the gaussian
img = util.random_noise(img, mode='gaussian', mean=0, var = (10/255)**2)
img = img*255
# Displaying the grayscale image
plt.figure(),plt.imshow(img,cmap='gray', vmin = 0, vmax = 255), plt.colorbar()
hist,_ = np.histogram(img,range(0,256))
plt.figure(figsize=(10,10)),plt.plot(hist)

segm = np.zeros(img.shape)
segm[img<25] = 0
segm[np.logical_and(img>=25, img<75)] = 1
segm[np.logical_and(img>=75, img<125)] = 2
segm[np.logical_and(img>=125, img<175)] = 3
segm[img>175] = 4
plt.figure(figsize=(10,10)),plt.imshow(segm,cmap='gray', vmin = 0, vmax = 4), plt.colorbar()
``````

And this is my result:

It looks like I still have some salt&pepper noise present, how can I eliminate it? I don’t want to use any Skimage functions built-in Python, I need to use code made by me.

Just a note:
Without prior knowledge it is impossible to remove stochastic noise, you can at best reduce it with a certain impact on the image structures.

Yeah, at least to reduce it, I have this code but it’s not OK because it did not do the segmentation on the new filtered image.

``````from skimage.morphology import disk
from skimage.filters import rank
# Define the neighbourd disk whose mean you would like to take for mean filtering
selem = disk(4)
# Convert image to uint8 for converting the image into required format
img = util.img_as_ubyte(img/255)
# Get the result by performing mean filter
normal_result = rank.mean(img, selem=selem)

fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(10, 10),
sharex=True, sharey=True)
ax = axes.ravel()

titles = ['Original','After Mean filter']
imgs = [img, normal_result]
for n in range(0, len(imgs)):
ax[n].imshow(imgs[n], cmap=plt.cm.gray)
ax[n].set_title(titles[n])
ax[n].axis('off')

plt.tight_layout()
plt.show()
``````