Python實現(xiàn)圖像去噪方式(中值去噪和均值去噪)
實現(xiàn)對圖像進行簡單的高斯去噪和椒鹽去噪。
代碼如下:
import numpy as np from PIL import Image import matplotlib.pyplot as plt import random import scipy.misc import scipy.signal import scipy.ndimage from matplotlib.font_manager import FontProperties font_set = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=10) def medium_filter(im, x, y, step): sum_s = [] for k in range(-int(step / 2), int(step / 2) + 1): for m in range(-int(step / 2), int(step / 2) + 1): sum_s.append(im[x + k][y + m]) sum_s.sort() return sum_s[(int(step * step / 2) + 1)] def mean_filter(im, x, y, step): sum_s = 0 for k in range(-int(step / 2), int(step / 2) + 1): for m in range(-int(step / 2), int(step / 2) + 1): sum_s += im[x + k][y + m] / (step * step) return sum_s def convert_2d(r): n = 3 # 3*3 濾波器, 每個系數(shù)都是 1/9 window = np.ones((n, n)) / n ** 2 # 使用濾波器卷積圖像 # mode = same 表示輸出尺寸等于輸入尺寸 # boundary 表示采用對稱邊界條件處理圖像邊緣 s = scipy.signal.convolve2d(r, window, mode='same', boundary='symm') return s.astype(np.uint8) def convert_3d(r): s_dsplit = [] for d in range(r.shape[2]): rr = r[:, :, d] ss = convert_2d(rr) s_dsplit.append(ss) s = np.dstack(s_dsplit) return s def add_salt_noise(img): rows, cols, dims = img.shape R = np.mat(img[:, :, 0]) G = np.mat(img[:, :, 1]) B = np.mat(img[:, :, 2]) Grey_sp = R * 0.299 + G * 0.587 + B * 0.114 Grey_gs = R * 0.299 + G * 0.587 + B * 0.114 snr = 0.9 noise_num = int((1 - snr) * rows * cols) for i in range(noise_num): rand_x = random.randint(0, rows - 1) rand_y = random.randint(0, cols - 1) if random.randint(0, 1) == 0: Grey_sp[rand_x, rand_y] = 0 else: Grey_sp[rand_x, rand_y] = 255 #給圖像加入高斯噪聲 Grey_gs = Grey_gs + np.random.normal(0, 48, Grey_gs.shape) Grey_gs = Grey_gs - np.full(Grey_gs.shape, np.min(Grey_gs)) Grey_gs = Grey_gs * 255 / np.max(Grey_gs) Grey_gs = Grey_gs.astype(np.uint8) # 中值濾波 Grey_sp_mf = scipy.ndimage.median_filter(Grey_sp, (7, 7)) Grey_gs_mf = scipy.ndimage.median_filter(Grey_gs, (8, 8)) # 均值濾波 Grey_sp_me = convert_2d(Grey_sp) Grey_gs_me = convert_2d(Grey_gs) plt.subplot(321) plt.title('加入椒鹽噪聲',fontproperties=font_set) plt.imshow(Grey_sp, cmap='gray') plt.subplot(322) plt.title('加入高斯噪聲',fontproperties=font_set) plt.imshow(Grey_gs, cmap='gray') plt.subplot(323) plt.title('中值濾波去椒鹽噪聲(8*8)',fontproperties=font_set) plt.imshow(Grey_sp_mf, cmap='gray') plt.subplot(324) plt.title('中值濾波去高斯噪聲(8*8)',fontproperties=font_set) plt.imshow(Grey_gs_mf, cmap='gray') plt.subplot(325) plt.title('均值濾波去椒鹽噪聲',fontproperties=font_set) plt.imshow(Grey_sp_me, cmap='gray') plt.subplot(326) plt.title('均值濾波去高斯噪聲',fontproperties=font_set) plt.imshow(Grey_gs_me, cmap='gray') plt.show() def main(): img = np.array(Image.open('E:/pycharm/GraduationDesign/Test/testthree.png')) add_salt_noise(img) if __name__ == '__main__': main()
效果如下
以上這篇Python實現(xiàn)圖像去噪方式(中值去噪和均值去噪)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python?數(shù)據(jù)可視化實現(xiàn)5種炫酷的動態(tài)圖
數(shù)據(jù)可以幫助我們描述這個世界、闡釋自己的想法和展示自己的成果,但如果只有單調(diào)乏味的文本和數(shù)字,我們卻往往能難抓住觀眾的眼球。而很多時候,一張漂亮的可視化圖表就足以勝過千言萬語2022-01-01Django零基礎(chǔ)入門之路由path和re_path詳解
這篇文章主要介紹了Django零基礎(chǔ)入門之路由path和re_path,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09python中cv2.imread()和Image.open()的區(qū)別和聯(lián)系詳解
image.open和cv2.imread都是Python中用于讀取圖像文件的函數(shù),但是它們之間有一些區(qū)別,這篇文章主要給大家介紹了關(guān)于python中cv2.imread()和Image.open()的區(qū)別和聯(lián)系,需要的朋友可以參考下2024-07-07對Python的交互模式和直接運行.py文件的區(qū)別詳解
今天小編就為大家分享一篇對Python的交互模式和直接運行.py文件的區(qū)別詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06python 點云地面點濾波-progressive TIN densification(PTD)算法介紹
關(guān)于地面點濾波的概念我們要與孤立點(outlier)濾波區(qū)分開,孤立點濾波可以理解為圖像中的去噪,去除數(shù)據(jù)測量過程中受到飛鳥、多路徑效應(yīng)所產(chǎn)生的遠低于/高于其他數(shù)據(jù)的點。今天通過本文給大家分享python PTD點云地面點濾波的相關(guān)知識,一起看看吧2021-08-08python中的Json模塊dumps、dump、loads、load函數(shù)用法詳解
這篇文章主要介紹了python中的Json模塊dumps、dump、loads、load函數(shù)用法講解,本文逐一介紹結(jié)合實例代碼給大家講解的非常詳細,需要的朋友可以參考下2022-11-11在Python中利用Into包整潔地進行數(shù)據(jù)遷移的教程
這篇文章主要介紹了在Python中如何利用Into包整潔地進行數(shù)據(jù)遷移,在數(shù)據(jù)格式的任意兩個格式之間高效地遷移數(shù)據(jù),需要的朋友可以參考下2015-03-03