Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Učitati sliku lena.png i dodati joj AWGN varijanse 100 i srednje vrednosti 0.
- Nad ovom zašumljenom slikom dodati impulsni so i biber šum gustine 5%.
- Za potrebe uklanjanja ovog kombinovanog šuma potrebno je napisati funkciju
- alphaTrimmedMeanFilt koja implementira alfa-trimovani usrednjivač dat formulom:
- '''
- from skimage import io
- import plotly.express as px
- import numpy as np
- img = io.imread('lena.png')
- fig = px.imshow(img, color_continuous_scale='gray')
- fig.show()
- rng = np.random.default_rng()
- img_noisy = img + rng.standard_normal(size=img.shape)*np.sqrt(100) + 0
- img_noisy[img_noisy>255] = 255
- img_noisy[img_noisy<0] = 0
- p = rng.uniform(size=img.shape)
- p0 = 0.05
- img_noisy[p<p0/2] = 0
- img_noisy[(p0/2 <= p) & (p<p0)] = 255
- fig = px.imshow(img_noisy, color_continuous_scale='gray')
- fig.show()
- import math
- def alphaTrimmedMeanFilt(g, S_shape, alpha):
- f = np.zeros(g.shape)
- pad_rows = math.floor(S_shape[0]/2)
- pad_cols = math.floor(S_shape[1]/2)
- img_p = np.pad(g, ((pad_rows,),(pad_cols,)), mode='symmetric')
- for row in range(f.shape[0]):
- for col in range(f.shape[1]):
- region = img_p[row:row+S_shape[0], col:col+S_shape[1]]
- region = np.sort(region, axis = None)
- res = region[alpha//2:-alpha//2].mean()
- f[row,col] = res
- return f
- # 3x3 region
- S_shape = (3,3)
- img_f = alphaTrimmedMeanFilt(img_noisy, S_shape, alpha=4)
- fig = px.imshow(img_f, color_continuous_scale='gray')
- fig.show()
- psnr1 = 10*np.log10(255**2/((img - img_noisy)**2).mean())
- psnr2 = 10*np.log10(255**2/((img - img_f)**2).mean())
- print("PSNR za zasumljenu sliku je {}".format(psnr1))
- print("PSNR za alfa-trimovani usrednjivac dimenzije {} je {}".format(S_shape, psnr2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement