天天看點

數字圖像處理——中值濾波器【像素級别處理】(python)數字圖像處理——中值濾波器【像素級别處理】(python)簡介代碼實作

數字圖像處理——中值濾波器【像素級别處理】(python)

文章目錄

  • 數字圖像處理——中值濾波器【像素級别處理】(python)
  • 簡介
  • 代碼實作

簡介

中值濾波(統計排序濾波器)是一種非線性數字濾波器技術,經常用于去除圖像或者其它信号中的噪聲。這個設計思想就是檢查輸入信号中的采樣并判斷它是否代表了信号,使用奇數個采樣組成的觀察窗實作這項功能。觀察視窗中的數值進行排序,位于觀察窗中間的中值作為輸出。然後,丢棄最早的值,取得新的采樣,重複上面的計算過程。

其中中值濾波對椒鹽噪聲處理非常好

代碼實作

輸入:

數字圖像處理——中值濾波器【像素級别處理】(python)數字圖像處理——中值濾波器【像素級别處理】(python)簡介代碼實作
import cv2
import datetime
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('Fig0335.tif')  # 測試圖檔
H = img.shape[0]
W = img.shape[1]

img3 = np.zeros((H, W, 3), np.uint8)  # 3*3鄰域平滑後的圖像
imgmid = np.zeros((H, W, 3), np.uint8)  # 3*3鄰域内取中值的圖像

tmpImg = np.zeros((H + 2, W + 2, 3), np.uint8)  # 擴充之後的圖像
for i in range(H):
    for j in range(W):
        tmpImg[i + 1, j + 1] = img[i, j]

starttime = datetime.datetime.now()
for i in range(H):
    for j in range(W):
        S = []
        for x in range(3):
            for y in range(3):
                # S[x * 3 + y] = tmpImg[i + x, j + y, 0]
                S.append(tmpImg[i + x, j + y, 0])
        img3[i, j, 0] = sum(S) // 9
        img3[i, j, 1] = img3[i, j, 0]
        img3[i, j, 2] = img3[i, j, 0]
        # 冒泡排序,隻要排到中間一個值,即4,是以x範圍是8->3
        # for x in range(8, 3, -1):
        #     for y in range(x):
        #         if S[y + 1] > S[y]:
        #             temp = S[y]
        #             S[y] = S[y + 1]
        #             S[y + 1] = temp
        # 自帶的排序,timesort,經過測試,這個比冒泡排序要快60%
        S.sort()
        print(S)
        imgmid[i, j, 0] = S[4]
        imgmid[i, j, 1] = imgmid[i, j, 0]
        imgmid[i, j, 2] = imgmid[i, j, 0]

endtime = datetime.datetime.now()
print(endtime - starttime)

# 原圖
plt.subplot(1, 3, 1)
plt.axis('off')
plt.title('Original image')
plt.imshow(img)

# 3*3鄰域
plt.subplot(1, 3, 2)
plt.axis('off')
plt.title('3*3 smoothing')
plt.imshow(img3)

# 鄰域中值替換之後
plt.subplot(1, 3, 3)
plt.axis('off')
plt.title('Middle Value Replaced Image')
plt.imshow(imgmid)

plt.show()

           

輸出:

數字圖像處理——中值濾波器【像素級别處理】(python)數字圖像處理——中值濾波器【像素級别處理】(python)簡介代碼實作
import cv2
import datetime
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('Fig0335.tif')  # 測試圖檔
H = img.shape[0]
W = img.shape[1]

img3 = np.zeros((H, W, 3), np.uint8)  # 3*3鄰域平滑後的圖像
imgmid = np.zeros((H, W, 3), np.uint8)  # 3*3鄰域内取中值的圖像
imgmid5 = np.zeros((H, W, 3), np.uint8)

tmpImg = np.zeros((H + 2, W + 2, 3), np.uint8)  # 擴充之後的圖像
tmpImg5 = np.zeros((H + 4, W + 4, 3), np.uint8)

for i in range(H):
    for j in range(W):
        tmpImg[i + 1, j + 1] = img[i, j]
        tmpImg5[i + 2, j + 2] = img[i, j]

starttime = datetime.datetime.now()
for i in range(H):
    for j in range(W):
        S = []
        T = []
        for x in range(3):
            for y in range(3):
                # S[x * 3 + y] = tmpImg[i + x, j + y, 0]
                S.append(tmpImg[i + x, j + y, 0])
        # img3[i, j, 0] = sum(S) // 9
        # img3[i, j, 1] = img3[i, j, 0]
        # img3[i, j, 2] = img3[i, j, 0]
        # 冒泡排序,隻要排到中間一個值,即4,是以x範圍是8->3
        # for x in range(8, 3, -1):
        #     for y in range(x):
        #         if S[y + 1] > S[y]:
        #             temp = S[y]
        #             S[y] = S[y + 1]
        #             S[y + 1] = temp
        # 自帶的排序,timesort,經過測試,這個比冒泡排序要快60%
        S.sort()
        print(S)
        imgmid[i, j, 0] = S[4]
        imgmid[i, j, 1] = imgmid[i, j, 0]
        imgmid[i, j, 2] = imgmid[i, j, 0]
        for x in range(5):
            for y in range(5):
                T.append(tmpImg5[i + x, j + y, 0])
        T.sort()
        imgmid5[i, j, 0] = T[12]
        imgmid5[i, j, 1] = T[12]
        imgmid5[i, j, 2] = T[12]

endtime = datetime.datetime.now()
print(endtime - starttime)

# 原圖
plt.subplot(1, 3, 1)
plt.axis('off')
plt.title('Original image')
plt.imshow(img)

# 3*3鄰域
plt.subplot(1, 3, 2)
plt.axis('off')
plt.title('5*5 smoothing')
plt.imshow(imgmid5)

# 鄰域中值替換之後
plt.subplot(1, 3, 3)
plt.axis('off')
plt.title('Middle Value Replaced Image')
plt.imshow(imgmid)

plt.show()

           
數字圖像處理——中值濾波器【像素級别處理】(python)數字圖像處理——中值濾波器【像素級别處理】(python)簡介代碼實作