最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實(shí)現(xiàn)圖像去噪方式(中值去噪和均值去噪)

 更新時(shí)間:2019年12月18日 09:31:29   作者:初見(jiàn)與告別  
今天小編就為大家分享一篇Python實(shí)現(xiàn)圖像去噪方式(中值去噪和均值去噪),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

實(shí)現(xiàn)對(duì)圖像進(jìn)行簡(jiǎ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 濾波器, 每個(gè)系數(shù)都是 1/9
  window = np.ones((n, n)) / n ** 2
  # 使用濾波器卷積圖像
  # mode = same 表示輸出尺寸等于輸入尺寸
  # boundary 表示采用對(duì)稱邊界條件處理圖像邊緣
  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實(shí)現(xiàn)圖像去噪方式(中值去噪和均值去噪)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python接口測(cè)試數(shù)據(jù)庫(kù)封裝實(shí)現(xiàn)原理

    Python接口測(cè)試數(shù)據(jù)庫(kù)封裝實(shí)現(xiàn)原理

    這篇文章主要介紹了Python接口測(cè)試數(shù)據(jù)庫(kù)封裝實(shí)現(xiàn)原理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • python輸入、數(shù)據(jù)類型轉(zhuǎn)換及運(yùn)算符方式

    python輸入、數(shù)據(jù)類型轉(zhuǎn)換及運(yùn)算符方式

    這篇文章主要介紹了python輸入、數(shù)據(jù)類型轉(zhuǎn)換及運(yùn)算符方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Python word2vec訓(xùn)練詞向量實(shí)例分析講解

    Python word2vec訓(xùn)練詞向量實(shí)例分析講解

    這篇文章主要介紹了Python word2vec訓(xùn)練詞向量實(shí)例分析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧
    2022-12-12
  • Anaconda徹底刪除虛擬環(huán)境的正確方法

    Anaconda徹底刪除虛擬環(huán)境的正確方法

    這篇文章主要給大家介紹了關(guān)于Anaconda徹底刪除虛擬環(huán)境的正確方法,要在Anaconda中刪除一個(gè)虛擬環(huán)境,可以按照本文以下步驟進(jìn)行操作,需要的朋友可以參考下
    2023-10-10
  • Python多線程中比time.sleep更好用的暫停方式

    Python多線程中比time.sleep更好用的暫停方式

    這篇文章主要介紹了Python多線程中比time.sleep更好用的暫停方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • python爬蟲(chóng)爬取某站上海租房圖片

    python爬蟲(chóng)爬取某站上海租房圖片

    這篇文章主要介紹了學(xué)習(xí)python爬蟲(chóng)并爬取某站上海租房圖片的原理介紹以及相關(guān)代碼分享,對(duì)此有興趣的朋友學(xué)習(xí)下。
    2018-02-02
  • Python將多個(gè)圖像合并輸出的實(shí)現(xiàn)方法

    Python將多個(gè)圖像合并輸出的實(shí)現(xiàn)方法

    這篇文章主要介紹了Python將多個(gè)圖像合并輸出的實(shí)現(xiàn)方法,本文介紹了兩種將多個(gè)圖像合并為一個(gè)輸出的方法:使用PIL庫(kù)或使用OpenCV和NumPy,這些庫(kù)都可以使用Python中的簡(jiǎn)單語(yǔ)法和少量的代碼來(lái)完成此任務(wù),需要的朋友可以參考下
    2023-06-06
  • pycharm 中mark directory as exclude的用法詳解

    pycharm 中mark directory as exclude的用法詳解

    今天小編就為大家分享一篇pycharm 中mark directory as exclude的用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02
  • 淺談在django中使用redirect重定向數(shù)據(jù)傳輸?shù)膯?wèn)題

    淺談在django中使用redirect重定向數(shù)據(jù)傳輸?shù)膯?wèn)題

    這篇文章主要介紹了淺談在django中使用redirect重定向數(shù)據(jù)傳輸?shù)膯?wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • 使用Flask和Django中解決跨域請(qǐng)求問(wèn)題

    使用Flask和Django中解決跨域請(qǐng)求問(wèn)題

    這篇文章主要介紹了使用Flask和Django中解決跨域請(qǐng)求問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-04-04

最新評(píng)論

荆州市| 营山县| 保靖县| 松潘县| 麻江县| 醴陵市| 文成县| 正镶白旗| 黄浦区| 灵石县| 固原市| 咸阳市| 资源县| 宜昌市| 文化| 隆尧县| 定襄县| 宜宾县| 邯郸县| 京山县| 浪卡子县| 黔江区| 皮山县| 新津县| 海晏县| 安乡县| 望奎县| 信阳市| 乌拉特前旗| 九江县| 监利县| 连州市| 曲周县| 朝阳市| 虹口区| 镇江市| 纳雍县| 重庆市| 华蓥市| 桂阳县| 寻乌县|